feature: NetworkBehaviour: HasSyncObjects for NetworkBehaviourInspector to avoid Reflection

This commit is contained in:
vis2k 2021-09-21 11:27:27 +08:00
parent 7c28f06c8f
commit f1b8aa4fc2
2 changed files with 2 additions and 4 deletions

View File

@ -40,10 +40,7 @@ bool SyncsAnything(Type scriptClass)
// search for SyncObjects manually. // search for SyncObjects manually.
// Any SyncObject should be added to syncObjects when unity creates an // Any SyncObject should be added to syncObjects when unity creates an
// object so we can check length of list so see if sync objects exists // object so we can check length of list so see if sync objects exists
FieldInfo syncObjectsField = scriptClass.GetField("syncObjects", BindingFlags.NonPublic | BindingFlags.Instance); return ((NetworkBehaviour)serializedObject.targetObject).HasSyncObjects();
List<SyncObject> syncObjects = (List<SyncObject>)syncObjectsField.GetValue(serializedObject.targetObject);
return syncObjects.Count > 0;
} }
void OnEnable() void OnEnable()

View File

@ -58,6 +58,7 @@ public abstract class NetworkBehaviour : MonoBehaviour
// SyncLists, SyncSets, etc. // SyncLists, SyncSets, etc.
protected readonly List<SyncObject> syncObjects = new List<SyncObject>(); protected readonly List<SyncObject> syncObjects = new List<SyncObject>();
public bool HasSyncObjects() => syncObjects.Count > 0;
// NetworkIdentity based values set from NetworkIdentity.Awake(), // NetworkIdentity based values set from NetworkIdentity.Awake(),
// which is way more simple and way faster than trying to figure out // which is way more simple and way faster than trying to figure out