示例#1
0
        protected virtual Type GetDrawerTypeForComponent([NotNull] Type componentType, out Type customEditorType)
        {
            Type drawerType;

            if (drawersFor.components.TryGetValue(componentType, out drawerType))
            {
                customEditorType = null;
                return(drawerType);
            }

                        #if UNITY_EDITOR
            if (Platform.EditorMode)
            {
                if (PluginCompatibilityUtility.UseCompatibilityModeForDisplayingTarget(componentType))
                {
                    customEditorType = null;
                    return(typeof(CustomEditorComponentDrawer));
                }

                if (CustomEditorUtility.TryGetCustomEditorType(componentType, out customEditorType))
                {
                    return(typeof(CustomEditorComponentDrawer));
                }
            }
                        #endif

            customEditorType = null;
            return(typeof(ComponentDrawer));
        }
示例#2
0
        protected virtual Type GetDrawerTypeForAssets([NotNull] Object[] assets, [CanBeNull] out Object[] assetImporters, [CanBeNull] out Type customEditorType)
        {
            assetImporters   = null;
            customEditorType = null;

            // handle mismatching types
            if (!assets.AllSameType())
            {
                return(typeof(MultipleAssetDrawer));
            }

            if (AssetImporters.TryGet(assets, ref assetImporters))
            {
                var assetImporterType = assetImporters[0].GetType();
                if (!CustomEditorUtility.TryGetCustomEditorType(assetImporterType, out customEditorType))
                {
                                        #if DEV_MODE
                    Debug.LogWarning("GetDrawerTypeForAssets(" + assets[0].GetType().Name + ") Setting assetImporters of type " + assetImporterType.Name + " to null because could not find CustomEditor type for them.");
                                        #endif

                    assetImporters = null;
                    CustomEditorUtility.TryGetCustomEditorType(assets[0].GetType(), out customEditorType);
                }
            }
            else
            {
                CustomEditorUtility.TryGetCustomEditorType(assets[0].GetType(), out customEditorType);
                                #if DEV_MODE && PI_ASSERTATIONS
                Debug.Assert(assetImporters == null);
                                #endif
            }

            return(GetDrawerTypeForAsset(assets[0]));
        }
        /// <summary> Sets up an instance of the drawers for usage. </summary>
        /// <param name="setTargets"> The targets that the drawers represent. Can not be null. </param>
        /// <param name="setEditorTargets"> The targets for which the main Editor is created. E.g. the asset importers for targets. Can be null. </param>
        /// <param name="setParent"> The parent drawers of the created drawers. Can be null. </param>
        /// <param name="setInspector"> The inspector in which the IDrawer are contained. Can not be null. </param>
        /// <param name="setEditorType"> The type of the custom editor. Can be null. </param>
        protected virtual void Setup([NotNull] Object[] setTargets, [CanBeNull] Object[] setEditorTargets, [CanBeNull] Type setEditorType, [CanBeNull] IParentDrawer setParent, [NotNull] IInspector setInspector)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(setTargets.Length > 0);
                        #endif

            headerHeight           = DrawGUI.Active.AssetTitlebarHeight(false);
            headerHeightDetermined = false;

            allSameType = setTargets.AllSameType();

            if (setEditorTargets == null)
            {
                if (!allSameType)
                {
                    setEditorTargets = setTargets;
                }
                else
                {
                    if (AssetImporters.TryGet(setTargets, ref setEditorTargets))
                    {
                        var assetImporterType = setEditorTargets[0].GetType();
                        CustomEditorUtility.TryGetCustomEditorType(assetImporterType, out setEditorType);
                    }
                    else
                    {
                        setEditorTargets = setTargets;
                    }
                }
            }
            else if (setEditorTargets[0] == null)
            {
                ArrayPool <Object> .Dispose(ref setEditorTargets);

                setEditorTargets = setTargets;
            }

                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(setEditorTargets != null || !allSameType);
            Debug.Assert(!allSameType || setEditorTargets.Length > 0);
                        #endif

            editorTargets = setEditorTargets;

            Sisus.AssetLabels.Get(setTargets, ref assetLabels, ref assetLabelsOnlyOnSomeTargets);
            Sisus.AssetLabels.OnAssetLabelsChanged += OnAssetLabelsChanged;

            var target = setTargets[0];
            localPath      = setTargets.Length == 1 ? AssetDatabase.GetAssetPath(target) : "";
            isPackageAsset = FileUtility.IsPackageAsset(localPath);

            // base handles calling UpdateEditor
            base.Setup(setTargets, setParent, setInspector, setEditorType);

            FetchAssetEditor();

                        #if DEV_MODE && PI_ASSERTATIONS && UNITY_2017_2_OR_NEWER
            if (!isAssetImporter && editor != HeaderEditor)
            {
                                #if DEV_MODE && DEBUG_SETUP_EDITOR
                Debug.LogError(Msg(ToString(), " non-asset importer editor did not match HeaderEditor! - targets=", StringUtils.TypesToString(setTargets), ", editorTargets=", StringUtils.TypesToString(setEditorTargets), ", editorType=", setEditorType, ", editor=", StringUtils.TypeToString(editor), ", HeaderEditor=", HeaderEditor));
                                #endif
            }
                        #endif

                        #if DEV_MODE && DEBUG_SETUP_EDITOR
            Debug.Log(Msg(ToString(), " Setup with targets=", StringUtils.TypesToString(setTargets), ", editorTargets=", StringUtils.TypesToString(setEditorTargets), ", editorType=", setEditorType, ", editor=", StringUtils.TypeToString(editor)));
                        #endif

                        #if DEV_MODE && PI_ASSERTATIONS
            if (editor == null)
            {
                Debug.LogWarning(Msg(ToString(), " editor was null after Setup with canHaveEditor=", canHaveEditor, " and targets: ", StringUtils.TypesToString(targets)));
            }
                        #endif

                        #if DEV_MODE && PI_ASSERTATIONS
            if (assetEditor == null)
            {
                Debug.LogWarning(Msg(ToString(), " headerEditor was null after Setup with canHaveEditor=", canHaveEditor, " and targets: ", StringUtils.TypesToString(targets)));
            }
                        #endif
        }
