Skip to content

Commit

Permalink
feat: new NetworkInspectorCallbacks component
Browse files Browse the repository at this point in the history
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
  • Loading branch information
James-Frowen committed Mar 25, 2024
1 parent 1c9f2d8 commit a8051c4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Mirage.Events;
using UnityEngine;

namespace Mirage
{
/// <summary>
/// Callbacks for <see cref="NetworkIdentity"/>
/// </summary>
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<AddLateEvent>(nameof(_onStartServer));
_onStartClient = GetAndClear<AddLateEvent>(nameof(_onStartClient));
_onStartLocalPlayer = GetAndClear<AddLateEvent>(nameof(_onStartLocalPlayer));
_onAuthorityChanged = GetAndClear<BoolAddLateEvent>(nameof(_onAuthorityChanged));
_onOwnerChanged = GetAndClear<NetworkPlayerAddLateEvent>(nameof(_onOwnerChanged));
_onStopClient = GetAndClear<AddLateEvent>(nameof(_onStopClient));
_onStopServer = GetAndClear<AddLateEvent>(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<T>(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
11 changes: 11 additions & 0 deletions Assets/Mirage/Runtime/NetworkIdentityInspectorCallbacks.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8051c4

Please sign in to comment.