Skip to content

Commit 01fb97e

Browse files
authored
Merge pull request #57 from arimger/develop
Develop - 0.11.9
2 parents ef5e83d + e0da418 commit 01fb97e

26 files changed

Lines changed: 525 additions & 118 deletions

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 0.11.9 [22.10.2022]
2+
3+
### Added:
4+
- ScriptableObjectCreationWizard (possibility to create multiple ScriptableObjects at once)
5+
6+
### Changed:
7+
- Fix displaying empty fields marked with the [SerializeReference] attribute
8+
- Fix Scene data serialization in the SerializedScene class
9+
- Fix retrieving FieldInfo from nested [SerializeReference]-based fields
10+
111
## 0.11.8 [15.09.2022]
212

313
### Added:

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedSceneDrawer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,24 @@ public static SceneData GetSceneData(SerializedProperty property)
103103
var sceneAsset = property.objectReferenceValue as SceneAsset;
104104
var scenePath = AssetDatabase.GetAssetPath(sceneAsset);
105105
var sceneGuid = AssetDatabase.AssetPathToGUID(scenePath);
106+
var sceneIndex = -1;
106107
for (var i = 0; i < EditorBuildSettings.scenes.Length; i++)
107108
{
108109
var sceneSettings = EditorBuildSettings.scenes[i];
110+
var isEnabled = sceneSettings.enabled;
111+
if (isEnabled)
112+
{
113+
sceneIndex++;
114+
}
115+
109116
var guid = sceneSettings.guid;
110117
if (guid.Equals(new GUID(sceneGuid)))
111118
{
112-
sceneData.index = i;
113-
sceneData.enabled = sceneSettings.enabled;
119+
sceneData.index = isEnabled ? sceneIndex : -1;
120+
sceneData.enabled = isEnabled;
114121
sceneData.guid = guid;
115122
sceneData.inBuild = true;
123+
break;
116124
}
117125
}
118126

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13-
private const float neededLabelWidth = 100.0f;
1413
private const float labelWidthOffset = -80.0f;
1514

1615
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
@@ -25,20 +24,21 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2524

2625
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
2726
{
28-
property.GetFieldInfo(out Type propertyType);
27+
var fieldInfo = property.GetFieldInfo(out _);
28+
var fieldType = property.GetProperType(fieldInfo);
2929
var candidateType = attribute.ParentType;
3030
if (candidateType != null)
3131
{
32-
if (propertyType.IsAssignableFrom(candidateType))
32+
if (fieldType.IsAssignableFrom(candidateType))
3333
{
3434
return candidateType;
3535
}
3636

3737
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
38-
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
38+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{fieldType}'");
3939
}
4040

41-
return propertyType;
41+
return fieldType;
4242
}
4343

4444
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)
@@ -89,12 +89,9 @@ private Rect PrepareTypePropertyPosition(in Rect labelPosition, in Rect inputPos
8989
if (isPropertyExpanded)
9090
{
9191
//property is expanded and we have place to move it to the next row
92-
if (labelWidth < neededLabelWidth)
93-
{
94-
position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
95-
position = EditorGUI.IndentedRect(position);
96-
return position;
97-
}
92+
position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
93+
position = EditorGUI.IndentedRect(position);
94+
return position;
9895
}
9996

10097
//adjust position to already rendered label

Assets/Editor Toolbox/Editor/Internal/PropertyScope.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ private void HandleEvents(Rect rect)
3333

3434
private void TryDrawLabel(Rect rect, GUIContent label)
3535
{
36-
LabelRect = rect;
3736
InputRect = rect;
38-
if (property.hasChildren)
37+
var size = EditorStyles.label.CalcSize(label);
38+
rect.xMax = rect.xMin + size.x;
39+
rect.xMax += EditorGuiUtility.IndentSize;
40+
rect.xMax += EditorGuiUtility.SpacingSize;
41+
LabelRect = rect;
42+
if (property.hasVisibleChildren)
3943
{
40-
var size = EditorStyles.label.CalcSize(label);
41-
size.x = Mathf.Max(EditorGuiUtility.FoldoutOffset, size.x);
42-
rect.xMax = rect.xMin + size.x;
43-
LabelRect = rect;
44-
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
44+
var labelRect = LabelRect;
45+
labelRect.xMax += EditorGuiUtility.FoldoutSize;
46+
LabelRect = labelRect;
47+
property.isExpanded = EditorGUI.Foldout(labelRect, property.isExpanded, label, true);
4548
}
4649
else
4750
{
48-
EditorGUI.LabelField(rect, label);
51+
EditorGUI.LabelField(LabelRect, label);
4952
}
5053
}
5154

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using UnityEditor;
44
using UnityEngine;
5+
using Object = UnityEngine.Object;
56

67
namespace Toolbox.Editor
78
{
@@ -206,16 +207,16 @@ public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, r
206207
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
207208
{
208209
rect = EditorGUI.PrefixLabel(rect, label);
209-
210+
210211
var fieldWidth = EditorGUIUtility.fieldWidth;
211212
var minFieldRect = new Rect(rect.xMin, rect.y, fieldWidth, rect.height);
212213
var maxFieldRect = new Rect(rect.xMax - fieldWidth, rect.y, fieldWidth, rect.height);
213214

214215
//set slider rect between min and max fields + additional padding
215216
var spacing = 8.0f;
216217
var sliderRect = Rect.MinMaxRect(minFieldRect.xMax + spacing,
217-
rect.yMin,
218-
maxFieldRect.xMin - spacing,
218+
rect.yMin,
219+
maxFieldRect.xMin - spacing,
219220
rect.yMax);
220221

221222
EditorGUI.BeginChangeCheck();
@@ -604,5 +605,31 @@ public static void CloseProperty()
604605
{
605606
EditorGUI.EndProperty();
606607
}
608+
609+
/// <summary>
610+
/// Displays all visible children (except the default script property) associated to the given <see cref="Object"/>.
611+
/// This method doesn't support Toolbox-based features.
612+
/// </summary>
613+
public static void DrawObjectProperties(Object instance)
614+
{
615+
using (SerializedObject serializedObject = new SerializedObject(instance))
616+
{
617+
var property = serializedObject.GetIterator();
618+
if (property.NextVisible(true))
619+
{
620+
if (!PropertyUtility.IsDefaultScriptProperty(property))
621+
{
622+
DrawNativeProperty(property.Copy());
623+
}
624+
625+
while (property.NextVisible(false))
626+
{
627+
DrawNativeProperty(property.Copy());
628+
}
629+
}
630+
631+
serializedObject.ApplyModifiedProperties();
632+
}
633+
}
607634
}
608635
}

Assets/Editor Toolbox/Editor/ToolboxPropertyHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ private void DrawProperty(SerializedProperty property, GUIContent label)
286286
//draw target property using the associated type drawer
287287
ToolboxDrawerModule.GetTargetTypeDrawer(type)?.OnGui(property, label);
288288
}
289+
290+
return;
289291
}
290-
else
291-
{
292-
OnGuiDefault(property, label);
293-
}
292+
293+
OnGuiDefault(property, label);
294294
}
295295

