Skip to content

Commit 2f2a7f7

Browse files
author
SolidAlloy
committed
fix: Optimized the Type property setter
When TypeReference was created or its Type property was set through scripting, it took 80ms because GUID was searched for each time. Now, GUID search has moved to OnBeforeSerialize(), so TypeReference creation/change takes less than 1 ms.
1 parent 718b49e commit 2f2a7f7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Runtime/TypeReference.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class TypeReference : ISerializationCallbackReceiver
2929
private Type _type;
3030

3131
private bool _needToLogTypeNotFound;
32+
private bool _typeChanged;
3233

3334
/// <summary>
3435
/// Initializes a new instance of the <see cref="TypeReference"/> class with Type equal to null.
@@ -87,9 +88,11 @@ public Type Type
8788
{
8889
MakeSureTypeHasName(value);
8990

91+
if (_type != value)
92+
_typeChanged = true;
93+
9094
_type = value;
9195
TypeNameAndAssembly = GetTypeNameAndAssembly(value);
92-
SetClassGuidIfExists(value);
9396
}
9497
}
9598

@@ -110,6 +113,12 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
110113

111114
void ISerializationCallbackReceiver.OnBeforeSerialize()
112115
{
116+
if (_typeChanged)
117+
{
118+
SetClassGuidIfExists(Type);
119+
_typeChanged = false;
120+
}
121+
113122
#if UNITY_EDITOR
114123
EditorApplication.delayCall -= TryUpdatingTypeUsingGUID;
115124
EditorApplication.delayCall -= LogTypeNotFoundIfNeeded;
@@ -223,6 +232,7 @@ private void SetClassGuidIfExists(Type type)
223232
}
224233
catch (UnityException) // thrown on assembly recompiling if field initialization is used on field.
225234
{
235+
// GUID will be found in SerializedTypeReference.SetGuidIfAssignmentFailed()
226236
GuidAssignmentFailed = true;
227237
GUID = string.Empty;
228238
}

0 commit comments

Comments
 (0)