Ejemplo n.º 1
0
        internal static void Place(GameObject go, GameObject parent)
        {
            Transform defaultObjectTransform = SceneView.GetDefaultParentObjectIfSet();

            if (parent != null)
            {
                SetGameObjectParent(go, parent.transform);
            }
            else if (defaultObjectTransform != null)
            {
                SetGameObjectParent(go, defaultObjectTransform);
            }
            else
            {
                // When creating a 3D object without a parent, this option puts it at the world origin instead of scene pivot.
                if (placeObjectsAtWorldOrigin)
                {
                    go.transform.position = Vector3.zero;
                }
                else
                {
                    SceneView.PlaceGameObjectInFrontOfSceneView(go);
                }

                StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
            Undo.SetCurrentGroupName("Create " + go.name);

            EditorWindow.FocusWindowIfItsOpen <SceneHierarchyWindow>();
            Selection.activeGameObject = go;
        }
Ejemplo n.º 2
0
        private static void CreateTextMeshProObjectPerform(MenuCommand command)
        {
            GameObject go = ObjectFactory.CreateGameObject("Text - RTLTMP");

            // Add support for new prefab mode
            StageUtility.PlaceGameObjectInCurrentStage(go);

            var textComponent = ObjectFactory.AddComponent <RTLTextMeshPro3D>(go);

            if (TMP_Settings.autoSizeTextContainer)
            {
                Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
                textComponent.rectTransform.sizeDelta = size;
            }
            else
            {
                textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProTextContainerSize;
            }

            textComponent.text      = "Sample text";
            textComponent.alignment = TextAlignmentOptions.TopLeft;

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

            GameObject contextObject = command.context as GameObject;

            if (contextObject != null)
            {
                GameObjectUtility.SetParentAndAlign(go, contextObject);
                Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name);
            }

            Selection.activeGameObject = go;
        }
Ejemplo n.º 3
0
        internal static void Place(GameObject go, GameObject parentTransform)
        {
            if (parentTransform != null)
            {
                var transform = go.transform;
                Undo.SetTransformParent(transform, parentTransform.transform, "Reparenting");
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.identity;
                transform.localScale    = Vector3.one;
                go.layer = parentTransform.gameObject.layer;

                if (parentTransform.GetComponent <RectTransform>())
                {
                    ObjectFactory.AddComponent <RectTransform>(go);
                }
            }
            else
            {
                PlaceGameObjectInFrontOfSceneView(go);

                StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
            Undo.SetCurrentGroupName("Create " + go.name);
            Selection.activeGameObject = go;
            if (EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D)
            {
                var position = go.transform.position;
                position.z            = 0;
                go.transform.position = position;
            }
        }
Ejemplo n.º 4
0
        static void CreateTerrain(MenuCommand menuCommand)
        {
            // Create the storage for the terrain in the project
            // (So we can reuse it in multiple scenes)
            TerrainData terrainData = new TerrainData();
            const int   size        = 1025;

            terrainData.heightmapResolution = size;
            terrainData.size = new Vector3(1000, 600, 1000);

            terrainData.heightmapResolution = 512;
            terrainData.baseMapResolution   = 1024;
            terrainData.SetDetailResolution(1024, terrainData.detailResolutionPerPatch);

            AssetDatabase.CreateAsset(terrainData, AssetDatabase.GenerateUniqueAssetPath("Assets/New Terrain.asset"));
            var        parent  = menuCommand.context as GameObject;
            GameObject terrain = Terrain.CreateTerrainGameObject(terrainData);

            terrain.name = "Terrain";

            GameObjectUtility.SetParentAndAlign(terrain, parent);
            StageUtility.PlaceGameObjectInCurrentStage(terrain);
            GameObjectUtility.EnsureUniqueNameForSibling(terrain);
            Selection.activeObject = terrain;
            Undo.RegisterCreatedObjectUndo(terrain, "Create terrain");
        }
Ejemplo n.º 5
0
        static public GameObject CreateNewUI()
        {
            var root = new GameObject("Canvas");

            root.layer = LayerMask.NameToLayer("UI");
            Canvas canvas = root.AddComponent <Canvas>();

            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
            root.AddComponent <CanvasScaler>();
            root.AddComponent <GraphicRaycaster>();

            StageUtility.PlaceGameObjectInCurrentStage(root);
            bool        customScene = false;
            PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
                customScene = true;
            }

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

            if (!customScene)
            {
                CreateEventSystem(false);
            }
            return(root);
        }
Ejemplo n.º 6
0
        static void CreateTextMeshProObjectPerform(MenuCommand command)
        {
            GameObject go = new GameObject("Text (TMP)");

            // Add support for new prefab mode
            StageUtility.PlaceGameObjectInCurrentStage(go);

            TextMeshPro textMeshPro = go.AddComponent <TextMeshPro>();

            textMeshPro.text      = "Sample text";
            textMeshPro.alignment = TextAlignmentOptions.TopLeft;



            Undo.RegisterCreatedObjectUndo((Object)go, "Create " + go.name);

            GameObject contextObject = command.context as GameObject;

            if (contextObject != null)
            {
                GameObjectUtility.SetParentAndAlign(go, contextObject);
                Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name);
            }

            Selection.activeGameObject = go;
        }
Ejemplo n.º 7
0
        public static GameObject CreateNewUI()
        {
            // Root for the UI
            var root = new GameObject("Canvas");

            root.layer = LayerMask.NameToLayer(uiLayerName);

            var canvas = root.AddComponent <Canvas>();

            canvas.renderMode = RenderMode.ScreenSpaceOverlay;

            root.AddComponent <CanvasScaler>();
            root.AddComponent <GraphicRaycaster>();
            root.AddComponent <CurvedUIController>();

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            var customScene = prefabStage != null;

            if (customScene)
            {
                root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
            }
            else
            {
                // Create an event system if not in a prefab scene
                CreateEventSystem(false);
            }

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

            return(root);
        }
Ejemplo n.º 8
0
    static GameObject CreateNewUI()
    {
        // Root for the UI
        var root = new GameObject("Canvas");

        root.layer = LayerMask.NameToLayer("UI");
        Canvas canvas = root.AddComponent <Canvas>();

        canvas.renderMode = RenderMode.ScreenSpaceOverlay;
        root.AddComponent <CanvasScaler>();
        root.AddComponent <GraphicRaycaster>();

        // Works for all stages.
        StageUtility.PlaceGameObjectInCurrentStage(root);
        bool        customScene = false;
        PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

        if (prefabStage != null)
        {
            root.transform.SetParent(prefabStage.prefabContentsRoot.transform, false);
            customScene = true;
        }

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

        // If there is no event system add one...
        // No need to place event system in custom scene as these are temporary anyway.
        // It can be argued for or against placing it in the user scenes,
        // but let's not modify scene user is not currently looking at.
        if (!customScene)
        {
            CreateEventSystem(false, null);
        }
        return(root);
    }
Ejemplo n.º 9
0
        private static void CreateEventSystem(bool select, GameObject parent)
        {
            StageHandle stage = parent == null?StageUtility.GetCurrentStageHandle() : StageUtility.GetStageHandle(parent);

            var esys = stage.FindComponentOfType <EventSystem>();

            if (esys == null)
            {
                var eventSystem = ObjectFactory.CreateGameObject("EventSystem");
                if (parent == null)
                {
                    StageUtility.PlaceGameObjectInCurrentStage(eventSystem);
                }
                else
                {
                    SetParentAndAlign(eventSystem, parent);
                }
                esys = ObjectFactory.AddComponent <EventSystem>(eventSystem);
                ObjectFactory.AddComponent <StandaloneInputModule>(eventSystem);

                Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
            }

            if (select && esys != null)
            {
                Selection.activeGameObject = esys.gameObject;
            }
        }
Ejemplo n.º 10
0
        // This is from GOCreationCommands
        internal static void Place(GameObject go, GameObject parent)
        {
            if (parent != null)
            {
                var transform = go.transform;
                Undo.SetTransformParent(transform, parent.transform, "Reparenting");
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.identity;
                transform.localScale    = Vector3.one;
                go.layer = parent.layer;

                if (parent.GetComponent <RectTransform>())
                {
                    ObjectFactory.AddComponent <RectTransform>(go);
                }
            }
            else
            {
                PlaceGameObjectInFrontOfSceneView(go);
                StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
                go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, 0);
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
            Undo.SetCurrentGroupName("Create " + go.name);

            //EditorWindow.FocusWindowIfItsOpen<SceneHierarchyWindow>();
            Selection.activeGameObject = go;
        }
    // UnityEditor.UI.MenuOptions
    public static GameObject CreateNewUI()
    {
        GameObject gameObject = new GameObject("Canvas");

        gameObject.layer = LayerMask.NameToLayer("UI");
        Canvas canvas = gameObject.AddComponent <Canvas>();

        canvas.renderMode = RenderMode.ScreenSpaceOverlay;
        gameObject.AddComponent <CanvasScaler>();
        gameObject.AddComponent <GraphicRaycaster>();
        StageUtility.PlaceGameObjectInCurrentStage(gameObject);
        bool        flag = false;
        PrefabStage currentPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();

        if (currentPrefabStage != null)
        {
            gameObject.transform.SetParent(currentPrefabStage.prefabContentsRoot.transform, false);
            flag = true;
        }
        Undo.RegisterCreatedObjectUndo(gameObject, "Create " + gameObject.name);
        if (!flag)
        {
            SpringGUIMenuOptions.CreateEventSystem(false);
        }
        return(gameObject);
    }
Ejemplo n.º 12
0
        static public GameObject CreateNewUI()
        {
            // Root for the UI
            var root = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));

            root.layer = LayerMask.NameToLayer(kUILayerName);
            Canvas canvas = root.GetComponent <Canvas>();

            canvas.renderMode = RenderMode.ScreenSpaceOverlay;

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);
            bool        customScene = false;
            PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
                customScene = true;
            }

            Undo.SetCurrentGroupName("Create " + root.name);

            // If there is no event system add one...
            // No need to place event system in custom scene as these are temporary anyway.
            // It can be argued for or against placing it in the user scenes,
            // but let's not modify scene user is not currently looking at.
            if (!customScene)
            {
                CreateEventSystem(false);
            }
            return(root);
        }
        public static void CreatePrimitiveEntity(MenuCommand menuCommand)
        {
            GameObject MeshEntity = new GameObject("MeshEntity");

            MeshEntity.AddComponent <MeshComponent>();
            GameObjectUtility.SetParentAndAlign(MeshEntity, menuCommand.context as GameObject);
            StageUtility.PlaceGameObjectInCurrentStage(MeshEntity);
            GameObjectUtility.EnsureUniqueNameForSibling(MeshEntity);
            Undo.RegisterCreatedObjectUndo(MeshEntity, "Create " + MeshEntity.name);
            Selection.activeObject = MeshEntity;
        }
Ejemplo n.º 14
0
        static void CreateTextMeshProObjectPerform(MenuCommand command)
        {
            GameObject go = ObjectFactory.CreateGameObject("Text (TMP)");

            // Add support for new prefab mode
            StageUtility.PlaceGameObjectInCurrentStage(go);

            TextMeshPro textComponent = ObjectFactory.AddComponent <TextMeshPro>(go);

            if (textComponent.m_isWaitingOnResourceLoad == false)
            {
                // Get reference to potential Presets for <TextMeshPro> component
                Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);

                if (presets == null || presets.Length == 0)
                {
                    textComponent.text      = "Sample text";
                    textComponent.alignment = TextAlignmentOptions.TopLeft;
                }
                else
                {
                    textComponent.renderer.sortingLayerID = textComponent._SortingLayerID;
                    textComponent.renderer.sortingOrder   = textComponent._SortingOrder;
                }

                if (TMP_Settings.autoSizeTextContainer)
                {
                    Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
                    textComponent.rectTransform.sizeDelta = size;
                }
                else
                {
                    textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProTextContainerSize;
                }
            }
            else
            {
                textComponent.text      = "Sample text";
                textComponent.alignment = TextAlignmentOptions.TopLeft;
            }

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

            GameObject contextObject = command.context as GameObject;

            if (contextObject != null)
            {
                GameObjectUtility.SetParentAndAlign(go, contextObject);
                Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name);
            }

            Selection.activeGameObject = go;
        }
Ejemplo n.º 15
0
        public static GameObject EmptyGameObjectWithTransform(Transform t)
        {
            GameObject go = new GameObject();

            go.transform.position      = t.position;
            go.transform.localRotation = t.localRotation;
            go.transform.localScale    = t.localScale;

            #if UNITY_EDITOR
            StageUtility.PlaceGameObjectInCurrentStage(go);
            #endif

            return(go);
        }
Ejemplo n.º 16
0
    private static void CreateEventSystem()
    {
        StageHandle stage = StageUtility.GetCurrentStageHandle();
        var         esys  = stage.FindComponentOfType <EventSystem>();

        if (esys == null)
        {
            var eventSystem = ObjectFactory.CreateGameObject("EventSystem");
            StageUtility.PlaceGameObjectInCurrentStage(eventSystem);
            esys = ObjectFactory.AddComponent <EventSystem>(eventSystem);
            ObjectFactory.AddComponent <StandaloneInputModule>(eventSystem);

            Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
        }
    }
Ejemplo n.º 17
0
        internal static void Place(GameObject go, GameObject parent, bool ignoreSceneViewPosition = true)
        {
            Transform defaultObjectTransform = SceneView.GetDefaultParentObjectIfSet();

            if (parent != null)
            {
                // At this point, RecordStructureChange is already ongoing (from the CreatePrimitive call through the CreateAndPlacePrimitive method). We need to flush the stack to finalise the RecordStructureChange before the
                // following SetTransformParent call takes place.
                Undo.FlushTrackedObjects();

                SetGameObjectParent(go, parent.transform);
            }
            else if (defaultObjectTransform != null)
            {
                // At this point, RecordStructureChange is already ongoing (from the CreatePrimitive call through the CreateAndPlacePrimitive method). We need to flush the stack to finalise the RecordStructureChange before the
                // following SetTransformParent call takes place.
                Undo.FlushTrackedObjects();

                SetGameObjectParent(go, defaultObjectTransform);
            }
            else
            {
                // When creating a 3D object without a parent, this option puts it at the world origin instead of scene pivot.
                if (placeObjectsAtWorldOrigin)
                {
                    go.transform.position = Vector3.zero;
                }
                else if (ignoreSceneViewPosition)
                {
                    SceneView.PlaceGameObjectInFrontOfSceneView(go);
                }

                StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
            Undo.SetCurrentGroupName("Create " + go.name);

            var sh = SceneHierarchyWindow.GetSceneHierarchyWindowToFocusForNewGameObjects();

            if (sh != null)
            {
                sh.Focus();
            }

            Selection.activeGameObject = go;
        }
Ejemplo n.º 18
0
        public void SceneSetup(Object selectedObject)
        {
            this.selectedObject = selectedObject;

            sceneObjects = new List <GameObject>();

            GameObject lightingObject = new GameObject("Lighting");

            lightingObject.hideFlags = HideFlags.HideAndDontSave;

            lightingObject.AddComponent <Light>().type = LightType.Directional;
            lightingObject.transform.eulerAngles       = new Vector3(45, 175, 0);
            sceneObjects.Add(lightingObject);

            StageUtility.PlaceGameObjectInCurrentStage(lightingObject);
        }
Ejemplo n.º 19
0
        /*
         * Borrowed from
         * https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Commands/GOCreationCommands.cs#L15
         * */
        private static void PlaceInScene(GameObject go, GameObject parent)
        {
            if (parent != null)
            {
                Transform transform = go.transform;
                Undo.SetTransformParent(transform, parent.transform, "Reparenting");
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.identity;
                transform.localScale    = Vector3.one;
            }
            else
            {
                PlaceGameObjectInFrontOfSceneView(go);
#if UNITY_2018_3_OR_LATER
                StageUtility.PlaceGameObjectInCurrentStage(go);
#endif
            }

            Selection.activeGameObject = go;
        }
Ejemplo n.º 20
0
        public static void AddPanel(MenuCommand menuCommand)
        {
            GameObject parent = menuCommand.context as GameObject;
            var        root   = ObjectFactory.CreateGameObject("Panel", typeof(PanelRenderer), typeof(UIElementsEventSystem));

            root.layer = LayerMask.NameToLayer(kUILayerName);

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);
            PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
            }

            Undo.SetCurrentGroupName("Create " + root.name);

            SetParentAndAlign(root, parent);
            Selection.activeGameObject = root;
        }
Ejemplo n.º 21
0
    static public GameObject CreateCanvas()
    {
        var root = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));

        root.layer = LayerMask.NameToLayer("UI");
        var canvas = root.GetComponent <Canvas>();

        canvas.renderMode = RenderMode.ScreenSpaceOverlay;

        // Works for all stages
        StageUtility.PlaceGameObjectInCurrentStage(root);
        PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

        if (prefabStage != null)
        {
            Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
        }

        Undo.SetCurrentGroupName("Create " + root.name);

        return(root);
    }
Ejemplo n.º 22
0
        internal static void Place(GameObject go, GameObject parent)
        {
            if (parent != null)
            {
                var transform = go.transform;
                Undo.SetTransformParent(transform, parent.transform, "Reparenting");
                transform.localPosition = Vector3.zero;
                transform.localRotation = Quaternion.identity;
                transform.localScale    = Vector3.one;
                go.layer = parent.layer;

                if (parent.GetComponent <RectTransform>())
                {
                    ObjectFactory.AddComponent <RectTransform>(go);
                }
            }
            else
            {
                // When creating a 3D object without a parent, this option puts it at the world origin instead of scene pivot.
                if (EditorPrefs.GetBool("Create3DObject.PlaceAtWorldOrigin", false))
                {
                    go.transform.position = Vector3.zero;
                }
                else
                {
                    SceneView.PlaceGameObjectInFrontOfSceneView(go);
                }

                StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
            Undo.SetCurrentGroupName("Create " + go.name);

            EditorWindow.FocusWindowIfItsOpen <SceneHierarchyWindow>();
            Selection.activeGameObject = go;
        }
Ejemplo n.º 23
0
        /// <summary>
        /// <see cref="https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/Inspector/AvatarPreview.cs#L326"/>
        /// </summary>
        public SkeletonPoseView(Transform spawnTransform, List <nuitrack.JointType> jointsMask, ColorTheme colorTheme = null)
        {
            this.jointsMask = jointsMask;
            this.colorTheme = colorTheme ?? this.colorTheme;

            GameObject dudeObject = (GameObject)EditorGUIUtility.Load("Avatar/DefaultAvatar.fbx");

            dude           = Object.Instantiate(dudeObject, spawnTransform);
            dude.hideFlags = HideFlags.HideAndDontSave;

            StageUtility.PlaceGameObjectInCurrentStage(dude);

            animator         = dude.GetComponent <Animator>();
            animator.enabled = false;

            rotationsOffset = new Dictionary <nuitrack.JointType, Quaternion>();

            foreach (nuitrack.JointType jointType in jointsMask)
            {
                Transform jointTransform = animator.GetBoneTransform(jointType.ToUnityBones());
                rotationsOffset.Add(jointType, jointTransform.rotation);
            }
        }
    private static void CreateEventSystem(bool select, GameObject parent)
    {
        EventSystem eventSystem = ((!(parent == null)) ? StageUtility.GetStageHandle(parent) : StageUtility.GetCurrentStageHandle()).FindComponentOfType <EventSystem>();

        if (eventSystem == null)
        {
            GameObject gameObject = new GameObject("EventSystem");
            if (parent == null)
            {
                StageUtility.PlaceGameObjectInCurrentStage(gameObject);
            }
            else
            {
                GameObjectUtility.SetParentAndAlign(gameObject, parent);
            }
            eventSystem = gameObject.AddComponent <EventSystem>();
            gameObject.AddComponent <StandaloneInputModule>();
            Undo.RegisterCreatedObjectUndo(gameObject, "Create " + gameObject.name);
        }
        if (select && eventSystem != null)
        {
            Selection.activeGameObject = eventSystem.gameObject;
        }
    }
        static void Place(GameObject go, Transform parent)
        {
            var transform = go.transform;

            if (parent != null)
            {
                // Must call RecordObject and reset values before SetTransformParent to ensure the
                // Transform values are correct upon undo in the case that it's a root object.
                // Undo.SetTransformParent did not have parameter worldPositionStays to be able
                // to set false until 2020.2.0a17 (fb# 1247086).
                Undo.RecordObject(transform, "Reset Transform");
                ResetTransform(transform);
                Undo.SetTransformParent(transform, parent, "Reparenting");
                ResetTransform(transform);
                Undo.RegisterCompleteObjectUndo(go, "Change Layer");
                go.layer = parent.gameObject.layer;
            }
            else
            {
                // Puts it at the scene pivot, and otherwise world origin if there is no Scene view.
                var view = SceneView.lastActiveSceneView;
                if (view != null)
                {
                    view.MoveToView(transform);
                }
                else
                {
                    transform.position = Vector3.zero;
                }

                StageUtility.PlaceGameObjectInCurrentStage(go);
            }

            // Only at this point do we know the actual parent of the object and can modify its name accordingly.
            GameObjectUtility.EnsureUniqueNameForSibling(go);
        }
Ejemplo n.º 26
0
        public static GameObject CreateNewSolver()
        {
            // Root for the actors.
            var       root   = new GameObject("Obi Solver");
            ObiSolver solver = root.AddComponent <ObiSolver>();

            // Try to find a fixed updater in the scene (though other kinds of updaters can exist, updating in FixedUpdate is the preferred option).
            ObiFixedUpdater updater = StageUtility.GetCurrentStageHandle().FindComponentOfType <ObiFixedUpdater>();

            // If we could not find an fixed updater in the scene, add one to the solver object.
            if (updater == null)
            {
                updater = root.AddComponent <ObiFixedUpdater>();
            }

            // Add the solver to the updater:
            updater.solvers.Add(solver);

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);
            Undo.RegisterCreatedObjectUndo(root, "Create " + root.name);

            return(root);
        }
