/// <inheritdoc cref="GetEditor(ref Editor, Object, Type, Object, bool)"/> public void GetEditorInternal(ref Editor editor, [NotNull] Object target, Type editorType = null, Object context = null, bool cache = true) { if (editor != null) { #if !NET_STANDARD_2_0 var editorTargets = getTargets(editor); if (editorTargets.Length == 1 && editorTargets[0] == target && context == getContext(editor)) #else var editorTargets = getTargets.GetValue(editor) as Object[]; if (editorTargets.Length == 1 && editorTargets[0] == target && context == getContext.GetValue(editor) as Object) #endif { return; } DisposeInternal(ref editor); } #if UNITY_2017_2_OR_NEWER bool isAssetImporterEditor = editorType != null && Types.AssetImporterEditor.IsAssignableFrom(editorType); var editorKey = new EditorKey(target, isAssetImporterEditor); #else var editorKey = new EditorKey(target, 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: returning cached: " + editor.GetType().Name + " for " + StringUtils.ToString(target) + " with key=" + editorKey.GetHashCode()); #endif return; } #if DEV_MODE && DEBUG_DESTROYED_CACHED_EDITORS Debug.LogWarning("cachedEditors for target " + StringUtils.TypeToString(target) + " 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 target " + StringUtils.TypeToString(target) + " and editorType " + StringUtils.ToString(editorType) + " with EditorKey hashCode " + editorKey.GetHashCode() + " contained a null value!\nCachedEditors:\n" + StringUtils.ToString(cachedEditors, "\n")); } #endif } #if DEV_MODE || IGNORE_GAMEOBJECT_EDITOR_OVERRIDES if (editorType == null) { if (target is GameObject) { editorType = GameObjectInspectorType; } } #endif editor = Editor.CreateEditorWithContext(ArrayPool <Object> .CreateWithContent(target), context, editorType); if (editor == null) { #if DEV_MODE Debug.LogWarning("Editor.CreateEditor for target " + StringUtils.TypeToString(target) + " 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(target) + " with key=" + editorKey.GetHashCode()); #endif if (cache) { cachedEditors[editorKey] = editor; } }
/// <summary> /// Disposes the SerializedObject of the Editor and destroys the Editor. /// </summary> /// <param name="editor"> [in,out] The editor to Dispose. This should not be null when the method is called. </param> /// <param name="key"> Dictionary key for the editor </param> private void Dispose(ref Editor editor, EditorKey key) { #if DEV_MODE Debug.Assert(!ReferenceEquals(editor, null), "Dispose called for null editor where ReferenceEquals(editor, " + StringUtils.Null + ")=" + StringUtils.True); #endif if (IsCached(key)) { #if !NEVER_CACHE_ANY_EDITORS if (!EditorApplication.isCompiling && Validate(editor)) { #if DEV_MODE && DEBUG_DISPOSE Debug.Log("Editors.Dispose - <color=green>Keeping</color> cached Editor " + StringUtils.TypeToString(editor) + " of " + StringUtils.TypeToString(editor.target) + " (" + StringUtils.ToString(editor.target) + ") with key=" + key.GetHashCode()); #endif editor = null; return; } #endif #if DEV_MODE && DEBUG_DISPOSE Debug.Log("Editors.Dispose - <color=red>Removing</color> cached Editor " + StringUtils.TypeToString(editor) + " of " + StringUtils.TypeToString(editor.target) + " (" + StringUtils.ToString(editor.target) + ") with key=" + key.GetHashCode()); #endif RemoveFromCache(key); } #if DEV_MODE && DEBUG_DISPOSE else { Debug.Log("Editors.Dispose - IsCached(" + StringUtils.False + "): Editor " + StringUtils.TypeToString(editor) + " of " + StringUtils.TypeToString(editor.target) + " (" + StringUtils.ToString(editor.target) + ") with key=" + key.GetHashCode() + "\ncachedEditors:\n" + StringUtils.ToString(cachedEditors, "\n")); } #endif DisposeStatic(ref editor); }
/// <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; } }