示例#1
0
        bool CollectCustomEditorToolsFromTracker(ActiveEditorTracker tracker, List <EditorTool> list)
        {
            list.Clear();
            var activeIsCustomEditorTool = IsCustomEditorTool(m_ActiveTool);
            var preservedActiveTool      = false;

            EditorToolUtility.GetEditorToolsForTracker(tracker, s_CustomEditorTools);

            foreach (var customEditorTool in s_CustomEditorTools)
            {
                var toolType  = customEditorTool.editorToolType;
                var toolOwner = customEditorTool.owner;

                EditorTool customEditorToolInstance;

                // The only case where a custom editor tool is serialized is when it is the active tool. All other
                // instances are discarded and rebuilt on any tracker rebuild.
                if (activeIsCustomEditorTool && CustomEditorToolIsMatch(toolOwner, toolType, m_ActiveTool))
                {
                    preservedActiveTool = true;

                    m_ActiveTool.m_Targets = toolOwner.targets;
                    m_ActiveTool.m_Target  = toolOwner.target;

                    // domain reload - the owning editor was destroyed and therefore we need to reset the EditMode active
                    if (m_ActiveTool is EditModeTool && toolOwner.GetInstanceID() != UnityEditorInternal.EditMode.ownerID)
                    {
                        UnityEditorInternal.EditMode.EditModeToolStateChanged(toolOwner, ((EditModeTool)m_ActiveTool).editMode);
                    }

                    customEditorToolInstance = m_ActiveTool;
                }
                else
                {
                    customEditorToolInstance = (EditorTool)CreateInstance(toolType, x =>
                    {
                        ((EditorTool)x).m_Targets = toolOwner.targets;
                        ((EditorTool)x).m_Target  = toolOwner.target;
                    });

                    customEditorToolInstance.hideFlags = HideFlags.DontSave;
                }

                list.Add(customEditorToolInstance);

                var editModeTool = customEditorToolInstance as EditModeTool;

                if (editModeTool != null)
                {
                    editModeTool.owner = toolOwner;
                }
            }

            return(preservedActiveTool);
        }
        void RebuildAvailableCustomEditorTools()
        {
            EditorApplication.delayCall -= RebuildAvailableCustomEditorTools;

            // Do not rebuild the cache since objects are serialized, destroyed, deserialized during this phase
            if (m_PlayModeState == PlayModeStateChange.ExitingEditMode ||
                m_PlayModeState == PlayModeStateChange.ExitingPlayMode)
            {
                return;
            }

            var preservedActiveTool = false;

            ClearCustomEditorTools();

            var inspectors = InspectorWindow.GetInspectors();

            // If the shared tracker is locked, use our own tracker instance so that the current selection is always
            // represented. Addresses case where a single locked inspector is open.
            var shared = ActiveEditorTracker.sharedTracker;

            m_CustomEditorTools.Clear();
            m_LockedCustomEditorTools.Clear();

            // Collect editor tools for the shared tracker first
            EditorToolUtility.GetEditorToolsForTracker(shared.isLocked ? tracker : shared, s_CustomEditorTools);

            foreach (var customEditorTool in s_CustomEditorTools)
            {
                if (m_CustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target))
                {
                    continue;
                }
                EditorTool tool;
                preservedActiveTool |= CreateOrRestoreTool(customEditorTool, out tool);
                m_CustomEditorTools.Add(tool);
            }

            // Next, collect tools from locked inspectors
            foreach (var inspector in inspectors)
            {
                if (inspector.isLocked)
                {
                    EditorToolUtility.GetEditorToolsForTracker(inspector.tracker, s_CustomEditorTools);

                    foreach (var customEditorTool in s_CustomEditorTools)
                    {
                        // Don't add duplicate tools to either another locked inspector with the same target, or a shared tracker
                        if (m_CustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target) ||
                            m_LockedCustomEditorTools.Any(x => x.GetType() == customEditorTool.editorToolType && x.target == customEditorTool.owner.target))
                        {
                            continue;
                        }
                        EditorTool tool;
                        preservedActiveTool |= CreateOrRestoreTool(customEditorTool, out tool);
                        m_LockedCustomEditorTools.Add(tool);
                    }
                }
            }

            if (IsCustomEditorTool(m_ActiveTool) && !preservedActiveTool)
            {
                var previous = m_ActiveTool;
                m_PreviousCustomEditorToolContext = new CustomEditorToolContext(m_ActiveTool);
                RestorePreviousTool();
                DestroyImmediate(previous);
            }
        }