Esempio n. 1
0
 public static bool SetOverrideLightingSettings(Scene scene)
 {
     return(SetOverrideLightingSettingsInternal(scene.handle));
 }
Esempio n. 2
0
        private DragAndDropVisualMode DoDragScenes(GameObjectTreeViewItem parentItem, GameObjectTreeViewItem targetItem, bool perform, DropPosition dropPos)
        {
            // We allow dragging SceneAssets on any game object in the Hierarchy to make it easy to drag in a Scene from
            // the project browser. If dragging on a game object (not a sceneheader) we place the dropped scene
            // below the game object's scene

            // Case: 1
            List <Scene> scenes = DragAndDrop.GetGenericData(kSceneHeaderDragString) as List <Scene>;
            bool         reorderExistingScenes = (scenes != null);

            // Case: 2
            bool insertNewScenes = false;

            if (!reorderExistingScenes && DragAndDrop.objectReferences.Length > 0)
            {
                int sceneAssetCount = 0;
                foreach (var dragged in DragAndDrop.objectReferences)
                {
                    if (dragged is SceneAsset)
                    {
                        sceneAssetCount++;
                    }
                }
                insertNewScenes = (sceneAssetCount == DragAndDrop.objectReferences.Length);
            }

            // Early out if not case 1 or 2
            if (!reorderExistingScenes && !insertNewScenes)
            {
                return(DragAndDropVisualMode.None);
            }

            if (perform)
            {
                List <Scene> scenesToBeMoved = null;
                if (insertNewScenes)
                {
                    List <Scene> insertedScenes = new List <Scene>();
                    foreach (var sceneAsset in DragAndDrop.objectReferences)
                    {
                        string scenePath = AssetDatabase.GetAssetPath(sceneAsset);
                        Scene  scene     = SceneManager.GetSceneByPath(scenePath);
                        if (SceneHierarchy.IsSceneHeaderInHierarchyWindow(scene))
                        {
                            m_TreeView.Frame(scene.handle, true, true);
                        }
                        else
                        {
                            bool unloaded = Event.current.alt;
                            if (unloaded)
                            {
                                scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.AdditiveWithoutLoading);
                            }
                            else
                            {
                                scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
                            }

                            if (SceneHierarchy.IsSceneHeaderInHierarchyWindow(scene))
                            {
                                insertedScenes.Add(scene);
                            }
                        }
                    }
                    if (targetItem != null)
                    {
                        scenesToBeMoved = insertedScenes;
                    }

                    // Select added scenes and frame last scene
                    if (insertedScenes.Count > 0)
                    {
                        Selection.instanceIDs = insertedScenes.Select(x => x.handle).ToArray();
                        m_TreeView.Frame(insertedScenes.Last().handle, true, false);
                    }
                }
                else // reorderExistingScenes
                {
                    scenesToBeMoved = scenes;
                }

                if (scenesToBeMoved != null)
                {
                    if (targetItem != null)
                    {
                        Scene dstScene = targetItem.scene;
                        if (SceneHierarchy.IsSceneHeaderInHierarchyWindow(dstScene))
                        {
                            if (!targetItem.isSceneHeader || dropPos == DropPosition.Upon)
                            {
                                dropPos = DropPosition.Below;
                            }

                            if (dropPos == DropPosition.Above)
                            {
                                for (int i = 0; i < scenesToBeMoved.Count; i++)
                                {
                                    EditorSceneManager.MoveSceneBefore(scenesToBeMoved[i], dstScene);
                                }
                            }
                            else if (dropPos == DropPosition.Below)
                            {
                                for (int i = scenesToBeMoved.Count - 1; i >= 0; i--)
                                {
                                    EditorSceneManager.MoveSceneAfter(scenesToBeMoved[i], dstScene);
                                }
                            }
                        }
                    }
                    else
                    {
                        Scene dstScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
                        for (int i = scenesToBeMoved.Count - 1; i >= 0; i--)
                        {
                            EditorSceneManager.MoveSceneAfter(scenesToBeMoved[i], dstScene);
                        }
                    }
                }
            }

            return(DragAndDropVisualMode.Move);
        }
        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject go = target as GameObject;

            if (!PrefabUtility.IsPartOfPrefabAsset(go))
            {
                return;
            }

            var prefabAssetRoot = go.transform.root.gameObject;

            Event evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:

                Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene();
                if (dragObject == null)
                {
                    if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene))
                    {
                        dragObject      = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene);
                        dragObject.name = go.name;
                    }
                    else
                    {
                        // Instatiate as regular GameObject in Play Mode so runtime logic
                        // won't run into restrictions on restructuring Prefab instances.
                        dragObject = Instantiate(prefabAssetRoot);
                        SceneManager.MoveGameObjectToScene(dragObject, destinationScene);
                    }
                    dragObject.hideFlags = HideFlags.HideInHierarchy;
                }

                if (HandleUtility.ignoreRaySnapObjects == null)
                {
                    HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>();
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Vector3 point, normal;

                if (HandleUtility.PlaceObject(evt.mousePosition, out point, out normal))
                {
                    float offset = 0;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, normal);
                        if (geomOffset != Mathf.Infinity)
                        {
                            offset = Vector3.Dot(dragObject.transform.position, normal) - geomOffset;
                        }
                    }
                    dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset));
                }
                else
                {
                    dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10);
                }

                // Use prefabs original z position when in 2D mode
                if (sceneView.in2DMode)
                {
                    Vector3 dragPosition = dragObject.transform.position;
                    dragPosition.z = prefabAssetRoot.transform.position.z;
                    dragObject.transform.position = dragPosition;
                }

                evt.Use();
                break;

            case EventType.DragPerform:

                var stage = StageNavigationManager.instance.currentStage;
                if (stage is PrefabStage)
                {
                    var prefabAssetThatIsAddedTo = AssetDatabase.LoadMainAssetAtPath(stage.assetPath);
                    if (PrefabUtility.CheckIfAddingPrefabWouldResultInCyclicNesting(prefabAssetThatIsAddedTo, go))
                    {
                        PrefabUtility.ShowCyclicNestingWarningDialog();
                        return;
                    }
                }

                Transform parent = sceneView.customParentForDraggedObjects;

                string uniqueName = GameObjectUtility.GetUniqueNameForSibling(parent, dragObject.name);
                if (parent != null)
                {
                    dragObject.transform.parent = parent;
                }
                dragObject.hideFlags = 0;
                Undo.RegisterCreatedObjectUndo(dragObject, "Place " + dragObject.name);
                EditorUtility.SetDirty(dragObject);
                DragAndDrop.AcceptDrag();
                Selection.activeObject             = dragObject;
                HandleUtility.ignoreRaySnapObjects = null;
                if (SceneView.mouseOverWindow != null)
                {
                    SceneView.mouseOverWindow.Focus();
                }
                if (!Application.IsPlaying(dragObject))
                {
                    dragObject.name = uniqueName;
                }
                dragObject = null;
                evt.Use();
                break;

            case EventType.DragExited:
                if (dragObject)
                {
                    UnityObject.DestroyImmediate(dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    dragObject = null;
                    evt.Use();
                }
                break;
            }
        }
