// 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;
        Canvas canvas2 = UnityEngine.Object.FindObjectOfType <Canvas>();

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

        // No canvas in the scene at all? Then create a new one.
        return(MenuOptions.CreateNewUI());
    }
예제 #2
0
    static public void AddCanvas(MenuCommand menuCommand)
    {
        var go = MenuOptions.CreateNewUI();

        MenuOptions.SetParentAndAlign(go, menuCommand.context as GameObject);
        if (go.transform.parent as RectTransform)
        {
            RectTransform rect = go.transform as RectTransform;
            rect.anchorMin        = Vector2.zero;
            rect.anchorMax        = Vector2.one;
            rect.anchoredPosition = Vector2.zero;
            rect.sizeDelta        = Vector2.zero;
        }
        Selection.activeGameObject = go;
    }
    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;
    }