CreateNewUI() public static method

public static CreateNewUI ( ) : GameObject
return UnityEngine.GameObject
Ejemplo n.º 1
0
        // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
        static public GameObject GetOrCreateCanvasGameObject()
        {
            GameObject selectedGo = Selection.activeGameObject;

            // Try to find a gameobject that is the selected GO or one if its parents.
            Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent <Canvas>() : null;

            if (IsValidCanvas(canvas))
            {
                return(canvas.gameObject);
            }

            // No canvas in selection or its parents? Then use any valid canvas.
            // We have to find all loaded Canvases, not just the ones in main scenes.
            Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType <Canvas>();
            for (int i = 0; i < canvasArray.Length; i++)
            {
                if (IsValidCanvas(canvasArray[i]))
                {
                    return(canvasArray[i].gameObject);
                }
            }

            // No canvas in the scene at all? Then create a new one.
            return(MenuOptions.CreateNewUI());
        }
Ejemplo n.º 2
0
        private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
        {
            GameObject parent = menuCommand.context as GameObject;
            bool       explicitParentChoice = true;

            if (parent == null)
            {
                parent = GetOrCreateCanvasGameObject();
                explicitParentChoice = false;

                // If in Prefab Mode, Canvas has to be part of Prefab contents,
                // otherwise use Prefab root instead.
                PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
                {
                    parent = prefabStage.prefabContentsRoot;
                }
            }

            if (parent.GetComponentInParent <Canvas>() == null)
            {
                // Create canvas under context GameObject,
                // and make that be the parent which UI element is added under.
                GameObject canvas = MenuOptions.CreateNewUI();
                canvas.transform.SetParent(parent.transform, false);
                parent = canvas;
            }

            // Setting the element to be a child of an element already in the scene should
            // be sufficient to also move the element to that scene.
            // However, it seems the element needs to be already in its destination scene when the
            // RegisterCreatedObjectUndo is performed; otherwise the scene it was created in is dirtied.
            SceneManager.MoveGameObjectToScene(element, parent.scene);

            Undo.RegisterCreatedObjectUndo(element, "Create " + element.name);

            if (element.transform.parent == null)
            {
                Undo.SetTransformParent(element.transform, parent.transform, "Parent " + element.name);
            }

            GameObjectUtility.EnsureUniqueNameForSibling(element);

            // We have to fix up the undo name since the name of the object was only known after reparenting it.
            Undo.SetCurrentGroupName("Create " + element.name);

            GameObjectUtility.SetParentAndAlign(element, parent);
            if (!explicitParentChoice) // not a context click, so center in sceneview
            {
                SetPositionVisibleinSceneView(parent.GetComponent <RectTransform>(),
                                              element.GetComponent <RectTransform>());
            }

            Selection.activeGameObject = element;
        }
Ejemplo n.º 3
0
        public static void AddCanvas(MenuCommand menuCommand)
        {
            GameObject newUi = MenuOptions.CreateNewUI();

            GameObjectUtility.SetParentAndAlign(newUi, menuCommand.context as GameObject);
            if ((bool)((Object)(newUi.transform.parent as RectTransform)))
            {
                RectTransform transform = newUi.transform as RectTransform;
                transform.anchorMin        = Vector2.zero;
                transform.anchorMax        = Vector2.one;
                transform.anchoredPosition = Vector2.zero;
                transform.sizeDelta        = Vector2.zero;
            }
            Selection.activeGameObject = newUi;
        }
Ejemplo n.º 4
0
        public static void AddCanvas(MenuCommand menuCommand)
        {
            GameObject gameObject = MenuOptions.CreateNewUI();

            GameObjectUtility.SetParentAndAlign(gameObject, menuCommand.context as GameObject);
            if (gameObject.transform.parent as RectTransform)
            {
                RectTransform rectTransform = gameObject.transform as RectTransform;
                rectTransform.anchorMin        = Vector2.zero;
                rectTransform.anchorMax        = Vector2.one;
                rectTransform.anchoredPosition = Vector2.zero;
                rectTransform.sizeDelta        = Vector2.zero;
            }
            Selection.activeGameObject = gameObject;
        }
