Skip to content

Commit bee1446

Browse files
Merge remote-tracking branch 'upstream/main' into #34-Add-Prefabs-AssetsItemBuilder
2 parents 1610d5f + 05908da commit bee1446

File tree

11 files changed

+3541
-43
lines changed

11 files changed

+3541
-43
lines changed

.editorconfig

Lines changed: 3467 additions & 0 deletions
Large diffs are not rendered by default.

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
12-
uses: actions/checkout@v2
12+
uses: actions/checkout@v3
1313
with:
1414
token: ${{ secrets.GH_TOKEN }}
1515
- name: Setup Node.js

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
### [1.7.3](https://github.com/Thundernerd/Unity3D-SerializableInterface/compare/v1.7.2...v1.7.3) (2022-07-22)
2+
3+
4+
### Refactors
5+
6+
* property drawer now reuses cached reference drawers ([f930d8e](https://github.com/Thundernerd/Unity3D-SerializableInterface/commit/f930d8ed92b8358e417d075f9a089f7161cadc50))
7+
8+
### [1.7.2](https://github.com/Thundernerd/Unity3D-SerializableInterface/compare/v1.7.1...v1.7.2) (2022-07-22)
9+
10+
11+
### Bug Fixes
12+
13+
* updated actions/checkout to version 3 ([ef748e1](https://github.com/Thundernerd/Unity3D-SerializableInterface/commit/ef748e176b864cd3dfc00f3b96e9cedb8783055d))
14+
115
### [1.7.1](https://github.com/Thundernerd/Unity3D-SerializableInterface/compare/v1.7.0...v1.7.1) (2022-06-18)
216

317

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,38 @@ namespace TNRD.Drawers
88
{
99
internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1010
{
11-
private readonly GUIContent label;
11+
private GUIContent label;
12+
private FieldInfo fieldInfo;
1213

13-
private static object previousReferenceValue;
14-
private static string previousPropertyPath;
14+
private object previousReferenceValue;
15+
private string previousPropertyPath;
1516

16-
/// <inheritdoc />
17-
public RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo)
18-
: base(property, genericType, fieldInfo)
17+
private object RawReferenceValue
18+
{
19+
get
20+
{
21+
#if UNITY_2021_1_OR_NEWER
22+
return RawReferenceProperty.managedReferenceValue;
23+
#else
24+
ISerializableInterface instance =
25+
(ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject);
26+
return instance.GetRawReference();
27+
#endif
28+
}
29+
30+
set
31+
{
32+
#if UNITY_2021_1_OR_NEWER
33+
RawReferenceProperty.managedReferenceValue = value;
34+
#else
35+
fieldInfo.SetValue(Property.serializedObject.targetObject, value);
36+
#endif
37+
}
38+
}
39+
40+
public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo)
1941
{
42+
Initialize(property, genericType);
2043
this.label = label;
2144
}
2245

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ private enum DragAndDropMode
2222

2323
private DragAndDropMode dragAndDropMode;
2424

25-
protected readonly SerializedProperty Property;
26-
protected readonly Type GenericType;
2725
protected readonly CustomObjectDrawer CustomObjectDrawer;
2826

27+
protected SerializedProperty Property { get; private set; }
28+
protected Type GenericType { get; private set; }
29+
2930
protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode");
3031
protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference");
3132
protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference");
@@ -91,19 +92,21 @@ protected object PropertyValue
9192
}
9293
}
9394

94-
protected ReferenceDrawer(SerializedProperty property, Type genericType, FieldInfo fieldInfo)
95+
protected ReferenceDrawer()
9596
{
96-
Property = property;
97-
GenericType = genericType;
98-
this.fieldInfo = fieldInfo;
99-
10097
CustomObjectDrawer = new CustomObjectDrawer();
10198
CustomObjectDrawer.ButtonClicked += OnButtonClicked;
10299
CustomObjectDrawer.Clicked += OnClicked;
103100
CustomObjectDrawer.DeletePressed += OnDeletePressed;
104101
CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked;
105102
}
106103

104+
protected void Initialize(SerializedProperty property, Type genericType)
105+
{
106+
Property = property;
107+
GenericType = genericType;
108+
}
109+
107110
private void OnButtonClicked(Rect position)
108111
{
109112
AdvancedDropdownState state = new AdvancedDropdownState();

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace TNRD.Drawers
99
{
1010
internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1111
{
12-
private readonly GUIContent label;
12+
private GUIContent label;
1313

14-
public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo)
15-
: base(property, genericType, fieldInfo)
14+
public void Initialize(SerializedProperty property, Type genericType, GUIContent label)
1615
{
16+
Initialize(property, genericType);
1717
this.label = label;
1818
}
1919

Editor/Items/NoneDropdownItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ namespace TNRD.Items
44
{
55
public class NoneDropdownItem : AdvancedDropdownItem, IDropdownItem
66
{
7-
private ReferenceMode mode => ReferenceMode.Raw;
87
public NoneDropdownItem() : base("None") { }
98

10-
ReferenceMode IDropdownItem.Mode => mode;
9+
ReferenceMode IDropdownItem.Mode => ReferenceMode.Raw;
1110

1211
public object GetValue()
1312
{

Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ namespace TNRD
1010
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
1111
internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer
1212
{
13+
private readonly RawReferenceDrawer rawReferenceDrawer = new RawReferenceDrawer();
14+
private readonly UnityReferenceDrawer unityReferenceDrawer = new UnityReferenceDrawer();
15+
1316
private SerializedProperty serializedProperty;
1417
private Type genericType;
1518

16-
private IReferenceDrawer activeDrawer;
17-
1819
/// <inheritdoc />
1920
public override bool CanCacheInspectorGUI(SerializedProperty property) => false;
2021

@@ -23,7 +24,6 @@ private void Initialize(SerializedProperty property)
2324
if (serializedProperty == property)
2425
return;
2526

26-
activeDrawer = null;
2727
serializedProperty = property;
2828
genericType = GetGenericArgument();
2929
Assert.IsNotNull(genericType, "Unable to find generic argument, are you doing some shady inheritance?");
@@ -33,16 +33,14 @@ private void Initialize(SerializedProperty property)
3333
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
3434
{
3535
Initialize(property);
36-
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
37-
return activeDrawer.GetHeight();
36+
return GetReferenceDrawer(property, label).GetHeight();
3837
}
3938

4039
/// <inheritdoc />
4140
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4241
{
4342
Initialize(property);
44-
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
45-
activeDrawer.OnGUI(position);
43+
GetReferenceDrawer(property, label).OnGUI(position);
4644
}
4745

4846
private Type GetGenericArgument()
@@ -81,25 +79,19 @@ private Type GetGenericArgument()
8179
return null;
8280
}
8381

84-
private IReferenceDrawer GetReferenceDrawer(
85-
IReferenceDrawer original,
86-
SerializedProperty property,
87-
GUIContent label
88-
)
82+
private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label)
8983
{
9084
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
9185
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;
9286

9387
switch (referenceMode)
9488
{
9589
case ReferenceMode.Raw:
96-
return original is RawReferenceDrawer
97-
? original
98-
: new RawReferenceDrawer(property, label, genericType, fieldInfo);
90+
rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
91+
return rawReferenceDrawer;
9992
case ReferenceMode.Unity:
100-
return original is UnityReferenceDrawer
101-
? original
102-
: new UnityReferenceDrawer(property, label, genericType, fieldInfo);
93+
unityReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
94+
return unityReferenceDrawer;
10395
default:
10496
throw new ArgumentOutOfRangeException();
10597
}

Editor/Utilities/SerializableInterfaceAdvancedDropdown.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected override AdvancedDropdownItem BuildRoot()
4848
.AddChild(new ClassesItemBuilder(interfaceType).Build())
4949
.AddChild(new SceneItemBuilder(interfaceType, relevantScene).Build());
5050

51-
foreach (var dropdownItem in item.children)
51+
foreach (AdvancedDropdownItem dropdownItem in item.children)
5252
{
5353
dropdownItem.AddChild(new NoneDropdownItem());
5454
}

Runtime/SerializableInterface.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace TNRD
99
/// </summary>
1010
/// <typeparam name="TInterface">The type of the interface you want to serialize</typeparam>
1111
[Serializable]
12-
public class SerializableInterface<TInterface> : ISerializableInterface
12+
public class SerializableInterface<TInterface> : ISerializableInterface where TInterface : class
1313
{
1414
[HideInInspector, SerializeField] private ReferenceMode mode = ReferenceMode.Unity;
1515
[HideInInspector, SerializeField] private UnityEngine.Object unityReference;
@@ -21,8 +21,8 @@ public TInterface Value
2121
{
2222
return mode switch
2323
{
24-
ReferenceMode.Raw => (TInterface)rawReference,
25-
ReferenceMode.Unity => (TInterface)(object)unityReference,
24+
ReferenceMode.Raw => rawReference as TInterface,
25+
ReferenceMode.Unity => (object)unityReference as TInterface,
2626
_ => throw new ArgumentOutOfRangeException()
2727
};
2828
}

0 commit comments

Comments
 (0)