Skip to content

Commit d84f8ac

Browse files
authored
Merge pull request #48 from arimger/develop
Develop - 0.11.4
2 parents 4cd14d0 + 6a2f286 commit d84f8ac

File tree

13 files changed

+247
-21
lines changed

13 files changed

+247
-21
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.11.4 [17.07.2022]
2+
3+
### Added:
4+
- ToolboxWizard, additional Toolbox-based equivalent for the ScriptableWizard class
5+
6+
### Changed:
7+
- Fix caching FieldInfo for [SerializeReference] properties
8+
19
## 0.11.3 [05.06.2022]
210

311
### Added:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Toolbox.Editor.Tests")]

Assets/Editor Toolbox/Editor/AssemblyInfo.cs.meta

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

Assets/Editor Toolbox/Editor/ToolboxDrawerModule.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ internal static void InitializeModule()
1919
}
2020

2121

22-
private readonly static Type decoratorDrawerBase = typeof(ToolboxDecoratorDrawer<>);
23-
private readonly static Type conditionDrawerBase = typeof(ToolboxConditionDrawer<>);
24-
private readonly static Type selfPropertyDrawerBase = typeof(ToolboxSelfPropertyDrawer<>);
25-
private readonly static Type listPropertyDrawerBase = typeof(ToolboxListPropertyDrawer<>);
22+
private static readonly Type decoratorDrawerBase = typeof(ToolboxDecoratorDrawer<>);
23+
private static readonly Type conditionDrawerBase = typeof(ToolboxConditionDrawer<>);
24+
private static readonly Type selfPropertyDrawerBase = typeof(ToolboxSelfPropertyDrawer<>);
25+
private static readonly Type listPropertyDrawerBase = typeof(ToolboxListPropertyDrawer<>);
2626

27-
private readonly static Dictionary<Type, ToolboxDecoratorDrawerBase> decoratorDrawers = new Dictionary<Type, ToolboxDecoratorDrawerBase>();
28-
private readonly static Dictionary<Type, ToolboxConditionDrawerBase> conditionDrawers = new Dictionary<Type, ToolboxConditionDrawerBase>();
29-
private readonly static Dictionary<Type, ToolboxPropertyDrawerBase> selfPropertyDrawers = new Dictionary<Type, ToolboxPropertyDrawerBase>();
30-
private readonly static Dictionary<Type, ToolboxPropertyDrawerBase> listPropertyDrawers = new Dictionary<Type, ToolboxPropertyDrawerBase>();
27+
private static readonly Dictionary<Type, ToolboxDecoratorDrawerBase> decoratorDrawers = new Dictionary<Type, ToolboxDecoratorDrawerBase>();
28+
private static readonly Dictionary<Type, ToolboxConditionDrawerBase> conditionDrawers = new Dictionary<Type, ToolboxConditionDrawerBase>();
29+
private static readonly Dictionary<Type, ToolboxPropertyDrawerBase> selfPropertyDrawers = new Dictionary<Type, ToolboxPropertyDrawerBase>();
30+
private static readonly Dictionary<Type, ToolboxPropertyDrawerBase> listPropertyDrawers = new Dictionary<Type, ToolboxPropertyDrawerBase>();
3131

3232
/// <summary>
3333
/// Collection of specific drawers mapped to selected (picked) object types.
3434
/// </summary>
35-
private readonly static Dictionary<Type, ToolboxTargetTypeDrawer> targetTypeDrawers = new Dictionary<Type, ToolboxTargetTypeDrawer>();
35+
private static readonly Dictionary<Type, ToolboxTargetTypeDrawer> targetTypeDrawers = new Dictionary<Type, ToolboxTargetTypeDrawer>();
3636

3737
/// <summary>
3838
/// Collection of currently cached handlers mapped to a unique property key.

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using UnityEditor;
88
using Object = UnityEngine.Object;
99

10-
[assembly: InternalsVisibleTo("Toolbox.Editor.Tests")]
11-
1210
namespace Toolbox.Editor
1311
{
1412
public static partial class PropertyUtility
@@ -27,7 +25,9 @@ internal static bool HasModifedProperties(this SerializedProperty property)
2725
internal static string GetPropertyHashKey(this SerializedProperty property)
2826
{
2927
var hash = property.serializedObject.GetHashCode();
30-
return string.Format("{0}.{1}", hash, property.propertyPath);
28+
return property.propertyType != SerializedPropertyType.ManagedReference
29+
? $"{hash}.{property.propertyPath}"
30+
: $"{hash}.{property.propertyPath}.{property.managedReferenceFieldTypename}";
3131
}
3232

3333
/// <summary>
@@ -36,7 +36,9 @@ internal static string GetPropertyHashKey(this SerializedProperty property)
3636
internal static string GetPropertyTypeKey(this SerializedProperty property)
3737
{
3838
var type = property.serializedObject.targetObject.GetType();
39-
return string.Format("{0}.{1}", type, property.propertyPath);
39+
return property.propertyType != SerializedPropertyType.ManagedReference
40+
? $"{type}.{property.propertyPath}"
41+
: $"{type}.{property.propertyPath}.{property.managedReferenceFieldTypename}";
4042
}
4143

4244
/// <summary>
@@ -253,7 +255,7 @@ internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Typ
253255

254256
internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
255257
{
256-
return GetFieldInfoFromProperty(target.GetType(), property.propertyPath, out propertyType);
258+
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
257259
}
258260

259261
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
@@ -265,14 +267,15 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
265267
return null;
266268
}
267269

268-
return GetFieldInfoFromProperty(classType, property.propertyPath, out type);
270+
return GetFieldInfoFromProperty(property, out type, classType);
269271
}
270272