Ejemplo n.º 5
0
        public static GameObject GetOrCreateCanvasGameObject()
        {
            GameObject activeGameObject = Selection.activeGameObject;
            Canvas     canvas           = !((Object)activeGameObject != (Object)null) ? (Canvas)null : activeGameObject.GetComponentInParent <Canvas>();

            if ((Object)canvas != (Object)null && canvas.gameObject.activeInHierarchy)
            {
                return(canvas.gameObject);
            }
            Canvas objectOfType = Object.FindObjectOfType(typeof(Canvas)) as Canvas;

            if ((Object)objectOfType != (Object)null && objectOfType.gameObject.activeInHierarchy)
            {
                return(objectOfType.gameObject);
            }
            return(MenuOptions.CreateNewUI());
        }
Ejemplo n.º 6
0
        private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
        {
            GameObject parent = menuCommand.context as GameObject;
            bool       explicitParentChoice = true;

            if (parent == null)
            {
                parent = GetOrCreateCanvasGameObject();
                explicitParentChoice = false;

                // If in Prefab Mode, Canvas has to be part of Prefab contents,
                // otherwise use Prefab root instead.
                PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
                {
                    parent = prefabStage.prefabContentsRoot;
                }
            }
            if (parent.GetComponentsInParent <Canvas>(true).Length == 0)
            {
                // Create canvas under context GameObject,
                // and make that be the parent which UI element is added under.
                GameObject canvas = MenuOptions.CreateNewUI();
                Undo.SetTransformParent(canvas.transform, parent.transform, "");
                parent = canvas;
            }

            GameObjectUtility.EnsureUniqueNameForSibling(element);

            SetParentAndAlign(element, parent);
            if (!explicitParentChoice) // not a context click, so center in sceneview
            {
                SetPositionVisibleinSceneView(parent.GetComponent <RectTransform>(), element.GetComponent <RectTransform>());
            }

            // This call ensure any change made to created Objects after they where registered will be part of the Undo.
            Undo.RegisterFullObjectHierarchyUndo(parent == null ? element : parent, "");

            // We have to fix up the undo name since the name of the object was only known after reparenting it.
            Undo.SetCurrentGroupName("Create " + element.name);

            Selection.activeGameObject = element;
        }
Ejemplo n.º 7
0
        // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas.
        static public GameObject GetOrCreateCanvasGameObject()
        {
            GameObject selectedGo = Selection.activeGameObject;

            // Try to find a gameobject that is the selected GO or one if its parents.
            Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent <Canvas>() : null;

            if (canvas != null && canvas.gameObject.activeInHierarchy)
            {
                return(canvas.gameObject);
            }

            // No canvas in selection or its parents? Then use just any canvas..
            canvas = Object.FindObjectOfType(typeof(Canvas)) as Canvas;
            if (canvas != null && canvas.gameObject.activeInHierarchy)
            {
                return(canvas.gameObject);
            }

            // No canvas in the scene at all? Then create a new one.
            return(MenuOptions.CreateNewUI());
        }
Ejemplo n.º 8
0
        public static GameObject GetOrCreateCanvasGameObject()
        {
            GameObject activeGameObject = Selection.activeGameObject;
            Canvas     canvas           = (!(activeGameObject != null)) ? null : activeGameObject.GetComponentInParent <Canvas>();
            GameObject result;

            if (canvas != null && canvas.gameObject.activeInHierarchy)
            {
                result = canvas.gameObject;
            }
            else
            {
                canvas = (UnityEngine.Object.FindObjectOfType(typeof(Canvas)) as Canvas);
                if (canvas != null && canvas.gameObject.activeInHierarchy)
                {
                    result = canvas.gameObject;
                }
                else
                {
                    result = MenuOptions.CreateNewUI();
                }
            }
            return(result);
        }