using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; public class UIHandler : MonoBehaviour, IObserver { [Header("Observer Type: ")] [SerializeField] private NotificationType notificationType; [SerializeField] private List<Building> buildings = new List<Building>(); private Dictionary<string, GameObject> goInstantiated = new Dictionary<string, GameObject>(); private Canvas canvas; // Start is called before the first frame update void Start() { buildings = FindObjectsOfType<Building>().ToList(); for(int i = 0; i < buildings.Count; i++) { buildings[i].AddObserver(this); } canvas = GetComponent<Canvas>(); } // Update is called once per frame void Update() { } //Notify the observer public void OnNotify(ObserverNotification notification) { if (notification.NotificationType == notificationType) { if (notification.ContainsKey("UIObject")) { GameObject uiObj; string message; string objectID; if (notification.TryGetData("UIObject", out uiObj)) { if (notification.TryGetData("Message", out message) && notification.TryGetData("ObjectID", out objectID)) { // Instantiate the UI object and store it with the unique ID GameObject instance = Instantiate(uiObj, gameObject.transform); instance.GetComponent<TextMeshProUGUI>().text = message; goInstantiated[objectID] = instance; } else { Instantiate(uiObj, gameObject.transform); } } } else if (notification.ContainsKey("DestroyUI")) { string uniqueID = notification.GetData<string>("DestroyUI"); if (goInstantiated.ContainsKey(uniqueID)) { Destroy(goInstantiated[uniqueID]); goInstantiated.Remove(uniqueID); } } } } }