示例#4
0
        /// <inheritdoc cref="GetEditor(ref Editor, Object[], Type, bool, Object, bool)"/>
        public void GetEditorInternal(ref Editor editor, [NotNull] Object[] targets, [CanBeNull] Type editorType, bool allTargetsHaveSameType, Object context = null, bool cache = true)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(targets != null);
            Debug.Assert(!targets.ContainsNullObjects(), "Editors.GetEditorInternal called with tagets containing null Objects");
            Debug.Assert(allTargetsHaveSameType == targets.AllSameType());
            Debug.Assert(targets.Length > 0, "GetEditor called with empty targets array! editorType was " + (editorType == null ? "null" : editorType.Name));
                        #endif

            if (editor != null)
            {
                                #if !NET_STANDARD_2_0
                var previousEditorTargets = getTargets(editor);
                var previousEditorContext = getContext(editor);
                                #else
                var previousEditorTargets = getTargets.GetValue(editor) as Object[];
                var previousEditorContext = getContext.GetValue(editor) as Object;
                                #endif
                if (targets.ContentsMatch(previousEditorTargets) && context == previousEditorContext)
                {
                    return;
                }

                DisposeInternal(ref editor);
            }

            if (!allTargetsHaveSameType)
            {
                GetEditor(ref editor, targets[0], editorType, context, cache);
                return;
            }

                        #if UNITY_2017_2_OR_NEWER
            bool isAssetImporterEditor = editorType != null?Types.AssetImporterEditor.IsAssignableFrom(editorType) : typeof(AssetImporter).IsAssignableFrom(targets[0].GetType());               //editorType != null && Types.AssetImporterEditor.IsAssignableFrom(editorType);

            var editorKey = new EditorKey(targets, isAssetImporterEditor);
                        #else
            var editorKey = new EditorKey(targets, false);
                        #endif

            if (cachedEditors.TryGetValue(editorKey, out editor))
            {
                if (editor != null)
                {
                    if (!DisposeIfInvalid(ref editor))
                    {
                        OnBecameActive(editor);

                                                #if DEV_MODE && DEBUG_GET_EDITOR
                        Debug.Log("Editors.GetEditor: for targets " + StringUtils.TypesToString(targets) + " and editorType " + StringUtils.ToString(editorType) + " returning cached: " + editor.GetType().Name + " with key=" + editorKey.GetHashCode());
                                                #endif
                        return;
                    }
                                        #if DEV_MODE && DEBUG_DESTROYED_CACHED_EDITORS
                    Debug.LogWarning("cachedEditors for targets " + StringUtils.TypeToString(targets) + " and editorType " + StringUtils.ToString(editorType) + " with EditorKey hashCode " + editorKey.GetHashCode() + " contained editor with null targets!\nCachedEditors:\n" + StringUtils.ToString(cachedEditors, "\n"));
                                        #endif
                }
                                #if DEV_MODE && DEBUG_DESTROYED_CACHED_EDITORS
                else
                {
                    Debug.LogWarning("cachedEditors for targets " + StringUtils.TypesToString(targets) + " and editorType " + StringUtils.ToString(editorType) + " with EditorKey hashCode " + editorKey.GetHashCode() + " contained a null value!\nCachedEditors:\n" + StringUtils.ToString(cachedEditors, "\n"));
                }
                                #endif
            }

                        #if DEV_MODE && DEBUG_GET_EDITOR
            Debug.Log(StringUtils.ToColorizedString("Editors.GetEditor called for ", StringUtils.ToString(targets), " with editorType=", editorType, ", context=", context, ", key=", editorKey.GetHashCode(), ", cache=", cache));
                        #endif

                        #if SUPPORT_EDITORS_FOR_INTERFACES
            if (editorType == null)
            {
                var interfaces = targets[0].GetType().GetInterfaces();
                for (int n = interfaces.Length - 1; n >= 0; n--)
                {
                    Type editorForInterface;
                    if (CustomEditorUtility.TryGetCustomEditorType(interfaces[n], out editorForInterface))
                    {
                        editorType = editorForInterface;
                                                #if DEV_MODE
                        Debug.Log("Editors.GetEditor : Replaced null editorType with interface based type " + StringUtils.ToString(editorType));
                                                #endif
                    }
                }
            }
                        #endif

            try
            {
                editor = Editor.CreateEditorWithContext(targets, context, editorType);
            }
                        #if DEV_MODE
            catch (Exception e)
            {
                Debug.LogError("Editor.CreateEditor for targets " + StringUtils.TypesToString(targets) + " and editorType " + StringUtils.ToString(editorType) + ": " + e);
                        #else
            catch
            {
                        #endif
                return;
            }

            if (editor == null)
            {
                                #if DEV_MODE
                Debug.LogWarning("Editor.CreateEditor for targets " + StringUtils.TypesToString(targets) + " and editorType " + StringUtils.ToString(editorType) + " returned null!");
                                #endif
                return;
            }

                        #if DEV_MODE && DEBUG_GET_EDITOR
            Debug.Log("Editors.GetEditor: Created new: " + editor.GetType().Name + " for " + StringUtils.ToString(targets) + " with key=" + editorKey.GetHashCode() + ", cache=" + StringUtils.ToColorizedString(cache));
                        #endif

            if (cache)
            {
                cachedEditors[editorKey] = editor;
            }
        }