mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
NetworkBheaviourInspector: SyncObjectDrawer class moved into separate file
This commit is contained in:
parent
c8070a3cac
commit
ab78c858da
@ -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}]";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
78
Assets/Mirror/Editor/SyncObjectDrawer.cs
Normal file
78
Assets/Mirror/Editor/SyncObjectDrawer.cs
Normal 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Mirror/Editor/SyncObjectDrawer.cs.meta
Normal file
3
Assets/Mirror/Editor/SyncObjectDrawer.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6f90afab12e04f0e945d83e9d38308a3
|
||||||
|
timeCreated: 1632556645
|
Loading…
Reference in New Issue
Block a user