NetworkBheaviourInspector: SyncObjectDrawer class moved into separate file

This commit is contained in:
vis2k 2021-09-25 15:58:30 +08:00
parent c8070a3cac
commit ab78c858da
3 changed files with 81 additions and 72 deletions

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
@ -94,74 +92,4 @@ protected void DrawDefaultSyncSettings()
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
} }
public class SyncObjectDrawer
{
readonly UnityEngine.Object targetObject;
readonly List<SyncObjectField> syncObjectFields;
public SyncObjectDrawer(UnityEngine.Object targetObject)
{
this.targetObject = targetObject;
syncObjectFields = new List<SyncObjectField>();
foreach (FieldInfo field in InspectorHelper.GetAllFields(targetObject.GetType(), typeof(NetworkBehaviour)))
{
if (field.IsSyncObject() && field.IsVisibleSyncObject())
{
syncObjectFields.Add(new SyncObjectField(field));
}
}
}
public void Draw()
{
if (syncObjectFields.Count == 0) { return; }
EditorGUILayout.Space();
EditorGUILayout.LabelField("Sync Objects", EditorStyles.boldLabel);
for (int i = 0; i < syncObjectFields.Count; i++)
{
DrawSyncObject(syncObjectFields[i]);
}
}
void DrawSyncObject(SyncObjectField syncObjectField)
{
syncObjectField.visible = EditorGUILayout.Foldout(syncObjectField.visible, syncObjectField.label);
if (syncObjectField.visible)
{
using (new EditorGUI.IndentLevelScope())
{
object fieldValue = syncObjectField.field.GetValue(targetObject);
if (fieldValue is IEnumerable synclist)
{
int index = 0;
foreach (object item in synclist)
{
string itemValue = item != null ? item.ToString() : "NULL";
string itemLabel = $"Element {index}";
EditorGUILayout.LabelField(itemLabel, itemValue);
index++;
}
}
}
}
}
class SyncObjectField
{
public bool visible;
public readonly FieldInfo field;
public readonly string label;
public SyncObjectField(FieldInfo field)
{
this.field = field;
visible = false;
label = $"{field.Name} [{field.FieldType.Name}]";
}
}
}
} }

View File

@ -0,0 +1,78 @@
// helper class for NetworkBehaviourInspector
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
namespace Mirror
{
class SyncObjectField
{
public bool visible;
public readonly FieldInfo field;
public readonly string label;
public SyncObjectField(FieldInfo field)
{
this.field = field;
visible = false;
label = $"{field.Name} [{field.FieldType.Name}]";
}
}
public class SyncObjectDrawer
{
readonly UnityEngine.Object targetObject;
readonly List<SyncObjectField> syncObjectFields;
public SyncObjectDrawer(UnityEngine.Object targetObject)
{
this.targetObject = targetObject;
syncObjectFields = new List<SyncObjectField>();
foreach (FieldInfo field in InspectorHelper.GetAllFields(targetObject.GetType(), typeof(NetworkBehaviour)))
{
if (field.IsSyncObject() && field.IsVisibleSyncObject())
{
syncObjectFields.Add(new SyncObjectField(field));
}
}
}
public void Draw()
{
if (syncObjectFields.Count == 0) { return; }
EditorGUILayout.Space();
EditorGUILayout.LabelField("Sync Objects", EditorStyles.boldLabel);
for (int i = 0; i < syncObjectFields.Count; i++)
{
DrawSyncObject(syncObjectFields[i]);
}
}
void DrawSyncObject(SyncObjectField syncObjectField)
{
syncObjectField.visible = EditorGUILayout.Foldout(syncObjectField.visible, syncObjectField.label);
if (syncObjectField.visible)
{
using (new EditorGUI.IndentLevelScope())
{
object fieldValue = syncObjectField.field.GetValue(targetObject);
if (fieldValue is IEnumerable synclist)
{
int index = 0;
foreach (object item in synclist)
{
string itemValue = item != null ? item.ToString() : "NULL";
string itemLabel = $"Element {index}";
EditorGUILayout.LabelField(itemLabel, itemValue);
index++;
}
}
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6f90afab12e04f0e945d83e9d38308a3
timeCreated: 1632556645