271-
public static FieldInfo GetFieldInfoFromProperty(Type host, string fieldPath, out Type type)
273+
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
272274
{
273275
FieldInfo field = null;
274276
type = host;
275277

278+
var fieldPath = property.propertyPath;
276279
var members = GetPropertyFieldTree(fieldPath, false);
277280
for (var i = 0; i < members.Length; i++)
278281
{
@@ -287,10 +290,21 @@ public static FieldInfo GetFieldInfoFromProperty(Type host, string fieldPath, ou
287290
continue;
288291
}
289292

293+
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
290294
FieldInfo foundField = null;
291295
for (var currentType = type; foundField == null && currentType != null; currentType = currentType.BaseType)
292296
{
293-
foundField = currentType.GetField(member, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
297+
foundField = currentType.GetField(member, fieldFlags);
298+
//NOTE: [SerializeReference] detected? If so we need to check dynamically cached type
299+
if (foundField == null)
300+
{
301+
var parent = property.GetParent();
302+
if (parent != null && parent.propertyType == SerializedPropertyType.ManagedReference)
303+
{
304+
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(parent.managedReferenceFullTypename, out var parentType);
305+
foundField = parentType.GetField(member, fieldFlags);
306+
}
307+
}
294308
}
295309

296310
if (foundField == null)

Assets/Editor Toolbox/Editor/Windows.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Wizards
5+
{
6+
using Editor = UnityEditor.Editor;
7+
8+
public class ToolboxWizard : EditorWindow
9+
{
10+
private Editor targetEditor;
11+
12+
private Vector2 scrollPosition;
13+
14+
private void OnDestroy()
15+
{
16+
DestroyImmediate(targetEditor);
17+
}
18+
19+
private void OnGUI()
20+
{
21+
using (var scrollView = new EditorGUILayout.ScrollViewScope(scrollPosition))
22+
{
23+
scrollPosition = scrollView.scrollPosition;
24+
EditorGUI.BeginChangeCheck();
25+
OnWizardGui();
26+
if (EditorGUI.EndChangeCheck())
27+
{
28+
OnWizardUpdate();
29+
}
30+
}
31+
32+
using (new EditorGUILayout.VerticalScope())
33+
{
34+
GUILayout.FlexibleSpace();
35+
using (new EditorGUILayout.HorizontalScope())
36+
{
37+
GUILayout.FlexibleSpace();
38+
HandleOtherButtons();
39+
GUI.enabled = IsValid;
40+
if (HandleCreateButton())
41+
{
42+
OnWizardCreate();
43+
Close();
44+
GUIUtility.ExitGUI();
45+
}
46+
47+
GUI.enabled = true;
48+
}
49+
50+
GUILayout.Space(5);
51+
}
52+
}
53+
54+
private void PrepareEditor()
55+
{
56+
if (targetEditor != null)
57+
{
58+
return;
59+
}
60+
61+
targetEditor = Editor.CreateEditor(this);
62+
targetEditor.hideFlags = HideFlags.HideAndDontSave;
63+
OnWizardUpdate();
64+
}
65+
66+
protected virtual void OnWizardCreate()
67+
{ }
68+
69+
protected virtual void OnWizardUpdate()
70+
{ }
71+
72+
protected virtual void OnWizardGui()
73+
{
74+
PrepareEditor();
75+
targetEditor.OnInspectorGUI();
76+
}
77+
78+
protected virtual bool HandleCreateButton()
79+
{
80+
return GUILayout.Button("Create", GUILayout.MinWidth(100));
81+
}
82+
83+
protected virtual void HandleOtherButtons()
84+
{ }
85+
86+
public static T DisplayWizard<T>(string title) where T : ToolboxWizard
87+
{
88+
return GetWindow<T>(true, title);
89+
}
90+
91+
protected bool IsValid { get; set; } = true;
92+
}
93+
}

Assets/Editor Toolbox/Editor/Windows/ToolboxWizard.cs.meta

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

Assets/Editor Toolbox/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.arimger.editor-toolbox",
33
"displayName": "Editor Toolbox",
4-
"version": "0.11.3",
4+
"version": "0.11.4",
55
"unity": "2018.1",
66
"description": "Tools, custom attributes, drawers, hierarchy overlay, and other extensions for the Unity Editor.",
77
"keywords": [
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Toolbox.Editor.Wizards;
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
public class SampleWizard : ToolboxWizard
7+
{
8+
[SerializeField, InLineEditor]
9+
private GameObject prefab;
10+
[SerializeField]
11+
private string targetName;
12+
13+
private bool hasInvalidName;
14+
15+
[MenuItem("GameObject/Sample Wizard")]
16+
public static void CreateWizard()
17+
{
18+
DisplayWizard<SampleWizard>("Create GameObject");
19+
}
20+
21+
protected override void OnWizardCreate()
22+
{
23+
GameObject go;
24+
if (prefab != null)
25+
{
26+
go = Instantiate(prefab);
27+
go.name = targetName;
28+
}
29+
else
30+
{
31+
go = new GameObject(targetName);
32+
}
33+
34+
Selection.activeObject = go;
35+
}
36+
37+
protected override void OnWizardUpdate()
38+
{
39+
hasInvalidName = string.IsNullOrEmpty(targetName);
40+
}
41+
42+
protected override void OnWizardGui()
43+
{
44+
base.OnWizardGui();
45+
if (hasInvalidName)
46+
{
47+
EditorGUILayout.HelpBox("Name is invalid", MessageType.Error, true);
48+
}
49+
}
50+
51+
protected override void HandleOtherButtons()
52+
{
53+
//you can draw more buttons here
54+
}
55+
}

0 commit comments

Comments
 (0)