Last active
June 22, 2023 12:56
-
-
Save Daxten/4d9a57f57607bfdbda1d57c4748bdaf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Mirage; | |
using NetworkCore; | |
using Unity.VisualScripting; | |
using UnityEditor; | |
using UnityEditor.Rendering; | |
using UnityEditor.UIElements; | |
using UnityEngine; | |
using UnityEngine.UIElements; | |
[CustomEditor(typeof(NetworkBehaviour), true)] | |
[CanEditMultipleObjects] | |
public class CustomNetworkBehaviourEditor : NetworkBehaviourInspector | |
{ | |
public override VisualElement CreateInspectorGUI() | |
{ | |
var root = base.CreateInspectorGUI(); | |
var oneFound = false; | |
var so = new SerializedObject(target); | |
foreach (var field in InspectorHelper.GetAllFields(target.GetType(), typeof(NetworkBehaviour))) | |
{ | |
if (typeof(ISyncLink).IsAssignableFrom(field.FieldType)) | |
{ | |
if (!oneFound) | |
{ | |
root.Add(NetworkBehaviourInspectorDrawer.CreateHeader("Sync Links")); | |
oneFound = true; | |
} | |
var tpe = field.FieldType.GetGenericArguments()[0]; | |
var value = field.GetValue(target); | |
var propertyField = new ObjectField(field.Name); | |
propertyField.SetValueWithoutNotify((dynamic)value); | |
propertyField.RegisterValueChangedCallback(evt => | |
{ | |
if (evt.newValue == null) | |
{ | |
((dynamic)value).Value = null; | |
} | |
else | |
{ | |
var comp = evt.newValue.GetComponent(tpe); | |
if (comp != null) | |
{ | |
var conv = Convert.ChangeType(comp, tpe); | |
((dynamic)value).Value = (dynamic)conv; | |
} | |
else | |
{ | |
propertyField.SetValueWithoutNotify(null); | |
((dynamic)value).Value = null; | |
} | |
} | |
var sp = so.FindProperty(field.Name); | |
sp.SetUnderlyingValue(value); | |
so.ApplyModifiedProperties(); | |
EditorUtility.SetDirty(target); | |
}); | |
propertyField.label = field.Name; | |
root.Add(propertyField); | |
} | |
} | |
return root; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment