示例#1
0
        protected override void OnGUI()
        {
            if (m_ParentInspectorWindow == null)
            {
                Close();
                EditorGUIUtility.ExitGUI();
            }
            Editor.m_AllowMultiObjectAccess = true;

            CreatePreviewables();

            // Do we have an editor that supports previews? Null if not.
            IPreviewable[] editorsWithPreviews = GetEditorsWithPreviews(tracker.activeEditors);
            IPreviewable   editor = GetEditorThatControlsPreview(editorsWithPreviews);

            bool hasPreview = (editor != null) && editor.HasPreviewGUI();

            // Toolbar
            Rect toolbarRect = EditorGUILayout.BeginHorizontal(GUIContent.none, Styles.preToolbar, GUILayout.Height(17));

            {
                GUILayout.FlexibleSpace();
                var labelRect = GUILayoutUtility.GetLastRect();
                // Label
                string label = string.Empty;
                if ((editor != null))
                {
                    label = editor.GetPreviewTitle().text;
                }
                GUI.Label(labelRect, label, Styles.preToolbar2);
                if (hasPreview)
                {
                    editor.OnPreviewSettings();
                }
            } EditorGUILayout.EndHorizontal();


            Event evt = Event.current;

            if (evt.type == EventType.MouseUp && evt.button == 1 && toolbarRect.Contains(evt.mousePosition))
            {
                Close();
                evt.Use();
                // Don't draw preview if we just closed this window
                return;
            }

            // Preview
            Rect previewPosition = GUILayoutUtility.GetRect(0, 10240, 64, 10240);

            // Draw background
            if (Event.current.type == EventType.Repaint)
            {
                Styles.preBackground.Draw(previewPosition, false, false, false, false);
            }

            // Draw preview
            if ((editor != null) && editor.HasPreviewGUI())
            {
                editor.DrawPreview(previewPosition);
            }
        }
        static void CacheServerVersion1GUI(bool allowCacheServerChanges, string overrideAddress)
        {
            GUILayout.Label(Properties.assetPipelineVersion1, EditorStyles.boldLabel);

            GUILayout.Space(5);

            if (!allowCacheServerChanges)
            {
                EditorGUILayout.HelpBox("Cache Server preferences cannot be modified because a remote address was specified via command line argument. To modify Cache Server preferences, restart Unity without the " + kIpAddressKeyArgs + " command line argument.", MessageType.Info, true);
            }

            using (new EditorGUI.DisabledScope(!allowCacheServerChanges))
            {
                var displayMode = !allowCacheServerChanges ? CacheServerMode.Remote : s_CacheServerMode;
                s_CacheServerMode = (CacheServerMode)EditorGUILayout.EnumPopup("Cache Server Mode", displayMode);
            }

            if (s_CacheServerMode == CacheServerMode.Remote)
            {
                using (new EditorGUI.DisabledScope(!allowCacheServerChanges))
                {
                    var displayAddress = overrideAddress != null ? overrideAddress : s_CacheServerIPAddress;
                    s_CacheServerIPAddress = EditorGUILayout.TextField("IP Address", displayAddress);

                    if (GUI.changed)
                    {
                        s_ConnectionState = ConnectionState.Unknown;
                    }
                }

                GUILayout.Space(5);

                using (new EditorGUI.DisabledScope(AssetDatabase.IsV2Enabled()))
                {
                    if (GUILayout.Button("Check Connection", GUILayout.Width(150)))
                    {
                        if (InternalEditorUtility.CanConnectToCacheServer())
                        {
                            s_ConnectionState = ConnectionState.Success;
                        }
                        else
                        {
                            s_ConnectionState = ConnectionState.Failure;
                        }
                    }
                }

                GUILayout.Space(-25);

                var s = AssetDatabase.IsV1Enabled() ? s_ConnectionState : ConnectionState.Unknown;

                switch (s)
                {
                case ConnectionState.Success:
                    EditorGUILayout.HelpBox("Connection successful.", MessageType.Info, false);
                    break;

                case ConnectionState.Failure:
                    EditorGUILayout.HelpBox("Connection failed.", MessageType.Warning, false);
                    break;

                case ConnectionState.Unknown:
                    GUILayout.Space(44);
                    break;
                }
            }
            else if (s_CacheServerMode == CacheServerMode.Local)
            {
                const int kMinSizeInGigabytes = 1;
                const int kMaxSizeInGigabytes = 200;

                // Write size in GigaBytes.
                s_LocalCacheServerSize = EditorGUILayout.IntSlider(Properties.maxCacheSize, s_LocalCacheServerSize, kMinSizeInGigabytes, kMaxSizeInGigabytes);

                s_EnableCustomPath = EditorGUILayout.Toggle(Properties.customCacheLocation, s_EnableCustomPath);
                // browse for cache folder if not per project
                if (s_EnableCustomPath)
                {
                    GUIStyle style = EditorStyles.miniButton;
                    GUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel(Properties.cacheFolderLocation, style);
                    Rect       r       = GUILayoutUtility.GetRect(GUIContent.none, style);
                    GUIContent guiText = string.IsNullOrEmpty(s_CachePath) ? Properties.browse : new GUIContent(s_CachePath);
                    if (EditorGUI.DropdownButton(r, guiText, FocusType.Passive, style))
                    {
                        string pathToOpen = s_CachePath;
                        string path       = EditorUtility.OpenFolderPanel(Properties.browseCacheLocation.text, pathToOpen, "");
                        if (!string.IsNullOrEmpty(path))
                        {
                            if (LocalCacheServer.CheckValidCacheLocation(path))
                            {
                                s_CachePath = path;
                                WritePreferences();
                            }
                            else
                            {
                                EditorUtility.DisplayDialog("Invalid Cache Location", string.Format("The directory {0} contains some files which don't look like Unity Cache server files. Please delete the directory contents or choose another directory.", path), "OK");
                            }
                            EditorGUIUtility.ExitGUI();
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                else
                {
                    s_CachePath = "";
                }

                bool locationExists = LocalCacheServer.CheckCacheLocationExists();
                if (locationExists == true)
                {
                    GUIContent cacheSizeIs = EditorGUIUtility.TrTextContent("Cache size is unknown");
                    if (s_LocalCacheServerUsedSize != -1)
                    {
                        cacheSizeIs = EditorGUIUtility.TextContent("Cache size is " + EditorUtility.FormatBytes(s_LocalCacheServerUsedSize));
                    }

                    GUILayout.BeginHorizontal();
                    GUIStyle style = EditorStyles.miniButton;
                    EditorGUILayout.PrefixLabel(cacheSizeIs, style);
                    Rect r = GUILayoutUtility.GetRect(GUIContent.none, style);
                    if (EditorGUI.Button(r, Properties.enumerateCache, style))
                    {
                        s_LocalCacheServerUsedSize = LocalCacheServer.CheckCacheLocationExists() ? FileUtil.GetDirectorySize(LocalCacheServer.GetCacheLocation()) : 0;
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                    GUIContent spacerContent = EditorGUIUtility.blankContent;
                    EditorGUILayout.PrefixLabel(spacerContent, style);
                    Rect r2 = GUILayoutUtility.GetRect(GUIContent.none, style);
                    if (EditorGUI.Button(r2, Properties.cleanCache, style))
                    {
                        LocalCacheServer.Clear();
                        s_LocalCacheServerUsedSize = 0;
                    }
                    GUILayout.EndHorizontal();
                }
                else
                {
                    EditorGUILayout.HelpBox("Local cache directory does not exist - please check that you can access the cache folder and are able to write to it", MessageType.Warning, false);
                    //If the cache server was on an external HDD or on a temporarily unavailable network drive, set the size to unknown so that the user re-queries it when they've reconnected
                    s_LocalCacheServerUsedSize = -1;
                }

                GUILayout.Label(Properties.cacheFolderLocation.text + ":");
                GUILayout.Label(LocalCacheServer.GetCacheLocation(), Styles.cacheFolderLocation);
            }
        }
示例#3
0
        void HandleDragPerformEvent(Editor[] editors, Event evt, ref int targetIndex)
        {
            if (targetIndex != -1)
            {
                var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                if (!draggingMode.HasValue || draggingMode.Value == DraggingMode.NotApplicable)
                {
                    targetIndex = -1;
                    return;
                }

                if (!editors[targetIndex].targets.All(t => t is Component))
                {
                    return;
                }

                var targetComponents = editors[targetIndex].targets.Cast <Component>().ToArray();

                if (draggingMode.Value == DraggingMode.Script)
                {
                    var scripts = DragAndDrop.objectReferences.Cast <MonoScript>();

                    // Ensure all script components can be added
                    var valid = true;
                    foreach (var targetComponent in targetComponents)
                    {
                        var gameObject = targetComponent.gameObject;
                        if (scripts.Any(s => !ComponentUtility.WarnCanAddScriptComponent(gameObject, s)))
                        {
                            valid = false;
                            break;
                        }
                    }

                    if (valid)
                    {
                        Undo.IncrementCurrentGroup();
                        var undoGroup = Undo.GetCurrentGroup();

                        // Add script components
                        var index           = 0;
                        var addedComponents = new Component[targetComponents.Length * scripts.Count()];
                        for (int i = 0; i < targetComponents.Length; i++)
                        {
                            var  targetComponent   = targetComponents[i];
                            var  gameObject        = targetComponent.gameObject;
                            bool targetIsTransform = targetComponent is Transform;
                            foreach (var script in scripts)
                            {
                                addedComponents[index++] = ObjectFactory.AddComponent(gameObject, script.GetClass());
                            }

                            // If the target is a Transform, the AddComponent might have replaced it with a RectTransform.
                            // Handle this possibility by updating the target component.
                            if (targetIsTransform)
                            {
                                targetComponents[i] = gameObject.transform;
                            }
                        }

                        // Move added components relative to target components
                        if (!ComponentUtility.MoveComponentsRelativeToComponents(addedComponents, targetComponents, m_TargetAbove))
                        {
                            // Ensure we have the same selection after calling RevertAllDownToGroup below (MoveComponentsRelativeToComponents can have opened a Prefab in Prefab Mode and changed selection to that root)
                            var wantedSelectedGameObject = Selection.activeGameObject;

                            // Revert added components if move operation fails (e.g. user has been shown the dialog with 'prefab instance restructuring is not posssible' or object is not editable)
                            Undo.RevertAllDownToGroup(undoGroup);

                            if (wantedSelectedGameObject != Selection.activeGameObject)
                            {
                                Selection.activeGameObject = wantedSelectedGameObject;
                            }
                        }
                    }
                }
                else
                {
                    // Handle dragging components
                    var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                    if (sourceComponents.Length == 0 || targetComponents.Length == 0)
                    {
                        return;
                    }

                    MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), false);
                }

                targetIndex = -1;
                DragAndDrop.AcceptDrag();
                evt.Use();
                EditorGUIUtility.ExitGUI();
            }
        }
示例#4
0
        protected void DrawPreview()
        {
            GUI.color = EditorApplication.isPlayingOrWillChangePlaymode ? HostView.kPlayModeDarken : Color.white;
            if (m_ParentInspectorWindow == null)
            {
                Close();
                EditorGUIUtility.ExitGUI();
            }

            Editor.m_AllowMultiObjectAccess = true;

            // Do we have an editor that supports previews? Null if not.
            IPreviewable[] editorsWithPreviews = GetEditorsWithPreviews(tracker.activeEditors);
            IPreviewable   editor = GetEditorThatControlsPreview(editorsWithPreviews);

            bool hasPreview = (editor != null) && editor.HasPreviewGUI();

            // Toolbar
            Rect toolbarRect = EditorGUILayout.BeginHorizontal(GUIContent.none, EditorStyles.toolbar, GUILayout.Height(kBottomToolbarHeight));

            {
                // Label
                string label = string.Empty;
                if ((editor != null))
                {
                    label = editor.GetPreviewTitle().text;
                }

                GUILayout.Label(label, Styles.preToolbarLabel);

                GUILayout.FlexibleSpace();

                if (hasPreview)
                {
                    editor.OnPreviewSettings();
                }
            } EditorGUILayout.EndHorizontal();


            Event evt = Event.current;

            if (evt.type == EventType.MouseUp && evt.button == 1 && toolbarRect.Contains(evt.mousePosition))
            {
                evt.Use();
                Close();
                // Don't draw preview if we just closed this window
                return;
            }

            // Preview
            Rect previewPosition = GUILayoutUtility.GetRect(0, 10240, 64, 10240);

            // Draw background
            if (Event.current.type == EventType.Repaint)
            {
                Styles.preBackground.Draw(previewPosition, false, false, false, false);
            }

            // Draw preview
            if ((editor != null) && editor.HasPreviewGUI())
            {
                editor.DrawPreview(previewPosition);
            }
        }
        private static void OnGUI(string searchContext)
        {
            EditorGUIUtility.labelWidth = 200f;
            // Get event type before the event is used.
            var eventType = Event.current.type;

            if (!InternalEditorUtility.HasTeamLicense())
            {
                GUILayout.Label(EditorGUIUtility.TempContent("You need to have a Pro or Team license to use the cache server.", EditorGUIUtility.GetHelpIcon(MessageType.Warning)), EditorStyles.helpBox);
            }


            using (new EditorGUI.DisabledScope(!InternalEditorUtility.HasTeamLicense()))
            {
                if (!s_PrefsLoaded)
                {
                    ReadPreferences();

                    if (s_CacheServerMode != CacheServerMode.Disabled && s_ConnectionState == ConnectionState.Unknown)
                    {
                        if (InternalEditorUtility.CanConnectToCacheServer())
                        {
                            s_ConnectionState = ConnectionState.Success;
                        }
                        else
                        {
                            s_ConnectionState = ConnectionState.Failure;
                        }
                    }

                    s_PrefsLoaded = true;
                }

                EditorGUI.BeginChangeCheck();

                var overrideAddress = GetCommandLineRemoteAddressOverride();

                if (overrideAddress != null)
                {
                    EditorGUILayout.HelpBox("Cache Server preferences cannot be modified because a remote address was specified via command line argument. To modify Cache Server preferences, restart Unity without the " + kIpAddressKeyArgs + " command line argument.", MessageType.Info, true);
                }

                using (new EditorGUI.DisabledScope(overrideAddress != null))
                {
                    var displayMode = overrideAddress != null ? CacheServerMode.Remote : s_CacheServerMode;
                    s_CacheServerMode = (CacheServerMode)EditorGUILayout.EnumPopup("Cache Server Mode", displayMode);
                }

                if (s_CacheServerMode == CacheServerMode.Remote)
                {
                    using (new EditorGUI.DisabledScope(overrideAddress != null))
                    {
                        var displayAddress = overrideAddress != null ? overrideAddress : s_CacheServerIPAddress;
                        s_CacheServerIPAddress = EditorGUILayout.DelayedTextField("IP Address", displayAddress);
                        if (GUI.changed)
                        {
                            s_ConnectionState = ConnectionState.Unknown;
                        }
                    }

                    GUILayout.Space(5);

                    if (GUILayout.Button("Check Connection", GUILayout.Width(150)))
                    {
                        if (InternalEditorUtility.CanConnectToCacheServer())
                        {
                            s_ConnectionState = ConnectionState.Success;
                        }
                        else
                        {
                            s_ConnectionState = ConnectionState.Failure;
                        }
                    }

                    GUILayout.Space(-25);
                    switch (s_ConnectionState)
                    {
                    case ConnectionState.Success:
                        EditorGUILayout.HelpBox("Connection successful.", MessageType.Info, false);
                        break;

                    case ConnectionState.Failure:
                        EditorGUILayout.HelpBox("Connection failed.", MessageType.Warning, false);
                        break;

                    case ConnectionState.Unknown:
                        GUILayout.Space(44);
                        break;
                    }
                }
                else if (s_CacheServerMode == CacheServerMode.Local)
                {
                    const int kMinSizeInGigabytes = 1;
                    const int kMaxSizeInGigabytes = 200;

                    // Write size in GigaBytes.
                    s_LocalCacheServerSize = EditorGUILayout.IntSlider(Properties.maxCacheSize, s_LocalCacheServerSize, kMinSizeInGigabytes, kMaxSizeInGigabytes);

                    s_EnableCustomPath = EditorGUILayout.Toggle(Properties.customCacheLocation, s_EnableCustomPath);
                    // browse for cache folder if not per project
                    if (s_EnableCustomPath)
                    {
                        GUIStyle style = EditorStyles.miniButton;
                        GUILayout.BeginHorizontal();
                        EditorGUILayout.PrefixLabel(Properties.cacheFolderLocation, style);
                        Rect       r       = GUILayoutUtility.GetRect(GUIContent.none, style);
                        GUIContent guiText = string.IsNullOrEmpty(s_CachePath) ? Properties.browse : new GUIContent(s_CachePath);
                        if (EditorGUI.DropdownButton(r, guiText, FocusType.Passive, style))
                        {
                            string pathToOpen = s_CachePath;
                            string path       = EditorUtility.OpenFolderPanel(Properties.browseCacheLocation.text, pathToOpen, "");
                            if (!string.IsNullOrEmpty(path))
                            {
                                if (LocalCacheServer.CheckValidCacheLocation(path))
                                {
                                    s_CachePath = path;
                                    WritePreferences();
                                }
                                else
                                {
                                    EditorUtility.DisplayDialog("Invalid Cache Location", string.Format("The directory {0} contains some files which don't look like Unity Cache server files. Please delete the directory contents or choose another directory.", path), "OK");
                                }
                                EditorGUIUtility.ExitGUI();
                            }
                        }
                        GUILayout.EndHorizontal();
                    }
                    else
                    {
                        s_CachePath = "";
                    }

                    bool locationExists = LocalCacheServer.CheckCacheLocationExists();
                    if (locationExists == true)
                    {
                        GUIContent cacheSizeIs = EditorGUIUtility.TrTextContent("Cache size is unknown");
                        if (s_LocalCacheServerUsedSize != -1)
                        {
                            cacheSizeIs = EditorGUIUtility.TextContent("Cache size is " + EditorUtility.FormatBytes(s_LocalCacheServerUsedSize));
                        }

                        GUILayout.BeginHorizontal();
                        GUIStyle style = EditorStyles.miniButton;
                        EditorGUILayout.PrefixLabel(cacheSizeIs, style);
                        Rect r = GUILayoutUtility.GetRect(GUIContent.none, style);
                        if (EditorGUI.Button(r, Properties.enumerateCache, style))
                        {
                            s_LocalCacheServerUsedSize = LocalCacheServer.CheckCacheLocationExists() ? FileUtil.GetDirectorySize(LocalCacheServer.GetCacheLocation()) : 0;
                        }
                        GUILayout.EndHorizontal();

                        GUILayout.BeginHorizontal();
                        GUIContent spacerContent = EditorGUIUtility.blankContent;
                        EditorGUILayout.PrefixLabel(spacerContent, style);
                        Rect r2 = GUILayoutUtility.GetRect(GUIContent.none, style);
                        if (EditorGUI.Button(r2, Properties.cleanCache, style))
                        {
                            LocalCacheServer.Clear();
                            s_LocalCacheServerUsedSize = 0;
                        }
                        GUILayout.EndHorizontal();
                    }
                    else
                    {
                        EditorGUILayout.HelpBox("Local cache directory does not exist - please check that you can access the cache folder and are able to write to it", MessageType.Warning, false);
                        //If the cache server was on an external HDD or on a temporarily unavailable network drive, set the size to unknown so that the user re-queries it when they've reconnected
                        s_LocalCacheServerUsedSize = -1;
                    }

                    GUILayout.Label(Properties.cacheFolderLocation.text + ":");
                    GUILayout.Label(LocalCacheServer.GetCacheLocation(), Styles.cacheFolderLocation);
                }

                if (EditorGUI.EndChangeCheck())
                {
                    s_HasPendingChanges = true;
                }

                // Only commit changes when we don't have an active hot control, to avoid restarting the cache server all the time while the slider is dragged, slowing down the UI.
                if (s_HasPendingChanges && GUIUtility.hotControl == 0)
                {
                    s_HasPendingChanges = false;
                    WritePreferences();
                    ReadPreferences();
                }
            }
        }
        void HandleEditorDragging(Editor[] editors, int editorIndex, Rect targetRect, float markerY, bool bottomTarget)
        {
            var evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:
                if (targetRect.Contains(evt.mousePosition))
                {
                    var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                    if (!draggingMode.HasValue)
                    {
                        var draggedObjects = DragAndDrop.objectReferences;

                        if (draggedObjects.Length == 0)
                        {
                            draggingMode = DraggingMode.NotApplicable;
                        }
                        else if (draggedObjects.All(o => o is Component && !(o is Transform)))
                        {
                            draggingMode = DraggingMode.Component;
                        }
                        else if (draggedObjects.All(o => o is MonoScript))
                        {
                            draggingMode = DraggingMode.Script;
                        }
                        else
                        {
                            draggingMode = DraggingMode.NotApplicable;
                        }

                        DragAndDrop.SetGenericData(k_DraggingModeKey, draggingMode);
                    }

                    if (draggingMode.Value != DraggingMode.NotApplicable)
                    {
                        if (bottomTarget)
                        {
                            m_TargetAbove = false;
                            m_TargetIndex = m_LastIndex;
                        }
                        else
                        {
                            m_TargetAbove = evt.mousePosition.y < targetRect.y + targetRect.height / 2f;
                            m_TargetIndex = editorIndex;

                            if (m_TargetAbove)
                            {
                                m_TargetIndex++;
                                while (m_TargetIndex < editors.Length && m_InspectorWindow.ShouldCullEditor(editors, m_TargetIndex))
                                {
                                    m_TargetIndex++;
                                }

                                if (m_TargetIndex == editors.Length)
                                {
                                    m_TargetIndex = -1;
                                    return;
                                }
                            }
                        }

                        if (m_TargetAbove && m_InspectorWindow.EditorHasLargeHeader(m_TargetIndex, editors))
                        {
                            m_TargetIndex--;
                            while (m_TargetIndex >= 0 && m_InspectorWindow.ShouldCullEditor(editors, m_TargetIndex))
                            {
                                m_TargetIndex--;
                            }

                            if (m_TargetIndex == -1)
                            {
                                return;
                            }

                            m_TargetAbove = false;
                        }

                        if (draggingMode.Value == DraggingMode.Script)
                        {
                            // Validate dragging scripts
                            // Always allow script dragging, instead fail during DragPerform with a dialog box
                            DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                        }
                        else
                        {
                            // Validate dragging components
                            var valid = false;
                            if (editors[m_TargetIndex].targets.All(t => t is Component))
                            {
                                var targetComponents = editors[m_TargetIndex].targets.Cast <Component>().ToArray();
                                var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                                valid = MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), true);
                            }

                            if (valid)
                            {
                                DragAndDrop.visualMode = EditorUtility.EventHasDragCopyModifierPressed(evt) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Move;
                            }
                            else
                            {
                                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                                m_TargetIndex          = -1;
                                return;
                            }
                        }

                        evt.Use();
                    }
                }
                else
                {
                    m_TargetIndex = -1;
                }
                break;

            case EventType.DragPerform:
                if (m_TargetIndex != -1)
                {
                    var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ;
                    if (!draggingMode.HasValue || draggingMode.Value == DraggingMode.NotApplicable)
                    {
                        m_TargetIndex = -1;
                        return;
                    }

                    if (!editors[m_TargetIndex].targets.All(t => t is Component))
                    {
                        return;
                    }

                    var targetComponents = editors[m_TargetIndex].targets.Cast <Component>().ToArray();

                    if (draggingMode.Value == DraggingMode.Script)
                    {
                        var scripts = DragAndDrop.objectReferences.Cast <MonoScript>();

                        // Ensure all script components can be added
                        var valid = true;
                        foreach (var targetComponent in targetComponents)
                        {
                            var gameObject = targetComponent.gameObject;
                            if (scripts.Any(s => !ComponentUtility.WarnCanAddScriptComponent(targetComponent.gameObject, s)))
                            {
                                valid = false;
                                break;
                            }
                        }

                        if (valid)
                        {
                            Undo.IncrementCurrentGroup();
                            var undoGroup = Undo.GetCurrentGroup();

                            // Add script components
                            var index           = 0;
                            var addedComponents = new Component[targetComponents.Length * scripts.Count()];
                            foreach (var targetComponent in targetComponents)
                            {
                                var gameObject = targetComponent.gameObject;
                                foreach (var script in scripts)
                                {
                                    addedComponents[index++] = ObjectFactory.AddComponent(gameObject, script.GetClass());
                                }
                            }

                            // Move added components relative to target components
                            if (!ComponentUtility.MoveComponentsRelativeToComponents(addedComponents, targetComponents, m_TargetAbove))
                            {
                                // Revert added components if move operation fails (e.g. user aborts when asked to break prefab instance)
                                Undo.RevertAllDownToGroup(undoGroup);
                            }
                        }
                    }
                    else
                    {
                        // Handle dragging components
                        var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray();
                        if (sourceComponents.Length == 0 || targetComponents.Length == 0)
                        {
                            return;
                        }

                        MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), false);
                    }

                    m_TargetIndex = -1;
                    DragAndDrop.AcceptDrag();
                    evt.Use();
                    EditorGUIUtility.ExitGUI();
                }
                break;

            case EventType.DragExited:
                m_TargetIndex = -1;
                break;

            case EventType.Repaint:
                if (m_TargetIndex != -1 && targetRect.Contains(evt.mousePosition))
                {
                    var markerRect = new Rect(targetRect.x, markerY, targetRect.width, 3f);
                    if (!m_TargetAbove)
                    {
                        markerRect.y += 2f;
                    }

                    Styles.insertionMarker.Draw(markerRect, false, false, false, false);
                }
                break;
            }
        }
示例#7
0
        public static bool SetStaticFlags(Object[] targetObjects, int changedFlags, bool flagValue)
        {
            bool allFlagsAreChanged = (changedFlags == int.MaxValue);
            var  msgChangedFlags    = changedFlags;

            if (msgChangedFlags < 0 && !allFlagsAreChanged)
            {
                //In order to have a list of human readable list of changed flags,
                //we need to filter out bits that does not correspont to any option.
                int allPossibleValues = 0;
                var values            = Enum.GetValues(typeof(StaticEditorFlags));
                foreach (var value in values)
                {
                    allPossibleValues |= (int)value;
                }

                msgChangedFlags = msgChangedFlags & allPossibleValues;
            }
            StaticEditorFlags flag = allFlagsAreChanged ?
                                     (StaticEditorFlags)0 :
                                     (StaticEditorFlags)Enum.Parse(typeof(StaticEditorFlags), msgChangedFlags.ToString());


            // Should we include child objects?
            GameObjectUtility.ShouldIncludeChildren includeChildren = GameObjectUtility.DisplayUpdateChildrenDialogIfNeeded(targetObjects.OfType <GameObject>(), "Change Static Flags",
                                                                                                                            allFlagsAreChanged ?
                                                                                                                            "Do you want to " + (flagValue ? "enable" : "disable") + " the static flags for all the child objects as well?" :
                                                                                                                            "Do you want to " + (flagValue ? "enable" : "disable") + " the " + ObjectNames.NicifyVariableName(flag.ToString()) + " flag for all the child objects as well?");

            if (includeChildren == GameObjectUtility.ShouldIncludeChildren.Cancel)
            {
                EditorGUIUtility.ExitGUI();
                return(false);
            }
            var objects = GetObjects(targetObjects, includeChildren == GameObjectUtility.ShouldIncludeChildren.IncludeChildren);

            Undo.RecordObjects(objects, "Change Static Flags");

            // Calculate new flags value separately for each object so other flags are not affected.
            foreach (GameObject go in objects)
            {
                int goFlags = (int)GameObjectUtility.GetStaticEditorFlags(go);

                // Following change is for backward compatibility after fixing case 1221145
                if (goFlags < 0)
                {
                    goFlags = int.MaxValue;
                }

                // MaxValue will cause issues when changing it to other values so we set it to the max possible value
                // that Static Editor flags can have before doing anything else with it
                if (goFlags == int.MaxValue && flagValue == false)
                {
                    goFlags = (int)Math.Pow(2, Enum.GetNames(typeof(StaticEditorFlags)).Length - 1) - 1;
                }

                goFlags = flagValue ?
                          goFlags | changedFlags :
                          goFlags & ~changedFlags;
                GameObjectUtility.SetStaticEditorFlags(go, (StaticEditorFlags)goFlags);
            }

            return(true);
        }