Skip to content

Commit

Permalink
feat: adding custom inspector for NetworkIdentity
Browse files Browse the repository at this point in the history
adding runtime values and id to inspector to help debug network objects
  • Loading branch information
James-Frowen committed Dec 28, 2024
1 parent 4236af8 commit 8cc9179
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
91 changes: 91 additions & 0 deletions Assets/Mirage/Editor/Inspectors/NetworkIdentityInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using UnityEditor;
using UnityEngine;

namespace Mirage
{
[CustomEditor(typeof(NetworkIdentity), true)]
public partial class NetworkIdentityInspector : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();

var target = (NetworkIdentity)this.target;

GUILayout.Space(12);
EditorGUILayout.LabelField("Spawn Ids", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
EditorGUILayout.LabelField("Scene Id", target.SceneId != 0
? target.SceneId.ToString("X")
: "<not part of a scene>");
EditorGUILayout.LabelField("Prefab Hash", target.PrefabHash != 0
? target.PrefabHash.ToString("X")
: "<no prefab>");
}

if (Application.isPlaying)
RuntimeInfo(target);
}

private void RuntimeInfo(NetworkIdentity target)
{


GUILayout.Space(12);
EditorGUILayout.LabelField("Runtime Values", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
EditorGUILayout.LabelField("Spawned", target.IsSpawned ? "Yes" : "No");
EditorGUILayout.FloatField("Net Id", target.NetId);
}

GUILayout.Space(12);
EditorGUILayout.LabelField("Server Values", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
EditorGUILayout.LabelField("Is Server", target.IsServer ? "Yes" : "No");
EditorGUILayout.ObjectField("Server Object Manager", target.ServerObjectManager, typeof(ServerObjectManager), false);
EditorGUILayout.TextField("Owner", (target.Owner != null)
? "Client Authority: " + target.Owner
: "NULL");
}

GUILayout.Space(12);
EditorGUILayout.LabelField("Client Values", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
EditorGUILayout.ObjectField("Client Object Manager", target.ClientObjectManager, typeof(ClientObjectManager), false);
EditorGUILayout.LabelField("Is Client", target.IsClient ? "Yes" : "No");
EditorGUILayout.LabelField("Is Local Player", target.IsLocalPlayer ? "Yes" : "No");
EditorGUILayout.LabelField("Has Authority", target.HasAuthority ? "Yes" : "No");
}

GUILayout.Space(12);
GUILayout.Label("Network Visibility", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
if (target.TryGetComponent<NetworkVisibility>(out var behaviour))
EditorGUILayout.ObjectField("Visibility", behaviour, typeof(MonoBehaviour), false);
else
EditorGUILayout.TextField("Visibility", "Default ServerObjectManager");
}

GUILayout.Space(12);
GUILayout.Label("Behaviours", EditorStyles.boldLabel);
using (new EditorGUI.DisabledGroupScope(true))
{
EditorGUI.indentLevel++;
foreach (var behaviour in target.NetworkBehaviours)
{
if (behaviour == null)
// could be the case in the editor after existing play mode.
continue;

EditorGUILayout.ObjectField("Visibility", behaviour, typeof(MonoBehaviour), false);
}
EditorGUI.indentLevel--;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirage/Editor/Inspectors/NetworkIdentityInspector.cs.meta

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

11 changes: 5 additions & 6 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public sealed class NetworkIdentity : MonoBehaviour
[FormerlySerializedAs("m_SceneId"), FormerlySerializedAs("sceneId")]
[SerializeField, HideInInspector]
private ulong _sceneId = 0;
[SerializeField, HideInInspector]
private int _prefabHash;

public ulong SceneId => _sceneId;

Expand Down Expand Up @@ -209,12 +211,10 @@ public void ClearSceneId()
/// </summary>
public bool InitialState { get; private set; }

[Header("Runtime References")]

/// <summary>
/// The ServerObjectManager is present only for server/host instances.
/// </summary>
[ReadOnlyInspector]
[HideInInspector]
[Tooltip("Reference to Server set after the object is spawned. Used when debugging to see which server this object belongs to.")]
public ServerObjectManager ServerObjectManager;

Expand All @@ -226,9 +226,10 @@ public void ClearSceneId()
/// <summary>
/// The ClientObjectManager is present only for client instances.
/// </summary>
[ReadOnlyInspector]
[HideInInspector]
[Tooltip("Reference to Client set after the object is spawned. Used when debugging to see which client this object belongs to.")]
public ClientObjectManager ClientObjectManager;

private INetworkPlayer _owner;

/// <summary>
Expand Down Expand Up @@ -366,8 +367,6 @@ public INetworkVisibility Visibility
}
}

[SerializeField, HideInInspector] private int _prefabHash;

public int PrefabHash
{
get
Expand Down

0 comments on commit 8cc9179

Please sign in to comment.