Skip to content

Commit aba67e2

Browse files
committed
no message (+3 squashed commit)
Squashed commit: [e7c11be] modify read me [f1b2eb2] add image [b0568ec] Add read me
1 parent 85b9144 commit aba67e2

File tree

12 files changed

+156
-7
lines changed

12 files changed

+156
-7
lines changed

Assets/Scenes/Demo.unity

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,17 @@ MonoBehaviour:
330330
m_Script: {fileID: 11500000, guid: 39b7687e21ae28348b198d5a533819cd, type: 3}
331331
m_Name:
332332
m_EditorClassIdentifier:
333+
peopel:
334+
rid: 7289075113654747151
335+
a: 10
336+
b: 20
337+
references:
338+
version: 2
339+
RefIds:
340+
- rid: 7289075113654747151
341+
type: {class: Student, ns: Demo.Test, asm: Assembly-CSharp}
342+
data:
343+
m_name: ABC
333344
--- !u!4 &1545680082
334345
Transform:
335346
m_ObjectHideFlags: 0

Assets/Scripts/SerializeExtension/Editor/SerializeExtensionDrawer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,22 @@ private void ShowPolymorphismField(Type abstractType, Rect position, SerializedP
9898
property.managedReferenceValue = null;
9999
}
100100
else {
101-
property.managedReferenceValue = FormatterServices.GetSafeUninitializedObject(subTypes[newSelectIndex - 1]); // 将选择框的一个偏移量减回来
101+
bool hasDefaultConstructor = false;
102+
103+
Type type = subTypes[newSelectIndex - 1]; // 将选择框的一个偏移量减回来
104+
ConstructorInfo[] constructors = type.GetConstructors();
105+
// 查找无参构造函数
106+
foreach (var constructor in constructors) {
107+
if (constructor.GetParameters().Length == 0) {
108+
// 调用无参构造函数并返回实例
109+
hasDefaultConstructor = true;
110+
property.managedReferenceValue = constructor.Invoke(null);
111+
}
112+
}
113+
114+
if (!hasDefaultConstructor) {
115+
property.managedReferenceValue = FormatterServices.GetSafeUninitializedObject(type);
116+
}
102117
}
103118
property.serializedObject.ApplyModifiedProperties(); //更改多态类型后必须马上保存,否则后续序列化可能会出现异常
104119
}

Assets/Scripts/Test/Person/Student.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Demo.Test;
1+
using Core;
22
using System;
33
using UnityEngine;
44

@@ -7,6 +7,7 @@ namespace Demo.Test
77
[Serializable]
88
public class Student : IPerson
99
{
10-
[SerializeField] int student_id;
10+
[SerializeExtension(canWrite: false)]
11+
[SerializeField] string m_name = "ABC";
1112
}
1213
}

Assets/Scripts/Test/Person/Teacher.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace Demo.Test
66
[Serializable]
77
public class Teacher : IPerson
88
{
9-
[SerializeField] float teach_id;
10-
11-
[SerializeField] int student_count;
9+
[SerializeField] int m_teachID;
1210
}
1311
}

Assets/Scripts/Test/Test.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
public class Test : MonoBehaviour
66
{
7-
[SerializeExtension]
7+
//[SerializeExtension]
8+
//[SerializeReference] IPerson peopel;
9+
10+
//[SerializeExtension(proxyPropertyName: nameof(Health))]
11+
//[SerializeField] int health;
12+
13+
//public int Health {
14+
// get => health;
15+
// set => health = Mathf.Clamp(value, 0, 100);
16+
//}
17+
18+
[SerializeExtension]
819
[SerializeReference] IPerson peopel;
20+
21+
22+
[SerializeField] int a = 10;
23+
24+
[SerializeExtension(canWrite: false)]
25+
[SerializeField] int b = 20;
926
}

README.md

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

src/01.png

51.3 KB
Loading

src/02.png

20.8 KB
Loading

src/03.png

31.6 KB
Loading

src/04.png

18.7 KB
Loading

src/05.png

18.5 KB
Loading

src/06.png

30.1 KB
Loading

0 commit comments

Comments
 (0)