示例#1
0
        internal void Initialize()
        {
            if (UpdateInputManager != null)
            {
                UpdateInputManager();
            }

#if UNITY_EDITOR
            DrivenRectTransformTracker.StopRecordingUndo();

            if (!Application.isPlaying)
            {
                ClearDeveloperConsoleIfNecessary();
            }
#endif

            HandleInitialization();

            UnityBrandColorScheme.sessionGradient          = UnityBrandColorScheme.GetRandomCuratedLightGradient();
            UnityBrandColorScheme.saturatedSessionGradient = UnityBrandColorScheme.GetRandomCuratedGradient();

            var delayedInitializationModules = new List <IDelayedInitializationModule>();
            foreach (var module in ModuleLoaderCore.instance.modules)
            {
                var delayedInitializationModule = module as IDelayedInitializationModule;
                if (delayedInitializationModule != null)
                {
                    delayedInitializationModules.Add(delayedInitializationModule);
                }
            }

            delayedInitializationModules.Sort((a, b) => a.initializationOrder.CompareTo(b.initializationOrder));

            foreach (var module in delayedInitializationModules)
            {
                module.Initialize();
            }
        }
        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)
                {
                    // While dragging the instantiated prefab we do not want to record undo for this object
                    // this will cause a remerge of the instance since changes are undone while dragging.
                    // The DrivenRectTransformTracker by default records Undo when used when driving
                    // UI components. This breaks our hideflag setup below due to a remerge of the dragged instance.
                    // StartRecordingUndo() is called on DragExited. Fixes case 1223793.
                    DrivenRectTransformTracker.StopRecordingUndo();

                    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:
                if (!DragPerform(sceneView, dragObject, go))
                {
                    return;
                }
                dragObject = null;
                evt.Use();
                break;

            case EventType.DragExited:
                // DragExited is always fired after DragPerform so we do no need to call StartRecordingUndo
                // in DragPerform
                DrivenRectTransformTracker.StartRecordingUndo();

                if (dragObject)
                {
                    UnityObject.DestroyImmediate(dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    dragObject = null;
                    evt.Use();
                }
                break;
            }
        }
示例#3
0
        void Initialize()
        {
#if UNITY_EDITOR
#if UNITY_2018_2_OR_NEWER
            DrivenRectTransformTracker.StopRecordingUndo();
#elif UNDO_PATCH
            DrivenRectTransformTracker.BlockUndo = true;
#endif
#endif
            Nested.evr     = this; // Set this once for the convenience of all nested classes
            m_DefaultTools = DefaultTools;
            SetHideFlags(defaultHideFlags);
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                ClearDeveloperConsoleIfNecessary();
            }
#endif
            HandleInitialization();

            m_Interfaces = (Interfaces)AddNestedModule(typeof(Interfaces));
            AddModule <SerializedPreferencesModule>(); // Added here in case any nested modules have preference serialization
            AddNestedModule(typeof(SerializedPreferencesModuleConnector));

            var nestedClassTypes = ObjectUtils.GetExtensionsOfClass(typeof(Nested));
            foreach (var type in nestedClassTypes)
            {
                AddNestedModule(type);
            }

            LateBindNestedModules(nestedClassTypes);

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                AddModule <HierarchyModule>();
                AddModule <ProjectFolderModule>();
            }
#endif

            AddModule <AdaptivePositionModule>();

            var viewer = GetNestedModule <Viewer>();
            viewer.preserveCameraRig = preserveLayout;
            viewer.InitializeCamera();

            var deviceInputModule = AddModule <DeviceInputModule>();
            deviceInputModule.InitializePlayerHandle();
            deviceInputModule.CreateDefaultActionMapInputs();
            deviceInputModule.processInput            = ProcessInput;
            deviceInputModule.updatePlayerHandleMaps  = Tools.UpdatePlayerHandleMaps;
            deviceInputModule.inputDeviceForRayOrigin = rayOrigin =>
                                                        (from deviceData in m_DeviceData
                                                         where deviceData.rayOrigin == rayOrigin
                                                         select deviceData.inputDevice).FirstOrDefault();

            GetNestedModule <UI>().Initialize();

            AddModule <KeyboardModule>();

            var multipleRayInputModule = GetModule <MultipleRayInputModule>();

            var dragAndDropModule = AddModule <DragAndDropModule>();
            multipleRayInputModule.rayEntered  += dragAndDropModule.OnRayEntered;
            multipleRayInputModule.rayExited   += dragAndDropModule.OnRayExited;
            multipleRayInputModule.dragStarted += dragAndDropModule.OnDragStarted;
            multipleRayInputModule.dragEnded   += dragAndDropModule.OnDragEnded;

            var tooltipModule = AddModule <TooltipModule>();
            this.ConnectInterfaces(tooltipModule);
            multipleRayInputModule.rayEntered  += tooltipModule.OnRayEntered;
            multipleRayInputModule.rayHovering += tooltipModule.OnRayHovering;
            multipleRayInputModule.rayExited   += tooltipModule.OnRayExited;

            AddModule <ActionsModule>();
            AddModule <HighlightModule>();

            var lockModule = AddModule <LockModule>();
            lockModule.updateAlternateMenu = (rayOrigin, o) => Menus.SetAlternateMenuVisibility(rayOrigin, o != null);

            AddModule <SelectionModule>();

            var spatialHashModule = AddModule <SpatialHashModule>();
            spatialHashModule.shouldExcludeObject = go => go.GetComponentInParent <EditorVR>();
            spatialHashModule.Setup();

            var intersectionModule = AddModule <IntersectionModule>();
            this.ConnectInterfaces(intersectionModule);
            intersectionModule.Setup(spatialHashModule.spatialHash);

            // TODO: Support module dependencies via ConnectInterfaces
            GetNestedModule <Rays>().ignoreList = intersectionModule.standardIgnoreList;

            AddModule <SnappingModule>();

            var vacuumables = GetNestedModule <Vacuumables>();

            var miniWorlds      = GetNestedModule <MiniWorlds>();
            var workspaceModule = AddModule <WorkspaceModule>();
            workspaceModule.preserveWorkspaces  = preserveLayout;
            workspaceModule.HiddenTypes         = HiddenTypes;
            workspaceModule.workspaceCreated   += vacuumables.OnWorkspaceCreated;
            workspaceModule.workspaceCreated   += miniWorlds.OnWorkspaceCreated;
            workspaceModule.workspaceCreated   += workspace => { deviceInputModule.UpdatePlayerHandleMaps(); };
            workspaceModule.workspaceDestroyed += vacuumables.OnWorkspaceDestroyed;
            workspaceModule.workspaceDestroyed += miniWorlds.OnWorkspaceDestroyed;

            UnityBrandColorScheme.sessionGradient          = UnityBrandColorScheme.GetRandomCuratedLightGradient();
            UnityBrandColorScheme.saturatedSessionGradient = UnityBrandColorScheme.GetRandomCuratedGradient();

            var sceneObjectModule = AddModule <SceneObjectModule>();
            sceneObjectModule.tryPlaceObject = (obj, targetScale) =>
            {
                foreach (var miniWorld in miniWorlds.worlds)
                {
                    if (!miniWorld.Contains(obj.position))
                    {
                        continue;
                    }

                    var referenceTransform = miniWorld.referenceTransform;
                    obj.transform.parent = null;
                    obj.position         = referenceTransform.position + Vector3.Scale(miniWorld.miniWorldTransform.InverseTransformPoint(obj.position), miniWorld.referenceTransform.localScale);
                    obj.rotation         = referenceTransform.rotation * Quaternion.Inverse(miniWorld.miniWorldTransform.rotation) * obj.rotation;
                    obj.localScale       = Vector3.Scale(Vector3.Scale(obj.localScale, referenceTransform.localScale), miniWorld.miniWorldTransform.lossyScale.Inverse());

                    spatialHashModule.AddObject(obj.gameObject);
                    return(true);
                }

                return(false);
            };

            AddModule <HapticsModule>();
            AddModule <GazeDivergenceModule>();
            AddModule <SpatialHintModule>();
            AddModule <SpatialScrollModule>();

            AddModule <FeedbackModule>();

            AddModule <WebModule>();

            //TODO: External module support (removes need for CCU in this instance)
