From a8051c4e4097ab85dfffe029f08938b5456074fc Mon Sep 17 00:00:00 2001 From: James Frowen Date: Mon, 25 Mar 2024 18:34:01 +0000 Subject: [PATCH] feat: new NetworkInspectorCallbacks component Component to hold unity events instead of NetworkIdentity. Anyone using the events on NetworkIdentity should add this component and press Convert so that events are moved --- .../NetworkIdentityInspectorCallbacks.cs | 82 +++++++++++++++++++ .../NetworkIdentityInspectorCallbacks.cs.meta | 11 +++ 2 files changed, 93 insertions(+) create mode 100644 Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs create mode 100644 Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs.meta diff --git a/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs b/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs new file mode 100644 index 0000000000..9f398cec1f --- /dev/null +++ b/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs @@ -0,0 +1,82 @@ +using Mirage.Events; +using UnityEngine; + +namespace Mirage +{ + /// + /// Callbacks for + /// + public class NetworkInspectorCallbacks : NetworkBehaviour + { + [SerializeField] private AddLateEvent _onStartServer = new AddLateEvent(); + [SerializeField] private AddLateEvent _onStartClient = new AddLateEvent(); + [SerializeField] private AddLateEvent _onStartLocalPlayer = new AddLateEvent(); + [SerializeField] private BoolAddLateEvent _onAuthorityChanged = new BoolAddLateEvent(); + [SerializeField] private NetworkPlayerAddLateEvent _onOwnerChanged = new NetworkPlayerAddLateEvent(); + [SerializeField] private AddLateEvent _onStopClient = new AddLateEvent(); + [SerializeField] private AddLateEvent _onStopServer = new AddLateEvent(); + + private void Awake() + { + Identity.OnStartServer.AddListener(_onStartServer.Invoke); + Identity.OnStartClient.AddListener(_onStartClient.Invoke); + Identity.OnStartLocalPlayer.AddListener(_onStartLocalPlayer.Invoke); + Identity.OnAuthorityChanged.AddListener(_onAuthorityChanged.Invoke); + Identity.OnOwnerChanged.AddListener(_onOwnerChanged.Invoke); + Identity.OnStopClient.AddListener(_onStopClient.Invoke); + Identity.OnStopServer.AddListener(_onStopServer.Invoke); + } + + +#if UNITY_EDITOR + public void Convert() + { + _onStartServer = GetAndClear(nameof(_onStartServer)); + _onStartClient = GetAndClear(nameof(_onStartClient)); + _onStartLocalPlayer = GetAndClear(nameof(_onStartLocalPlayer)); + _onAuthorityChanged = GetAndClear(nameof(_onAuthorityChanged)); + _onOwnerChanged = GetAndClear(nameof(_onOwnerChanged)); + _onStopClient = GetAndClear(nameof(_onStopClient)); + _onStopServer = GetAndClear(nameof(_onStopServer)); + + UnityEditor.EditorUtility.SetDirty(this); + UnityEditor.EditorUtility.SetDirty(Identity); + + if (Identity.gameObject.scene.IsValid()) + { + UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(Identity.gameObject.scene); + } + } + + private T GetAndClear(string field) where T : new() + { + var type = typeof(NetworkIdentity); + var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic; + var info = type.GetField(field, flags); + var value = (T)info.GetValue(Identity); + info.SetValue(Identity, new T()); + return value; + } + } +#endif +} + +#if UNITY_EDITOR +namespace Mirage.EditorScripts +{ + [UnityEditor.CustomEditor(typeof(NetworkInspectorCallbacks))] + public class NetworkInspectorCallbacksEditor : UnityEditor.Editor + { + public override void OnInspectorGUI() + { + DrawDefaultInspector(); + + var target = (NetworkInspectorCallbacks)base.target; + if (GUILayout.Button("Copy Identity events")) + { + target.Convert(); + } + } + } +} +#endif diff --git a/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs.meta b/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs.meta new file mode 100644 index 0000000000..ff5be00045 --- /dev/null +++ b/Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3592d87e6b4cb8743a08a398c1386e75 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3} + userData: + assetBundleName: + assetBundleVariant: