Esempio n. 1
0
        /// <summary>
        /// Shows popup for the user to choose a Canvas that will be used and adds a MenuController component to it.
        /// </summary>
        public static void CreateMenuController(UnityEngine.Events.UnityAction <MenuController> onFinish)
        {
            Canvas[] canvasses   = HierarchyHelper.GetSelectedOrGeneralObjectsOfType <Canvas>();
            Vector2  coordinates = EditorUtils.GetMousePosition();

            try {
                PopupWindow.Show(new Rect(GUIUtility.ScreenToGUIPoint(coordinates), new Vector2(250, 150)), new CanvasSelector(canvasses, (Canvas canvas) => {
                    if (canvas == null)
                    {
                        // The user wants us to create a canvas
                        EditorApplication.ExecuteMenuItem("GameObject/UI/Canvas");
                        Canvas[] newCanvasses = HierarchyHelper.GetObjectsOfType <Canvas>();
                        canvasses             = newCanvasses.Except(canvasses).ToArray();
                        if (canvasses == null || canvasses.Length <= 0)
                        {
                            Debug.LogError("Something went wrong with creating the new canvas. Please try again.");
                            return;
                        }
                        canvas = canvasses[0];
                    }
                    MenuController result = Undo.AddComponent <MenuController>(canvas.gameObject);
                    if (onFinish != null)
                    {
                        onFinish(result);
                    }
                }));
            } catch (UnityEngine.ExitGUIException) {
                // https://answers.unity.com/questions/385235/editorguilayoutcolorfield-inside-guilayoutwindow-c.html
            }
        }
Esempio n. 2
0
        /// <summary>
        /// TODO: now only called when hierarchy changes, not when a new prefab is dragged into the scene.
        /// </summary>
        private void UpdateAvailableMenuControllers()
        {
            EnsureSettingsObject();
            // TODO: support MenuControllers in prefabs?
            _availableMenuControllers = HierarchyHelper.GetObjectsOfType <MenuController>();

#if UNITY_2018_3_OR_NEWER
            // Add the open prefab to the list.
            PrefabStage currentPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();
            if (currentPrefabStage != null)
            {
                AddPrefabStageMenuControllers(currentPrefabStage);
            }
#endif
            int labelSize = _availableMenuControllers.Length;
            _availableMenuControllersLabels = new string[labelSize];
            for (int i = 0; i < _availableMenuControllers.Length; i++)
            {
                _availableMenuControllersLabels[i] = _availableMenuControllers[i].name;
                if (_editorMenuCreatorSettings.MenuController == _availableMenuControllers[i])
                {
                    _selectedMenuController = i;
                }
            }
            if (_selectedMenuController >= _availableMenuControllers.Length)
            {
                _selectedMenuController = -1;
            }
        }
Esempio n. 3
0
 private static void AddMenu(MenuCommand menuCommand)
 {
     if (Selection.objects.Length > 1)
     {
         if (menuCommand.context != Selection.objects[0])
         {
             return;
         }
     }
     // Use the selected objects.
     MenuController[] menuControllers = HierarchyHelper.GetSelectedOrGeneralObjectsOfType <MenuController>();
     if (menuControllers.Length <= 0)
     {
         if (EditorUtility.DisplayDialog("Adding menu", "Could not find any MenuController in this scene.", "Create MenuController", "Cancel"))
         {
             // If the user wanted to create a menucontroller, set the new menucontroller that is created as selected.
             CreateMenuController((MenuController menuController) => {
                 AddMenuToMenuController(new MenuController[] { menuController });
             });
         }
         else
         {
             return;
         }
     }
     else
     {
         AddMenuToMenuController(menuControllers);
     }
 }