From 8729481366c5f1ef1d2c233b5fe7f93b17c809d1 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sat, 25 Sep 2021 16:16:19 +0800 Subject: [PATCH] SyncObjectDrawer renamed to EnumerableSyncObjectDrawer because it's only meant for enumerable SyncObjects --- ...rawer.cs => EnumerableSyncObjectDrawer.cs} | 35 ++++++++++--------- ...eta => EnumerableSyncObjectDrawer.cs.meta} | 0 .../Editor/NetworkBehaviourInspector.cs | 12 +++---- 3 files changed, 24 insertions(+), 23 deletions(-) rename Assets/Mirror/Editor/{SyncObjectDrawer.cs => EnumerableSyncObjectDrawer.cs} (54%) rename Assets/Mirror/Editor/{SyncObjectDrawer.cs.meta => EnumerableSyncObjectDrawer.cs.meta} (100%) diff --git a/Assets/Mirror/Editor/SyncObjectDrawer.cs b/Assets/Mirror/Editor/EnumerableSyncObjectDrawer.cs similarity index 54% rename from Assets/Mirror/Editor/SyncObjectDrawer.cs rename to Assets/Mirror/Editor/EnumerableSyncObjectDrawer.cs index eb867dfdd..14ca3a9b5 100644 --- a/Assets/Mirror/Editor/SyncObjectDrawer.cs +++ b/Assets/Mirror/Editor/EnumerableSyncObjectDrawer.cs @@ -1,4 +1,5 @@ -// helper class for NetworkBehaviourInspector +// helper class for NetworkBehaviourInspector to draw all enumerable SyncObjects +// (SyncList/Set/Dictionary) using System.Collections; using System.Collections.Generic; using System.Reflection; @@ -6,13 +7,13 @@ namespace Mirror { - class SyncObjectField + class EnuerableSyncObjectField { public bool visible; public readonly FieldInfo field; public readonly string label; - public SyncObjectField(FieldInfo field) + public EnuerableSyncObjectField(FieldInfo field) { this.field = field; visible = false; @@ -20,49 +21,49 @@ public SyncObjectField(FieldInfo field) } } - public class SyncObjectDrawer + public class EnumerableSyncObjectDrawer { readonly UnityEngine.Object targetObject; - readonly List syncObjectFields; + readonly List enumerableSyncObjectFields; - public SyncObjectDrawer(UnityEngine.Object targetObject) + public EnumerableSyncObjectDrawer(UnityEngine.Object targetObject) { this.targetObject = targetObject; - syncObjectFields = new List(); + enumerableSyncObjectFields = new List(); foreach (FieldInfo field in InspectorHelper.GetAllFields(targetObject.GetType(), typeof(NetworkBehaviour))) { if (field.ImplementsInterface() && field.IsVisibleInInspector()) { - syncObjectFields.Add(new SyncObjectField(field)); + enumerableSyncObjectFields.Add(new EnuerableSyncObjectField(field)); } } } public void Draw() { - if (syncObjectFields.Count == 0) { return; } + if (enumerableSyncObjectFields.Count == 0) { return; } EditorGUILayout.Space(); EditorGUILayout.LabelField("Sync Objects", EditorStyles.boldLabel); - for (int i = 0; i < syncObjectFields.Count; i++) + for (int i = 0; i < enumerableSyncObjectFields.Count; i++) { - DrawSyncObject(syncObjectFields[i]); + DrawEnumerableSyncObject(enumerableSyncObjectFields[i]); } } - void DrawSyncObject(SyncObjectField syncObjectField) + void DrawEnumerableSyncObject(EnuerableSyncObjectField enuerableSyncObjectField) { - syncObjectField.visible = EditorGUILayout.Foldout(syncObjectField.visible, syncObjectField.label); - if (syncObjectField.visible) + enuerableSyncObjectField.visible = EditorGUILayout.Foldout(enuerableSyncObjectField.visible, enuerableSyncObjectField.label); + if (enuerableSyncObjectField.visible) { using (new EditorGUI.IndentLevelScope()) { - object fieldValue = syncObjectField.field.GetValue(targetObject); - if (fieldValue is IEnumerable synclist) + object fieldValue = enuerableSyncObjectField.field.GetValue(targetObject); + if (fieldValue is IEnumerable syncObject) { int index = 0; - foreach (object item in synclist) + foreach (object item in syncObject) { string itemValue = item != null ? item.ToString() : "NULL"; string itemLabel = $"Element {index}"; diff --git a/Assets/Mirror/Editor/SyncObjectDrawer.cs.meta b/Assets/Mirror/Editor/EnumerableSyncObjectDrawer.cs.meta similarity index 100% rename from Assets/Mirror/Editor/SyncObjectDrawer.cs.meta rename to Assets/Mirror/Editor/EnumerableSyncObjectDrawer.cs.meta diff --git a/Assets/Mirror/Editor/NetworkBehaviourInspector.cs b/Assets/Mirror/Editor/NetworkBehaviourInspector.cs index 8915959cc..b11ad5caf 100644 --- a/Assets/Mirror/Editor/NetworkBehaviourInspector.cs +++ b/Assets/Mirror/Editor/NetworkBehaviourInspector.cs @@ -10,7 +10,7 @@ namespace Mirror public class NetworkBehaviourInspector : Editor { bool syncsAnything; - SyncObjectDrawer syncObjectDrawer; + EnumerableSyncObjectDrawer enumerableSyncObjectDrawer; // does this type sync anything? otherwise we don't need to show syncInterval bool SyncsAnything(Type scriptClass) @@ -51,7 +51,7 @@ void OnEnable() Type scriptClass = target.GetType(); - syncObjectDrawer = new SyncObjectDrawer(serializedObject.targetObject); + enumerableSyncObjectDrawer = new EnumerableSyncObjectDrawer(serializedObject.targetObject); syncsAnything = SyncsAnything(scriptClass); } @@ -59,17 +59,17 @@ void OnEnable() public override void OnInspectorGUI() { DrawDefaultInspector(); - DrawDefaultSyncObjects(); + DrawDefaultEnumerableSyncObjects(); DrawDefaultSyncSettings(); } // Draws Sync Objects that are IEnumerable - protected void DrawDefaultSyncObjects() + protected void DrawDefaultEnumerableSyncObjects() { // Need this check in case OnEnable returns early - if (syncObjectDrawer == null) return; + if (enumerableSyncObjectDrawer == null) return; - syncObjectDrawer.Draw(); + enumerableSyncObjectDrawer.Draw(); } // Draws SyncSettings if the NetworkBehaviour has anything to sync