296296
private void BeginDecoratorDrawers(PropertyCondition conditionState = PropertyCondition.Valid)

Assets/Editor Toolbox/Editor/Utilities/EditorGuiUtility.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Toolbox.Editor
1212
/// </summary>
1313
internal static class EditorGuiUtility
1414
{
15+
private const float indentPerLevel = 15.0f;
16+
1517
private static readonly Dictionary<string, Texture2D> loadedTextures = new Dictionary<string, Texture2D>();
1618

1719

@@ -99,6 +101,9 @@ public static Texture GetHelpIcon(MessageType messageType)
99101
}
100102

101103

102-
public static float FoldoutOffset { get; internal set; } = 28.0f;
104+
public static float FoldoutSize { get; internal set; } = 15.0f;
105+
public static float SpacingSize => EditorGUIUtility.standardVerticalSpacing;
106+
public static float HeightSize => EditorGUIUtility.singleLineHeight;
107+
public static float IndentSize => EditorGUI.indentLevel * indentPerLevel;
103108
}
104109
}

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace Toolbox.Editor
1010
{
1111
public static partial class PropertyUtility
1212
{
13+
//NOTE: last non-reflection implementation was ok but support for [SerializeReference] makes it a bit slow
14+
// unfortunately UnityEditor.ScriptAttributeUtility.GetFieldInfoFromProperty is internal so we have to retrive it using reflection
15+
private static readonly MethodInfo getGetFieldInfoFromPropertyMethod =
16+
ReflectionUtility.GetEditorMethod("UnityEditor.ScriptAttributeUtility", "GetFieldInfoFromProperty",
17+
BindingFlags.NonPublic | BindingFlags.Static);
18+
1319
/// <summary>
1420
/// Indicates if the property has all changes applied and can be safely used for reflection-based features.
1521
/// </summary>
@@ -249,22 +255,20 @@ public static Type GetScriptTypeFromProperty(SerializedProperty property)
249255
return scriptInstance.GetClass();
250256
}
251257

252-
253258
public static FieldInfo GetFieldInfo(this SerializedProperty property)
254259
{
255260
return GetFieldInfo(property, out _);
256261
}
257262

258263
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType)
259264
{
260-
return GetFieldInfoFromProperty(property, out propertyType);
261-
}
262-
263-
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
264-
{
265-
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
265+
var parameters = new object[] { property, null };
266+
var result = getGetFieldInfoFromPropertyMethod.Invoke(null, parameters) as FieldInfo;
267+
propertyType = parameters[1] as Type;
268+
return result;
266269
}
267270

271+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
268272
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
269273
{
270274
var classType = GetScriptTypeFromProperty(property);
@@ -277,8 +281,11 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
277281
return GetFieldInfoFromProperty(property, out type, classType);
278282
}
279283

284+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
280285
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
281286
{
287+
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
288+
282289
FieldInfo field = null;
283290
type = host;
284291

@@ -297,7 +304,6 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
297304
continue;
298305
}
299306

300-
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
301307
FieldInfo foundField = null;
302308
for (var currentType = type; foundField == null && currentType != null; currentType = currentType.BaseType)
303309
{
@@ -383,7 +389,7 @@ public static SerializedProperty GetSize(this SerializedProperty array)
383389

384390
public static T GetAttribute<T>(SerializedProperty property) where T : Attribute
385391
{
386-
return GetAttribute<T>(property, GetFieldInfoFromProperty(property, out _));
392+
return GetAttribute<T>(property, GetFieldInfo(property, out _));
387393
}
388394

389395
public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute
@@ -393,7 +399,7 @@ public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo
393399

394400
public static T[] GetAttributes<T>(SerializedProperty property) where T : Attribute
395401
{
396-
return GetAttributes<T>(property, GetFieldInfoFromProperty(property, out _));
402+
return GetAttributes<T>(property, GetFieldInfo(property, out _));
397403
}
398404

399405
public static T[] GetAttributes<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute

0 commit comments

Comments
 (0)