Skip to content

Commit e3ad7e6

Browse files
committed
fix: Fixed not working custom editor for generic MonoBehaviours with new versions of Odin Inspector
1 parent 73af6e8 commit e3ad7e6

10 files changed

+196
-40
lines changed

.GenericUnityInternals/GenericUnityEditorInternals/GenericUnityEditorInternals.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
<Reference Include="System.Core" />
4040
<Reference Include="System.Xml" />
4141
<Reference Include="UnityEngine.CoreModule">
42-
<HintPath>C:\Program Files\Unity Editors\2021.2.2f1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll</HintPath>
42+
<HintPath>C:\Program Files\Unity Editors\2021.2.6f1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll</HintPath>
4343
</Reference>
4444
<Reference Include="UnityEditor.CoreModule">
45-
<HintPath>c:\Program Files\Unity Editors\2021.2.2f1\Editor\Data\Managed\UnityEngine\UnityEditor.CoreModule.dll</HintPath>
45+
<HintPath>c:\Program Files\Unity Editors\2021.2.6f1\Editor\Data\Managed\UnityEngine\UnityEditor.CoreModule.dll</HintPath>
4646
</Reference>
4747
<Reference Include="UnityEngine.IMGUIModule">
48-
<HintPath>c:\Program Files\Unity Editors\2021.2.2f1\Editor\Data\Managed\UnityEngine\UnityEngine.IMGUIModule.dll</HintPath>
48+
<HintPath>c:\Program Files\Unity Editors\2021.2.6f1\Editor\Data\Managed\UnityEngine\UnityEngine.IMGUIModule.dll</HintPath>
4949
</Reference>
5050
<Reference Include="GenericUnityObjects">
5151
<HintPath>..\..\..\..\Library\ScriptAssemblies\GenericUnityObjects.dll</HintPath>
@@ -61,7 +61,6 @@
6161
<Compile Include="EditorGUIHelper.cs" />
6262
<Compile Include="EditorGUILayoutHelper.cs" />
6363
<Compile Include="GenericMonoImporterInspector.cs" />
64-
<Compile Include="GenericHeaderEditor.cs" />
6564
<Compile Include="GenericTypeHelper.cs" />
6665
<Compile Include="GenericUnityEventDrawer.cs" />
6766
<Compile Include="ObjectFieldHelper.cs" />
Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,93 @@
1-
namespace GenericUnityObjects.UnityEditorInternals
1+
namespace GenericUnityObjects.Editor
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Reflection;
6+
using GenericUnityObjects.Util;
57
using UnityEditor;
6-
using Util;
8+
using UnityEngine;
79
using Object = UnityEngine.Object;
10+
11+
#if ODIN_INSPECTOR
12+
using Sirenix.OdinInspector.Editor;
13+
#endif
814

915
/// <summary>
1016
/// An extension of Editor that changes name of <see cref="GenericScriptableObject"/> assets in the Inspector header.
1117
/// For all other assets, it draws header like before.
1218
/// </summary>
13-
public class GenericHeaderEditor : Editor
19+
public class GenericHeaderEditor :
20+
#if ODIN_INSPECTOR
21+
OdinEditor
22+
#else
23+
Editor
24+
#endif
1425
{
1526
private static readonly Dictionary<TargetInfo, string> _targetTitlesCache = new Dictionary<TargetInfo, string>();
1627
private static readonly Dictionary<Type, string> _typeNamesCache = new Dictionary<Type, string>();
1728

18-
internal override string targetTitle => GetTitle();
29+
private static Func<Editor, string, Rect> _drawHeaderGUI;
30+
31+
private static Func<Editor, string, Rect> DrawHeaderGUI
32+
{
33+
get
34+
{
35+
if (_drawHeaderGUI == null)
36+
{
37+
var drawHeaderMethod = typeof(Editor).GetMethod("DrawHeaderGUI",
38+
BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(Editor), typeof(string) },
39+
null);
40+
41+
_drawHeaderGUI = (Func<Editor, string, Rect>) Delegate.CreateDelegate(typeof(Func<Editor, string, Rect>), drawHeaderMethod);
42+
}
43+
44+
return _drawHeaderGUI;
45+
}
46+
}
47+
48+
private static Func<Object, string> _getTypeName;
49+
50+
private static Func<Object, string> GetTypeName
51+
{
52+
get
53+
{
54+
if (_getTypeName == null)
55+
{
56+
var getTypeNameMethod = typeof(ObjectNames).GetMethod("GetTypeName",
57+
BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(Object) }, null);
58+
59+
_getTypeName = (Func<Object, string>) Delegate.CreateDelegate(typeof(Func<Object, string>), getTypeNameMethod);
60+
}
61+
62+
return _getTypeName;
63+
}
64+
}
65+
66+
protected override void OnHeaderGUI()
67+
{
68+
DrawHeaderGUI(this, GetTitle());
69+
}
1970

