Skip to content

Commit

Permalink
Merge pull request #58 from Looooong/fix/mixture-editor-not-loading-d…
Browse files Browse the repository at this point in the history
…efault-texture-editor-for-texture2darray

[Fix] MixtureEditor not loading default texture editor for Texture2DArray
  • Loading branch information
peeweek authored Nov 3, 2022
2 parents 85010e5 + d74d3fc commit 8e917ef
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions Packages/com.alelievr.mixture/Editor/Graph/MixtureInspector.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
using System;
using System.Linq;
using System.Collections.Generic;
using Object = UnityEngine.Object;
using UnityEngine.UIElements;
Expand Down Expand Up @@ -104,8 +102,11 @@ static void DrawMixtureSmallIcon(Rect rect, Texture2D mixtureIcon, bool focused)
}
}

class MixtureEditor : Editor
abstract class MixtureEditor : Editor
{
// All Unity texture editor types are internal, so we have to look it up by name via reflection.
// The editor name can be gotten from https://github.com/Unity-Technologies/UnityCsReference/tree/master/Editor/Mono/Inspector
protected abstract string defaultTextureEditorTypeName { get; }
protected Editor defaultTextureEditor;
protected Editor variantEditor;
protected MixtureGraph graph;
Expand Down Expand Up @@ -141,36 +142,27 @@ protected virtual void OnEnable()
Debug.LogError("Can't find parent graph for Mixture Variant " + variant);
}
}

CreateDefaultTextureEditor();
}

static Dictionary< Type, string > defaultTextureInspectors = new Dictionary< Type, string >()
{
{ typeof(Texture2D), "UnityEditor.TextureInspector"},
{ typeof(Texture3D), "UnityEditor.Texture3DInspector"},
{ typeof(Cubemap), "UnityEditor.CubemapInspector"},
{ typeof(CustomRenderTexture), "UnityEditor.CustomRenderTextureEditor"},
{ typeof(Material), "UnityEditor.MaterialEditor" },
};

protected virtual void LoadInspectorFor(Type typeForEditor, Object[] targets)
void CreateDefaultTextureEditor()
{
string editorTypeName;
if (defaultTextureInspectors.TryGetValue(typeForEditor, out editorTypeName))
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
var editorType = assembly.GetType(defaultTextureEditorTypeName);
if (editorType != null)
{
var editorType = assembly.GetType(editorTypeName);
if (editorType != null)
{
Editor.CreateCachedEditor(targets, editorType, ref defaultTextureEditor);
Editor.CreateCachedEditor(targets, editorType, ref defaultTextureEditor);

if (variantEditor != null)
(variantEditor as MixtureVariantInspector).SetDefaultTextureEditor(defaultTextureEditor);
if (variantEditor != null)
(variantEditor as MixtureVariantInspector).SetDefaultTextureEditor(defaultTextureEditor);

return ;
}
return;
}
}

throw new Exception($"Cannot load default texture editor: {defaultTextureEditorTypeName}");
}

protected virtual void OnDisable()
Expand All @@ -192,7 +184,7 @@ protected virtual void OnDisable()

// This block of functions allow us to use the default behavior of the texture inspector instead of re-writing
// the preview / static icon code for each texture type, we use the one from the default texture inspector.
Editor GetPreviewEditor() => variantEditor ?? defaultTextureEditor ?? this;
Editor GetPreviewEditor() => variantEditor ?? defaultTextureEditor;
public override string GetInfoString() => GetPreviewEditor().GetInfoString();
public override void ReloadPreviewInstances() => GetPreviewEditor().ReloadPreviewInstances();
public override bool RequiresConstantRepaint() => GetPreviewEditor().RequiresConstantRepaint();
Expand Down Expand Up @@ -393,16 +385,14 @@ public override Texture2D RenderStaticPreview(string assetPath, Object[] subAsse
[CustomEditor(typeof(Texture2D), false)]
class MixtureInspectorTexture2D : MixtureEditor
{
protected override void OnEnable()
{
base.OnEnable();
LoadInspectorFor(typeof(Texture2D), targets);
}
protected override string defaultTextureEditorTypeName => "UnityEditor.TextureInspector";
}

[CustomEditor(typeof(Texture2DArray), false)]
class MixtureInspectorTexture2DArray : MixtureEditor
{
protected override string defaultTextureEditorTypeName => "UnityEditor.Texture2DArrayInspector";

Texture2DArray array;
int slice;

Expand Down Expand Up @@ -439,11 +429,12 @@ class MixtureInspectorTexture3D : MixtureEditor
Texture3D volume;
int slice = 0;

protected override string defaultTextureEditorTypeName => "UnityEditor.Texture3DInspector";

protected override void OnEnable()
{
base.OnEnable();
volume = target as Texture3D;
LoadInspectorFor(typeof(Texture3D), targets);
}

public override void OnInspectorGUI()
Expand Down Expand Up @@ -474,11 +465,12 @@ class MixtureInspectorTextureCube : MixtureEditor
Cubemap cubemap;
int slice;

protected override string defaultTextureEditorTypeName => "UnityEditor.CubemapInspector";

protected override void OnEnable()
{
base.OnEnable();
cubemap = target as Cubemap;
LoadInspectorFor(typeof(Cubemap), targets);
}
}

Expand All @@ -488,10 +480,11 @@ class RealtimeMixtureInspector : MixtureEditor
CustomRenderTexture crt;
bool isMixture;

protected override string defaultTextureEditorTypeName => "UnityEditor.CustomRenderTextureEditor";

protected override void OnEnable()
{
base.OnEnable();
base.LoadInspectorFor(typeof(CustomRenderTexture), targets);
crt = target as CustomRenderTexture;

ReloadPreviewInstances();
Expand Down

0 comments on commit 8e917ef

Please sign in to comment.