Exemple #1
0
        /// <summary>
        /// Move a GameObject to the proper active root.
        /// Checks if a default parent exists, otherwise it adds the object as a root of the active scene, which can be a prefab stage.
        /// </summary>
        /// <param name="gameObject"></param>
        internal static void MoveToActiveRoot(GameObject gameObject)
        {
#if UNITY_2020_2_OR_NEWER
            var parent = SceneView.GetDefaultParentObjectIfSet();
            if (parent != null)
            {
                gameObject.transform.SetParent(parent);
                return;
            }
#endif
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
            var activeScene = SceneManager.GetActiveScene();

            if (prefabStage != null)
            {
                if (gameObject.scene != prefabStage.scene)
                {
                    SceneManager.MoveGameObjectToScene(gameObject, prefabStage.scene);

                    // Prefabs cannot have multiple roots
                    gameObject.transform.SetParent(prefabStage.prefabContentsRoot.transform, true);
                }
            }
            else if (gameObject.scene != activeScene)
            {
                gameObject.transform.SetParent(null);
                SceneManager.MoveGameObjectToScene(gameObject, activeScene);
            }
        }
Exemple #2
0
        private void OnSceneGUI()
        {
            if (!script)
            {
                return;
            }

            FloatingTransform.Disable = PrefabStageUtility.GetCurrentPrefabStage() != null || editSamples;

            if (editSamples)
            {
                //Mute default controls
                Tools.hidden = true;

                Handles.color = new Color(0.66f, 0.66f, 0.66f, 1);

                for (int i = 0; i < script.samples.Count; i++)
                {
                    sampleWorldPos = script.ConvertToWorldSpace(script.samples[i]);

                    float size = HandleUtility.GetHandleSize(sampleWorldPos) * 0.25f;
                    if (Handles.Button(sampleWorldPos, Quaternion.identity, size, size, Handles.SphereHandleCap))
                    {
                        selectedSampleIndex = i;
                    }
                }

                if (selectedSampleIndex > -1)
                {
                    sampleWorldPos     = script.ConvertToWorldSpace(script.samples[selectedSampleIndex]);
                    prevSampleWorldPos = sampleWorldPos;

                    sampleWorldPos = Handles.PositionHandle(sampleWorldPos, script.childTransform ? script.childTransform.rotation : script.transform.rotation);
                    script.samples[selectedSampleIndex] = script.ConvertToLocalSpace(sampleWorldPos);

                    //If moved
                    if (sampleWorldPos != prevSampleWorldPos)
                    {
                        prevSampleWorldPos = sampleWorldPos;
                        EditorUtility.SetDirty(target);
                    }
                }
            }
            else
            {
                selectedSampleIndex = -1;
                Tools.hidden        = false;

                if (script.samples == null)
                {
                    return;
                }

                Handles.color = new Color(1, 1, 1, 0.25f);
                for (int i = 0; i < script.samples.Count; i++)
                {
                    sampleWorldPos = script.ConvertToWorldSpace(script.samples[i]);
                    Handles.SphereHandleCap(0, sampleWorldPos, SceneView.lastActiveSceneView.camera.transform.rotation, HandleUtility.GetHandleSize(sampleWorldPos) * 0.25f, EventType.Repaint);
                }
            }
        }