Ejemplo n.º 27
0
        public static void AddUIDocument(MenuCommand menuCommand)
        {
            GameObject parent     = menuCommand.context as GameObject;
            Type       type       = typeof(UIDocument);
            var        root       = ObjectFactory.CreateGameObject(type.Name, type);
            UIDocument uiDocument = root.GetComponent <UIDocument>();

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);
            PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
            }

            Undo.SetCurrentGroupName("Create " + root.name);

            if (parent != null)
            {
                SetParentAndAlign(root, parent);
                uiDocument.ReactToHierarchyChanged();
            }
            else
            {
                root.layer = LayerMask.NameToLayer(k_UILayerName);
            }

            Selection.activeGameObject = root;


            // Set a PanelSettings instance so that the UI appears immediately on selecting the UXML.
            // If the UIDocument was created as a child of another UIDocument, this step is not necessary.
            if (uiDocument.parentUI == null)
            {
                var panelSettingsInProject = AssetDatabase.FindAssets(k_AssetSearchByTypePanelSettings, k_AssetsFolderFilter);
                if (panelSettingsInProject != null && panelSettingsInProject.Length > 0)
                {
                    // Use the first one found.
                    PanelSettings panelSettings =
                        AssetDatabase.LoadAssetAtPath <PanelSettings>(
                            AssetDatabase.GUIDToAssetPath(panelSettingsInProject[0]));
                    uiDocument.panelSettings = panelSettings;
                }
                else
                {
                    // Create one.
                    PanelSettings panelSettings = ScriptableObject.CreateInstance <PanelSettings>();

                    if (!AssetDatabase.IsValidFolder(k_UITKEssentialResourcesFolderPath))
                    {
                        AssetDatabase.CreateFolder(k_AssetsFolder, k_UITKEssentialResourcesFolderName);
                    }

                    AssetDatabase.CreateAsset(panelSettings, k_PanelSettingsAssetPath);
                    panelSettingsInProject = AssetDatabase.FindAssets(k_AssetSearchByTypePanelSettings, k_AssetsFolderFilter);

                    // We just created the asset, it MUST exist so if it doesn't there's something wrong.
                    Debug.Assert(panelSettingsInProject != null && panelSettingsInProject.Length > 0,
                                 "PanelSettings asset not found for assigning to created UIDocument");

                    panelSettings =
                        AssetDatabase.LoadAssetAtPath <PanelSettings>(
                            AssetDatabase.GUIDToAssetPath(panelSettingsInProject[0]));
                    uiDocument.panelSettings = panelSettings;
                }
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Place the given <paramref name="gameObject"/> in the current stage,
        /// normally the scene but may be a prefab being edited. If the prefab
        /// stage is open, the <paramref name="gameObject"/> will be added
        /// under the root prefab.
        /// </summary>
        /// <param name="gameObject"></param>
        public static void PlaceInCurrentStange(GameObject gameObject)
        {
#if UNITY_EDITOR
            StageUtility.PlaceGameObjectInCurrentStage(gameObject);
#endif
        }