#if INCLUDE_POLY_TOOLKIT
            AddModule <PolyModule>();
#endif

            viewer.AddPlayerModel();
            viewer.AddPlayerFloor();
            GetNestedModule <Rays>().CreateAllProxies();

            // In case we have anything selected at start, set up manipulators, inspector, etc.
            EditorApplication.delayCall += OnSelectionChanged;
        }
        internal void OnSceneDragInternal(SceneView sceneView, int index, EventType type, Vector2 mousePosition, bool alt)
        {
            GameObject go = target as GameObject;

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

            var prefabAssetRoot = go.transform.root.gameObject;

            switch (type)
            {
            case EventType.DragUpdated:
                Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene();
                if (m_DragObject == null)
                {
                    // While dragging the instantiated prefab we do not want to record undo for this object
                    // this will cause a remerge of the instance since changes are undone while dragging.
                    // The DrivenRectTransformTracker by default records Undo when used when driving
                    // UI components. This breaks our hideflag setup below due to a remerge of the dragged instance.
                    // StartRecordingUndo() is called on DragExited. Fixes case 1223793.
                    DrivenRectTransformTracker.StopRecordingUndo();

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

                    if (HandleUtility.ignoreRaySnapObjects == null)
                    {
                        HandleUtility.ignoreRaySnapObjects = m_DragObject.GetComponentsInChildren <Transform>();
                    }
                    else
                    {
                        HandleUtility.ignoreRaySnapObjects = HandleUtility.ignoreRaySnapObjects.Union(m_DragObject.GetComponentsInChildren <Transform>()).ToArray();
                    }

                    PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                    if (prefabStage != null)
                    {
                        GameObject prefab = AssetDatabase.LoadMainAssetAtPath(prefabStage.assetPath) as GameObject;

                        if (prefab != null)
                        {
                            if (PrefabUtility.CheckIfAddingPrefabWouldResultInCyclicNesting(prefab, target))
                            {
                                s_CyclicNestingDetected = true;
                            }
                        }
                    }
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Vector3 point, normal;
                float   offset = 0;

                if (index == 0)
                {
                    s_PlaceObject = HandleUtility.PlaceObject(mousePosition, out s_PlaceObjectPoint, out s_PlaceObjectNormal);
                }

                point  = s_PlaceObjectPoint;
                normal = s_PlaceObjectNormal;

                if (s_PlaceObject)
                {
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float geomOffset = HandleUtility.CalcRayPlaceOffset(m_DragObject.GetComponentsInChildren <Transform>(), normal);
                        if (geomOffset != Mathf.Infinity)
                        {
                            offset = Vector3.Dot(m_DragObject.transform.position, normal) - geomOffset;
                        }
                    }
                    m_DragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset));
                }
                else
                {
                    m_DragObject.transform.position = HandleUtility.GUIPointToWorldRay(mousePosition).GetPoint(10);
                }

                if (alt)
                {
                    if (offset != 0)
                    {
                        m_DragObject.transform.position = point;
                    }
                    m_DragObject.transform.position += prefabAssetRoot.transform.localPosition;
                }

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

                // Schedule selection clearing for when we start performing the actual drag action
                s_ShouldClearSelection = true;
                break;

            case EventType.DragPerform:
                DragPerform(sceneView, m_DragObject, go);
                m_DragObject = null;
                break;

            case EventType.DragExited:
                // DragExited is always fired after DragPerform so we do no need to call StartRecordingUndo
                // in DragPerform
                DrivenRectTransformTracker.StartRecordingUndo();

                if (m_DragObject)
                {
                    DestroyImmediate(m_DragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    m_DragObject = null;
                }
                s_ShouldClearSelection  = false;
                s_CyclicNestingDetected = false;
                break;
            }
        }