Skip to content

Instantly share code, notes, and snippets.

@wallstop
Created October 10, 2024 02:39
Show Gist options
  • Save wallstop/b0487d3113b97eb19a3d011cb2997826 to your computer and use it in GitHub Desktop.
Save wallstop/b0487d3113b97eb19a3d011cb2997826 to your computer and use it in GitHub Desktop.
namespace Editor.Networking
{
using System.Collections.Generic;
using Core.Helper;
using global::Networking;
using Unity.Netcode;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(CollectAllNetworkPrefabs))]
public sealed class CollectAllNetworkPrefabsEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
NetworkManager networkManager = (target as CollectAllNetworkPrefabs).GetComponent<NetworkManager>();
if (GUILayout.Button("Update GlobalObjectIds"))
{
foreach (GameObject go in UnityHelpers.EnumeratePrefabs())
{
if (go.GetComponentsInChildren<NetworkObject>(true).Length <= 0)
{
continue;
}
UnityHelpers.ModifyAndSavePrefab(
go, networkGameObject =>
{
foreach (NetworkObject networkObject in networkGameObject
.GetComponentsInChildren<NetworkObject>(true))
{
networkObject.GenerateGlobalObjectIdHash();
}
});
}
}
if (GUILayout.Button("Update All Network Prefabs"))
{
UpdateNetworkManager(networkManager);
}
}
public static void UpdateNetworkManager(NetworkManager networkManager)
{
UnityHelpers.ModifyAndSavePrefab(
networkManager.gameObject, networkManagerGameObject =>
{
networkManager = networkManagerGameObject.GetComponent<NetworkManager>();
List<NetworkPrefab> networkPrefabs = networkManager.NetworkConfig.NetworkPrefabs;
networkPrefabs.Clear();
foreach (GameObject go in UnityHelpers.EnumeratePrefabs())
{
Buffers<NetworkObject>.List.Clear();
go.GetComponentsInChildren(true, Buffers<NetworkObject>.List);
foreach (NetworkObject networkObject in Buffers<NetworkObject>.List)
{
NetworkPrefab networkPrefab = new()
{
Prefab = networkObject.gameObject,
};
networkPrefabs.Add(networkPrefab);
}
}
networkPrefabs.Sort(
(lhs, rhs) =>
{
int comparison = string.CompareOrdinal(lhs.Prefab.name, rhs.Prefab.name);
if (comparison != 0)
{
return comparison;
}
string lhsAssetPath = AssetDatabase.GetAssetPath(lhs.Prefab);
string rhsAssetPath = AssetDatabase.GetAssetPath(rhs.Prefab);
return string.CompareOrdinal(lhsAssetPath, rhsAssetPath);
});
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment