SyncObjectDrawer renamed to EnumerableSyncObjectDrawer because it's only meant for enumerable SyncObjects

This commit is contained in:
vis2k 2021-09-25 16:16:19 +08:00
parent 8298a2556b
commit 8729481366
3 changed files with 24 additions and 23 deletions

View File

@ -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<SyncObjectField> syncObjectFields;
readonly List<EnuerableSyncObjectField> enumerableSyncObjectFields;
public SyncObjectDrawer(UnityEngine.Object targetObject)
public EnumerableSyncObjectDrawer(UnityEngine.Object targetObject)
{
this.targetObject = targetObject;
syncObjectFields = new List<SyncObjectField>();
enumerableSyncObjectFields = new List<EnuerableSyncObjectField>();
foreach (FieldInfo field in InspectorHelper.GetAllFields(targetObject.GetType(), typeof(NetworkBehaviour)))
{
if (field.ImplementsInterface<SyncObject>() && 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}";

View File

@ -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