Skip to content

Commit dd74c5b

Browse files
authored
Merge pull request #49 from arimger/develop
Develop - 0.11.5
2 parents b7ff262 + ad8836f commit dd74c5b

16 files changed

Lines changed: 153 additions & 38 deletions

File tree

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.11.5 [18.08.2022]
2+
3+
### Added:
4+
- Possibility to hide the m_Script property while using the InLineEditorAttribute (HideScript = true)
5+
- Possibility to pick base type in the ReferencePickerAttribute
6+
- Possibility to pick display options in the ReferencePickerAttribute
7+
8+
### Changed:
9+
- Fix displaying custom labels in the ToolboxEditorList
10+
- For now all methods placed in the PropertyUtility script are public
11+
112
## 0.11.4 [17.07.2022]
213

314
### Added:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ static InLineEditorAttributeDrawer()
2323
editor.ReloadPreviewInstances();
2424
}
2525

26+
if (a.HideScript)
27+
{
28+
if (editor is ToolboxEditor toolboxEditor)
29+
{
30+
toolboxEditor.IgnoreProperty(PropertyUtility.Defaults.scriptPropertyName);
31+
}
32+
}
33+
2634
return editor;
2735
}
2836
else

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13-
private readonly TypeField typeField = new TypeField(new TypeConstraintReference(null));
13+
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
14+
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
15+
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
1416

1517

16-
private void CreateTypeProperty(SerializedProperty property)
18+
private void UpdateContexts(ReferencePickerAttribute attribute)
19+
{
20+
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
21+
}
22+
23+
private void CreateTypeProperty(SerializedProperty property, Type parentType)
1724
{
18-
property.GetFieldInfo(out Type propertyType);
1925
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
2026
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
2127
position = EditorGUI.IndentedRect(position);
@@ -44,7 +50,7 @@ private void CreateTypeProperty(SerializedProperty property)
4450
{
4551
ToolboxEditorLog.LogWarning("Invalid attempt to update disposed property.");
4652
}
47-
}, currentType, propertyType);
53+
}, currentType, parentType);
4854
}
4955

5056
private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
@@ -55,6 +61,23 @@ private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
5561
property.serializedObject.ApplyModifiedProperties();
5662
}
5763

64+
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
65+
{
66+
property.GetFieldInfo(out Type propertyType);
67+
var candidateType = attribute.ParentType;
68+
if (candidateType != null)
69+
{
70+
if (propertyType.IsAssignableFrom(candidateType))
71+
{
72+
return candidateType;
73+
}
74+
75+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
76+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
77+
}
78+
79+
return propertyType;
80+
}
5881

5982
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
6083
{
@@ -65,8 +88,11 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
6588
return;
6689
}
6790

91+
UpdateContexts(attribute);
92+
var parentType = GetParentType(property, attribute);
93+
6894
EditorGUI.indentLevel++;
69-
CreateTypeProperty(property);
95+
CreateTypeProperty(property, parentType);
7096
ToolboxEditorGui.DrawPropertyChildren(property);
7197
EditorGUI.indentLevel--;
7298
}

Assets/Editor Toolbox/Editor/Internal/ToolboxEditorList.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private void DrawElementRow(int index, bool isActive, bool isTarget, bool hasFoc
8282
var elementRect = elementGroup.rect;
8383
//adjust label width to the known dragging area
8484
EditorGUIUtility.labelWidth -= Style.dragAreaWidth;
85-
DrawElement(index);
85+
DrawElement(elementRect, index, isActive, hasFocus);
8686
EditorGUIUtility.labelWidth += Style.dragAreaWidth;
8787
}
8888

@@ -125,11 +125,18 @@ private void DrawElementDragHandle(Rect rect, int index, bool isSelected, bool h
125125
}
126126
}
127127

128-
private void DrawElement(int index)
128+
private void DrawElement(Rect rect, int index, bool isActive, bool hasFocus)
129129
{
130-
var element = List.GetArrayElementAtIndex(index);
131-
var content = GetElementContent(element, index);
132-
ToolboxEditorGui.DrawToolboxProperty(element, content);
130+
if (drawElementCallback != null)
131+
{
132+
drawElementCallback(rect, index, isActive, hasFocus);
133+
}
134+
else
135+
{
136+
var element = List.GetArrayElementAtIndex(index);
137+
var content = GetElementContent(element, index);
138+
ToolboxEditorGui.DrawToolboxProperty(element, content);
139+
}
133140
}
134141

135142
/// <summary>

Assets/Editor Toolbox/Editor/Internal/TypesEditorCollection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ private static string FormatGroupedTypeName(Type type, TypeGrouping grouping)
7979
}
8080

8181
return "Scripts/" + type.FullName.Replace('.', '/');
82+
83+
case TypeGrouping.ByFlatName:
84+
return type.Name;
8285
}
8386
}
8487

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections;
33
using System.Linq;
44
using System.Reflection;
5-
using System.Runtime.CompilerServices;
65

76
using UnityEditor;
87
using Object = UnityEngine.Object;
@@ -14,15 +13,15 @@ public static partial class PropertyUtility
1413
/// <summary>
1514
/// Indicates if the property has all changes applied and can be safely used for reflection-based features.
1615
/// </summary>
17-
internal static bool HasModifedProperties(this SerializedProperty property)
16+
public static bool HasModifedProperties(this SerializedProperty property)
1817
{
1918
return property.serializedObject.hasModifiedProperties;
2019
}
2120

2221
/// <summary>
2322
/// Creates and returns unique (hash based) key for this property.
2423
/// </summary>
25-
internal static string GetPropertyHashKey(this SerializedProperty property)
24+
public static string GetPropertyHashKey(this SerializedProperty property)
2625
{
2726
var hash = property.serializedObject.GetHashCode();
2827
#if UNITY_2019_2_OR_NEWER
@@ -37,7 +36,7 @@ internal static string GetPropertyHashKey(this SerializedProperty property)
3736
/// <summary>
3837
/// Creates and returns unique (type based) key for this property.
3938
/// </summary>
40-
internal static string GetPropertyTypeKey(this SerializedProperty property)
39+
public static string GetPropertyTypeKey(this SerializedProperty property)
4140
{
4241
var type = property.serializedObject.targetObject.GetType();
4342
#if UNITY_2019_2_OR_NEWER
@@ -52,31 +51,31 @@ internal static string GetPropertyTypeKey(this SerializedProperty property)
5251
/// <summary>
5352
/// Returns <see cref="object"/> which truly declares this property.
5453
/// </summary>
55-
internal static object GetDeclaringObject(this SerializedProperty property)
54+
public static object GetDeclaringObject(this SerializedProperty property)
5655
{
5756
return GetDeclaringObject(property, property.serializedObject.targetObject);
5857
}
5958

6059
/// <summary>
6160
/// Returns <see cref="object"/> which truly declares this property.
6261
/// </summary>
63-
internal static object GetDeclaringObject(this SerializedProperty property, bool ignoreArrays)
62+
public static object GetDeclaringObject(this SerializedProperty property, bool ignoreArrays)
6463
{
6564
return GetDeclaringObject(property, property.serializedObject.targetObject, ignoreArrays);
6665
}
6766

6867
/// <summary>
6968
/// Returns <see cref="object"/> which truly declares this property.
7069
/// </summary>
71-
internal static object GetDeclaringObject(this SerializedProperty property, Object target)
70+
public static object GetDeclaringObject(this SerializedProperty property, Object target)
7271
{
7372
return GetDeclaringObject(property, target, true);
7473
}
7574

7675
/// <summary>
7776
/// Returns <see cref="object"/> which truly declares this property.
7877
/// </summary>
79-
internal static object GetDeclaringObject(this SerializedProperty property, Object target, bool ignoreArrays)
78+
public static object GetDeclaringObject(this SerializedProperty property, Object target, bool ignoreArrays)
8079
{
8180
EnsureReflectionSafeness(property);
8281

@@ -101,7 +100,7 @@ internal static object GetDeclaringObject(this SerializedProperty property, Obje
101100
return validReference;
102101
}
103102

104-
internal static object GetTreePathReference(string treeField, object treeParent)
103+
public static object GetTreePathReference(string treeField, object treeParent)
105104
{
106105
if (IsSerializableArrayElement(treeField, out var index))
107106
{
@@ -123,7 +122,7 @@ internal static object GetTreePathReference(string treeField, object treeParent)
123122
/// Returns proper <see cref="FieldInfo"/> value for this property, even if the property is an array element.
124123
/// </summary>
125124
/// <param name="fieldInfo">FieldInfo associated to provided property.</param>
126-
internal static object GetProperValue(this SerializedProperty property, FieldInfo fieldInfo)
125+
public static object GetProperValue(this SerializedProperty property, FieldInfo fieldInfo)
127126
{
128127
return GetProperValue(property, fieldInfo, property.GetDeclaringObject());
129128
}
@@ -133,7 +132,7 @@ internal static object GetProperValue(this SerializedProperty property, FieldInf
133132
/// </summary>
134133
/// <param name="property"></param>
135134
/// <param name="fieldInfo">FieldInfo associated to provided property.</param>
136-
internal static object GetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object declaringObject)
135+
public static object GetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object declaringObject)
137136
{
138137
if (fieldInfo == null)
139138
{
@@ -161,7 +160,7 @@ internal static object GetProperValue(this SerializedProperty property, FieldInf
161160
/// </summary>
162161
/// <param name="property"></param>
163162
/// <param name="fieldInfo">FieldInfo associated to provided property.</param>
164-
internal static void SetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object value)
163+
public static void SetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object value)
165164
{
166165
SetProperValue(property, fieldInfo, value, true);
167166
}
@@ -173,7 +172,7 @@ internal static void SetProperValue(this SerializedProperty property, FieldInfo
173172
/// </summary>
174173
/// <param name="property"></param>
175174
/// <param name="fieldInfo">FieldInfo associated to provided property.</param>
176-
internal static void SetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object value, bool callOnValidate)
175+
public static void SetProperValue(this SerializedProperty property, FieldInfo fieldInfo, object value, bool callOnValidate)
177176
{
178177
if (fieldInfo == null)
179178
{
@@ -211,7 +210,7 @@ internal static void SetProperValue(this SerializedProperty property, FieldInfo
211210
/// Returns proper <see cref="Type"/> for this property, even if the property is an array element.
212211
/// </summary>
213212
/// <param name="fieldInfo">FieldInfo associated to provided property.</param>
214-
internal static Type GetProperType(this SerializedProperty property, FieldInfo fieldInfo)
213+
public static Type GetProperType(this SerializedProperty property, FieldInfo fieldInfo)
215214
{
216215
if (fieldInfo == null)
217216
{
@@ -233,7 +232,7 @@ internal static Type GetProperType(this SerializedProperty property, FieldInfo f
233232
}
234233
}
235234

236-
internal static Type GetScriptTypeFromProperty(SerializedProperty property)
235+
public static Type GetScriptTypeFromProperty(SerializedProperty property)
237236
{
238237
var scriptProperty = property.serializedObject.FindProperty(Defaults.scriptPropertyName);
239238
if (scriptProperty == null)
@@ -251,17 +250,17 @@ internal static Type GetScriptTypeFromProperty(SerializedProperty property)
251250
}
252251

253252

254-
internal static FieldInfo GetFieldInfo(this SerializedProperty property)
253+
public static FieldInfo GetFieldInfo(this SerializedProperty property)
255254
{
256255
return GetFieldInfo(property, out _);
257256
}
258257

259-
internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType)
258+
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType)
260259
{
261260
return GetFieldInfoFromProperty(property, out propertyType);
262261
}
263262

264-
internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
263+
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
265264
{
266265
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
267266
}

Assets/Editor Toolbox/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ public Material var1;
415415
```
416416
![inspector](https://github.com/arimger/Unity-Editor-Toolbox/blob/develop/Docs/inlined1.png)
417417

418+
```csharp
419+
[InLineEditor(HideScript = false)]
420+
public MyCustomType var1;
421+
```
422+
418423
##### Reorderable List
419424

420425
Custom implementation of standard ReorderableList (UnityEditorInternal). Usable as an attribute in serialized fields or a single object in custom Editors.
@@ -540,9 +545,11 @@ public int var1;
540545
You can draw properties marked with the **[SerializeReference]** attribute with an additional type picker that allows you to manipulate what managed type will be serialized.
541546

542547
```csharp
543-
[SerializeReference, ReferencePicker]
548+
[SerializeReference, ReferencePicker(TypeGrouping = TypeGrouping.ByFlatName)]
544549
public Interface1 var1;
545550
[SerializeReference, ReferencePicker]
551+
public Interface1 var1;
552+
[SerializeReference, ReferencePicker(ParentType = typeof(ClassWithInterface2)]
546553
public ClassWithInterfaceBase var2;
547554

548555
public interface Interface1 { }

Assets/Editor Toolbox/Runtime/Attributes/Regular/TypeConstraintAttribute.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public enum TypeGrouping
134134
/// grouping method must only be used for <see cref="MonoBehaviour"/> types.
135135
/// </summary>
136136
ByAddComponentMenu,
137+
/// <summary>
138+
/// Only name of the <see cref="Type"/>.
139+
/// </summary>
140+
ByFlatName
137141
}
138142

139143
/// <summary>

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/PropertySelfAttributes/InLineEditorAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public InLineEditorAttribute(bool drawHeader, bool drawPreview = true, bool draw
3333

3434
public bool DrawSettings { get; private set; }
3535

36+
/// <summary>
37+
/// Indicates if the "m_Script" property should be hidden.
38+
/// Will work only for Toolbox-based Editors.
39+
/// </summary>
40+
public bool HideScript { get; set; } = true;
41+
3642
/// <summary>
3743
/// Indicates if the inlined Editor should be disabled.
3844
/// </summary>

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/PropertySelfAttributes/ReferencePickerAttribute.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ namespace UnityEngine
1212
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
1313
[Conditional("UNITY_EDITOR")]
1414
public class ReferencePickerAttribute : ToolboxSelfPropertyAttribute
15-
{ }
15+
{
16+
public ReferencePickerAttribute()
17+
{ }
18+
19+
public ReferencePickerAttribute(Type parentType) : this(parentType, TypeGrouping.None)
20+
{ }
21+
22+
public ReferencePickerAttribute(Type parentType, TypeGrouping typeGrouping)
23+
{
24+
ParentType = parentType;
25+
TypeGrouping = typeGrouping;
26+
}
27+
28+
public Type ParentType { get; set; }
29+
/// <summary>
30+
/// Gets or sets grouping of selectable classes.
31+
/// Defaults to <see cref="TypeGrouping.None"/> unless explicitly specified.
32+
/// </summary>
33+
public TypeGrouping TypeGrouping { get; set; } = TypeGrouping.None;
34+
}
1635
}
1736
#endif

0 commit comments

Comments
 (0)