private void AddLifeCycleNotifierAndSetDestroyCallback(GameObject gameObject) { LifecycleNotifier notifier = gameObject.AddComponent <LifecycleNotifier>() as LifecycleNotifier; notifier.OnDestroyCallback = () => { m_CurrentDialog = null; }; }
/// <summary> /// Creates the UI from a prefab. Configures the UI. Shows the dialog. /// </summary> private void InstantiateDialog() { if (m_CurrentDialog == null) { Debug.LogError(this + " requires m_CurrentDialog. Not showing dialog."); return; } // Load this once if (UIFakeStoreCanvasPrefab == null) { UIFakeStoreCanvasPrefab = Resources.Load("UIFakeStoreCanvas") as GameObject; } Canvas dialogCanvas = UIFakeStoreCanvasPrefab.GetComponent <Canvas>(); // To show, and to configure UI, first realize it on screen m_Canvas = Object.Instantiate(dialogCanvas); // TRICKY: I support one dialog at a time but there's a delay between a request // to the UI system to destroy a UI element and the UI system completing the destruction. // To avoid conflicts with partially destroyed dialogs hanging around too long we add a // custom behavior to the scene explicitly to notify me when the UI has been destroyed. LifecycleNotifier notifier = m_Canvas.gameObject.AddComponent <LifecycleNotifier>() as LifecycleNotifier; notifier.OnDestroyCallback = () => { // Indicates we've completely closed our dialog m_CurrentDialog = null; }; m_ParentGameObjectPath = m_Canvas.name + "/Panel/"; // Ensure existence of EventSystem for use by UI if (Object.FindObjectOfType <EventSystem>() == null) { // No EventSystem found, create a new one and add to the Canvas m_EventSystem = new GameObject("EventSystem", typeof(EventSystem)); m_EventSystem.AddComponent <StandaloneInputModule>(); m_EventSystem.transform.parent = m_Canvas.transform; } // Configure the dialog var qt = GameObject.Find(m_ParentGameObjectPath + "HeaderText"); Text queryTextComponent = qt.GetComponent <Text>(); queryTextComponent.text = m_CurrentDialog.QueryText; Text allowText = GetOkayButtonText(); allowText.text = m_CurrentDialog.OkayButtonText; Text denyText = GetCancelButtonText(); denyText.text = m_CurrentDialog.CancelButtonText; // Populate the dropdown GetDropdown().options.Clear(); // Assume it has defaults prepopulated foreach (var item in m_CurrentDialog.Options) { GetDropdown().options.Add(new Dropdown.OptionData(item)); } if (m_CurrentDialog.Options.Count > 0) { m_LastSelectedDropdownIndex = 0; } // Ensure the dropdown renders its default value GetDropdown().RefreshShownValue(); // Wire up callbacks GetOkayButton().onClick.AddListener(() => { this.OkayButtonClicked(); }); GetCancelButton().onClick.AddListener(() => { this.CancelButtonClicked(); }); GetDropdown().onValueChanged.AddListener((int selectedItem) => { this.DropdownValueChanged(selectedItem); }); // Honor FakeStoreUIMode if (UIMode == FakeStoreUIMode.StandardUser) { GetDropdown().onValueChanged.RemoveAllListeners(); GameObject.Destroy(GetDropdownContainerGameObject()); } else if (UIMode == FakeStoreUIMode.DeveloperUser) { GetCancelButton().onClick.RemoveAllListeners(); GameObject.Destroy(GetCancelButtonGameObject()); } }