Esempio n. 4
0
        public override DragAndDropVisualMode DoDrag(TreeViewItem parentItem, TreeViewItem targetItem, bool perform, DropPosition dropPos)
        {
            var hierarchyTargetItem = targetItem as GameObjectTreeViewItem;

            // Allow client to handle drag
            if (m_CustomDragHandling != null)
            {
                DragAndDropVisualMode dragResult = m_CustomDragHandling(parentItem as GameObjectTreeViewItem, hierarchyTargetItem, dropPos, perform);
                if (dragResult != DragAndDropVisualMode.None)
                {
                    return(dragResult);
                }
            }

            // Scene dragging logic
            DragAndDropVisualMode dragSceneResult = DoDragScenes(parentItem as GameObjectTreeViewItem, hierarchyTargetItem, perform, dropPos);

            if (dragSceneResult != DragAndDropVisualMode.None)
            {
                return(dragSceneResult);
            }

            if (targetItem != null && !IsDropTargetUserModifiable(hierarchyTargetItem, dropPos))
            {
                return(DragAndDropVisualMode.Rejected);
            }

            var option       = InternalEditorUtility.HierarchyDropMode.kHierarchyDragNormal;
            var searchActive = !string.IsNullOrEmpty(dataSource.searchString);

            if (searchActive)
            {
                option |= InternalEditorUtility.HierarchyDropMode.kHierarchySearchActive;
            }
            if (parentItem == null || targetItem == null)
            {
                // Here we are dragging outside any treeview items:

                if (parentForDraggedObjectsOutsideItems != null)
                {
                    // Use specific parent for DragAndDropForwarding
                    return(DragAndDropService.Drop(DragAndDropService.kHierarchyDropDstId, 0, option, parentForDraggedObjectsOutsideItems, perform));
                }
                else
                {
                    // Simulate drag upon the last loaded scene in the hierarchy (adds as last root sibling of the last scene)
                    Scene lastScene = dataSource.GetLastScene();
                    if (!lastScene.IsValid())
                    {
                        return(DragAndDropVisualMode.Rejected);
                    }

                    option |= InternalEditorUtility.HierarchyDropMode.kHierarchyDropUpon;
                    return(DragAndDropService.Drop(DragAndDropService.kHierarchyDropDstId, lastScene.handle, option, null, perform));
                }
            }

            // Here we are hovering over items

            var draggingUpon = dropPos == TreeViewDragging.DropPosition.Upon;

            if (searchActive && !draggingUpon)
            {
                return(DragAndDropVisualMode.None);
            }

            if (draggingUpon)
            {
                option |= InternalEditorUtility.HierarchyDropMode.kHierarchyDropUpon;
            }
            else
            {
                if (dropPos == TreeViewDragging.DropPosition.Above)
                {
                    option |= InternalEditorUtility.HierarchyDropMode.kHierarchyDropAbove;
                }
                else
                {
                    option |= InternalEditorUtility.HierarchyDropMode.kHierarchyDropBetween;
                }
            }

            bool isDroppingBetweenParentAndFirstChild = parentItem != null && targetItem != parentItem && dropPos == DropPosition.Above && parentItem.children[0] == targetItem;

            if (isDroppingBetweenParentAndFirstChild)
            {
                option |= InternalEditorUtility.HierarchyDropMode.kHierarchyDropAfterParent;
            }

            int gameObjectOrSceneInstanceID = GetDropTargetInstanceID(hierarchyTargetItem, dropPos);

            if (gameObjectOrSceneInstanceID == 0)
            {
                return(DragAndDropVisualMode.Rejected);
            }

            if (perform && SubSceneGUI.IsUsingSubScenes() && !IsValidSubSceneDropTarget(gameObjectOrSceneInstanceID, dropPos, DragAndDrop.objectReferences))
            {
                return(DragAndDropVisualMode.Rejected);
            }

            return(DragAndDropService.Drop(DragAndDropService.kHierarchyDropDstId, gameObjectOrSceneInstanceID, option, null, perform));
        }