using System; using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; [Serializable] public abstract class UIBaseManager : MonoBehaviour, ManagerInterface { private Animator animator; void Start() { animator = GetComponent(); } public static UIBaseManager Instance { get; protected set; } [SerializeField] public GameObject DynamicPanel; [SerializeField] public KeyCode keyToOpen; /// /// Managed objects list - most important element in module /// [SerializeField] protected List Elements = new List(); // { get; set; } - this block inspector public virtual void Awake() { if (Instance == null) { Instance = this; } else { Debug.Log(gameObject); Debug.Log(Instance); Destroy(gameObject); } } //public GameObject Chest1 = GameObject.Find("Gold Chest"); public async void Update() { if (Input.GetKeyDown(keyToOpen)) { if (!DynamicPanel) { //condition to be near to the chest if (ChestUIManager.Instance.CurrentChestName != null && ChestUIManager.Instance.CurrentChestName != "") { //delay using async Yield and triggering chests to start open animation var end = Time.time + 0.3; animateOpening(); while (Time.time < end) { await System.Threading.Tasks.Task.Yield(); } } this.OpenPanel(); } else { this.ClosePanel(); var end2 = Time.time + 0.1; while (Time.time < end2) { await System.Threading.Tasks.Task.Yield(); } animateClosing(); end2 = Time.time + 0.3; while (Time.time < end2) { await System.Threading.Tasks.Task.Yield(); } resettriggers(); } } if (ChestUIManager.Instance.CurrentChestName == null || ChestUIManager.Instance.CurrentChestName == "") { var end2 = Time.time + 0.1; while (Time.time < end2) { await System.Threading.Tasks.Task.Yield(); } animateClosing(); end2 = Time.time + 0.3; while (Time.time < end2) { await System.Threading.Tasks.Task.Yield(); } resettriggers(); } } /// /// Function to fetch currently opened panel asociated with this UI manager /// /// public void SetPanelController(GameObject panelConrtoller) { // ToDo change fetched types //DynamicPanel = dynamicPanelController; } private async void animateOpening() { GameObject.Find("Gold Chest").GetComponent().SetTrigger("OpenIt"); GameObject.Find("Prize Chest").GetComponent().SetTrigger("OpenIt"); GameObject.Find("Wodden Chest").GetComponent().SetTrigger("OpenIt"); } private async void animateClosing() { GameObject.Find("Gold Chest").GetComponent().ResetTrigger("OpenIt"); GameObject.Find("Prize Chest").GetComponent().ResetTrigger("OpenIt"); GameObject.Find("Wodden Chest").GetComponent().ResetTrigger("OpenIt"); GameObject.Find("Gold Chest").GetComponent().SetTrigger("CloseIt"); GameObject.Find("Prize Chest").GetComponent().SetTrigger("CloseIt"); GameObject.Find("Wodden Chest").GetComponent().SetTrigger("CloseIt"); } private async void resettriggers() { var end3 = Time.time + 0.0; while (Time.time < end3) { await System.Threading.Tasks.Task.Yield(); } GameObject.Find("Gold Chest").GetComponent().ResetTrigger("CloseIt"); GameObject.Find("Prize Chest").GetComponent().ResetTrigger("CloseIt"); GameObject.Find("Wodden Chest").GetComponent().ResetTrigger("CloseIt"); GameObject.Find("Wodden Chest").GetComponent().SetTrigger("reactivate"); GameObject.Find("Prize Chest").GetComponent().SetTrigger("reactivate"); GameObject.Find("Gold Chest").GetComponent().SetTrigger("reactivate"); } public virtual bool OpenPanel() { Console.WriteLine("Panel opened"); GameObject globalGUI = GameObject.FindGameObjectWithTag("GUI"); if(!globalGUI) throw new Exception("Panel could not be opened - can't find global GUI object!!"); GameObject uiPanelTemplate = GetTemplatePanel(); DynamicPanel = GameObject.Instantiate(uiPanelTemplate, uiPanelTemplate.transform.position, Quaternion.identity, globalGUI.transform); // 4'th arg allow set object as child DynamicPanel.transform.localPosition = uiPanelTemplate.transform.position; // prevent overwritten position by... environment??? DynamicPanel.name = uiPanelTemplate.name; SetupPanel(); return true; } public virtual bool ClosePanel() { Console.WriteLine("Panel closed"); try { Destroy(DynamicPanel); return true; } catch (Exception ex) { Debug.LogError(ex.Message); } return true; } public virtual void SetupPanel() { if(!DynamicPanel) throw new Exception("Panel not found - UIBaseManager don't have UI Panel instance!!"); // bind this class instance //DynamicPanel.GetComponent().Bind(this); } /// /// Function which return panel status /// True - if panel is opened and it's instance is handled /// false - if panel is closed and it's instance is not handled /// /// public bool GetPanelStatus() { return DynamicPanel != null; } public List GetList() { return Elements; } /// /// Function to setting list of elements - invoked once at the beginning /// Reset current content /// /// /// public void SetList(List list) { Elements = list; if(DynamicPanel != null) // is null = is close - when we firstly init list content before open panel in SceneTaskDataManager file UpdateList(); } /// /// Set of functions to synchronize local list with list from SystemManager /// public void Add(T model) { // throw new System.NotImplementedException(); Elements.Add(model); UpdateList(); // add to local list // and rebuild view - sync lists (this.UpdateList() ;) ) } public abstract void UpdateList(); public void Remove() { UpdateList(); throw new System.NotImplementedException(); // remove from local list // and rebuild view - sync lists (this.UpdateList() ;) ) } /// /// Function to find saved template of created panel instance /// /// protected abstract GameObject GetTemplatePanel(); }