2071
private string GetTitle()
2172
{
2273
Type genericType = target.GetType().BaseType;
2374

24-
if (genericType?.IsGenericType != true)
25-
return base.targetTitle;
26-
27-
return m_Targets.Length == 1 || ! m_AllowMultiObjectAccess
75+
return targets.Length == 1
2876
? GetOneTitle(genericType)
2977
: GetMixedTitle(genericType);
3078
}
3179

3280
private string GetOneTitle(Type genericType)
3381
{
82+
if (genericType?.IsGenericType != true)
83+
return ObjectNames.GetInspectorTitle(target);
84+
3485
var targetInfo = new TargetInfo(target);
3586

3687
if (_targetTitlesCache.TryGetValue(targetInfo, out string title))
3788
return title;
3889

39-
string typeName = GetTypeName(genericType);
90+
string typeName = GetGenericTypeName(genericType);
4091

4192
// target.name is empty when a new asset is created interactively and has not been named yet.
4293
if (string.IsNullOrEmpty(target.name))
@@ -49,10 +100,13 @@ private string GetOneTitle(Type genericType)
49100

50101
private string GetMixedTitle(Type genericType)
51102
{
52-
return $"{m_Targets.Length} objects of type {GetTypeName(genericType)}";
103+
if (genericType?.IsGenericType != true)
104+
return targets.Length + " " + ObjectNames.NicifyVariableName(GetTypeName(target)) + "s";
105+
106+
return $"{targets.Length} objects of type {GetGenericTypeName(genericType)}";
53107
}
54108

55-
private static string GetTypeName(Type genericType)
109+
private static string GetGenericTypeName(Type genericType)
56110
{
57111
if (_typeNamesCache.TryGetValue(genericType, out string typeName))
58112
return typeName;

Editor/Drawers/GenericHeaderEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Drawers/GenericUnityObjectEditor.cs renamed to Editor/Drawers/GenericScriptableObjectEditor.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
namespace GenericUnityObjects.Editor
22
{
3-
using GenericUnityObjects;
4-
using UnityEditor;
5-
using UnityEditorInternals;
6-
using UnityEngine;
7-
8-
#if EASY_BUTTONS
93
using EasyButtons.Editor;
4+
using UnityEditor;
5+
6+
#if ODIN_INSPECTOR
7+
using Sirenix.OdinInspector;
8+
using Sirenix.OdinInspector.Editor;
9+
using Sirenix.Utilities;
1010
#endif
1111

1212
#if ! DISABLE_GENERIC_OBJECT_EDITOR
13-
[CanEditMultipleObjects]
14-
[CustomEditor(typeof(MonoBehaviour), true)]
15-
internal class MonoBehaviourEditor : GenericUnityObjectEditor { }
16-
1713
[CanEditMultipleObjects]
1814
[CustomEditor(typeof(GenericScriptableObject), true)]
19-
internal class GenericScriptableObjectEditor : GenericUnityObjectEditor { }
2015
#endif
21-
22-
internal class GenericUnityObjectEditor : GenericHeaderEditor
16+
public class GenericScriptableObjectEditor : GenericHeaderEditor
2317
{
2418
private GenericUnityObjectHelper _helper;
2519

2620
#if EASY_BUTTONS
2721
private ButtonsDrawer _buttonsDrawer;
2822
#endif
29-
30-
private void OnEnable()
23+
24+
protected override void OnEnable()
3125
{
26+
base.OnEnable();
27+
3228
_helper = new GenericUnityObjectHelper(target);
3329

3430
#if EASY_BUTTONS
3531
_buttonsDrawer = new ButtonsDrawer(target);
3632
#endif
3733
}
38-
34+
3935
public override void OnInspectorGUI()
4036
{
4137
if (target == null)
@@ -46,27 +42,33 @@ public override void OnInspectorGUI()
4642

4743
serializedObject.UpdateIfRequiredOrScript();
4844

45+
#if ODIN_INSPECTOR
46+
if (target == null || GlobalConfig<GeneralDrawerConfig>.Instance.ShowMonoScriptInEditor && !target.GetType().IsDefined(typeof(HideMonoScriptAttribute), true))
47+
#endif
48+
_helper.DrawMonoScript(serializedObject.FindProperty("m_Script"));
49+
50+
#if ODIN_INSPECTOR
51+
bool previousValue = ForceHideMonoScriptInEditor;
52+
ForceHideMonoScriptInEditor = true;
53+
base.OnInspectorGUI();
54+
ForceHideMonoScriptInEditor = previousValue;
55+
#else
4956
SerializedProperty iterator = serializedObject.GetIterator();
5057

5158
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false)
5259
{
53-
if (iterator.propertyPath == "m_Script")
54-
{
55-
_helper.DrawMonoScript(iterator);
56-
}
57-
else
58-
{
60+
if (iterator.propertyPath != "m_Script")
5961
EditorGUILayout.PropertyField(iterator, true, null);
60-
}
6162
}
63+
#endif
6264

6365
#if EASY_BUTTONS
6466
_buttonsDrawer.DrawButtons(targets);
6567
#endif
6668

6769
serializedObject.ApplyModifiedProperties();
6870
}
69-
71+
7072
private void DrawMissingScript()
7173
{
7274
using (new EditorGUI.DisabledScope(true))

Editor/Drawers/GenericScriptableObjectEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Drawers/MonoBehaviourEditor.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
namespace GenericUnityObjects.Editor
2+
{
3+
using System.Reflection;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
#if EASY_BUTTONS
8+
using EasyButtons.Editor;
9+
#endif
10+
11+
#if ODIN_INSPECTOR
12+
using Sirenix.OdinInspector;
13+
using Sirenix.OdinInspector.Editor;
14+
using Sirenix.Utilities;
15+
#endif
16+
17+
#if ! DISABLE_GENERIC_OBJECT_EDITOR
18+
[CanEditMultipleObjects]
19+
[CustomEditor(typeof(MonoBehaviour), true)]
20+
[InitializeOnLoad]
21+
#endif
22+
public class MonoBehaviourEditor : OdinEditor
23+
{
24+
static MonoBehaviourEditor()
25+
{
26+
// a workaround for bug https://bitbucket.org/sirenix/odin-inspector/issues/833/customeditor-with-editorforchildclasses
27+
var odinEditorTypeField = typeof(InspectorTypeDrawingConfig).GetField("odinEditorType", BindingFlags.Static | BindingFlags.NonPublic);
28+
odinEditorTypeField.SetValue(null, typeof(MonoBehaviourEditor));
29+
}
30+
31+
private GenericUnityObjectHelper _helper;
32+
33+
#if EASY_BUTTONS
34+
private ButtonsDrawer _buttonsDrawer;
35+
#endif
36+
37+
protected override void OnEnable()
38+
{
39+
base.OnEnable();
40+
41+
_helper = new GenericUnityObjectHelper(target);
42+
43+
#if EASY_BUTTONS
44+
_buttonsDrawer = new ButtonsDrawer(target);
45+
#endif
46+
}
47+
48+
public override void OnInspectorGUI()
49+
{
50+
if (target == null)
51+
{
52+
DrawMissingScript();
53+
return;
54+
}
55+
56+
serializedObject.UpdateIfRequiredOrScript();
57+
58+
#if ODIN_INSPECTOR
59+
if (target == null || GlobalConfig<GeneralDrawerConfig>.Instance.ShowMonoScriptInEditor && !target.GetType().IsDefined(typeof(HideMonoScriptAttribute), true))
60+
#endif
61+
_helper.DrawMonoScript(serializedObject.FindProperty("m_Script"));
62+
63+
#if ODIN_INSPECTOR
64+
bool previousValue = ForceHideMonoScriptInEditor;
65+
ForceHideMonoScriptInEditor = true;
66+
base.OnInspectorGUI();
67+
ForceHideMonoScriptInEditor = previousValue;
68+
#else
69+
SerializedProperty iterator = serializedObject.GetIterator();
70+
71+
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false)
72+
{
73+
if (iterator.propertyPath != "m_Script")
74+
EditorGUILayout.PropertyField(iterator, true, null);
75+
}
76+
#endif
77+
78+
#if EASY_BUTTONS
79+
_buttonsDrawer.DrawButtons(targets);
80+
#endif
81+
82+
serializedObject.ApplyModifiedProperties();
83+
}
84+
85+
private void DrawMissingScript()
86+
{
87+
using (new EditorGUI.DisabledScope(true))
88+
{
89+
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
90+
}
91+
}
92+
}
93+
}

Editor/GenericUnityObjects.Editor.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"precompiledReferences": [
2121
"GenericUnityEditorInternals.dll",
2222
"UnityEditorInternals.dll",
23-
"Sirenix.OdinInspector.Editor.dll"
23+
"Sirenix.OdinInspector.Editor.dll",
24+
"Sirenix.Utilities.dll",
25+
"Sirenix.OdinInspector.Attributes.dll"
2426
],
2527
"autoReferenced": true,
2628
"defineConstraints": [],

GenericUnityEditorInternals.dll

-1 KB
Binary file not shown.

GenericUnityEditorInternals.pdb

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)