NetworkManagerEditor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7. namespace Mirror
  8. {
  9. [CustomEditor(typeof(NetworkManager), true)]
  10. [CanEditMultipleObjects]
  11. public class NetworkManagerEditor : Editor
  12. {
  13. SerializedProperty spawnListProperty;
  14. ReorderableList spawnList;
  15. protected NetworkManager networkManager;
  16. protected void Init()
  17. {
  18. if (spawnList == null)
  19. {
  20. networkManager = target as NetworkManager;
  21. spawnListProperty = serializedObject.FindProperty("spawnPrefabs");
  22. spawnList = new ReorderableList(serializedObject, spawnListProperty)
  23. {
  24. drawHeaderCallback = DrawHeader,
  25. drawElementCallback = DrawChild,
  26. onReorderCallback = Changed,
  27. onRemoveCallback = RemoveButton,
  28. onChangedCallback = Changed,
  29. onAddCallback = AddButton,
  30. // this uses a 16x16 icon. other sizes make it stretch.
  31. elementHeight = 16
  32. };
  33. }
  34. }
  35. public override void OnInspectorGUI()
  36. {
  37. Init();
  38. DrawDefaultInspector();
  39. EditorGUI.BeginChangeCheck();
  40. spawnList.DoLayoutList();
  41. if (EditorGUI.EndChangeCheck())
  42. {
  43. serializedObject.ApplyModifiedProperties();
  44. }
  45. if (GUILayout.Button("Populate Spawnable Prefabs"))
  46. {
  47. ScanForNetworkIdentities();
  48. }
  49. }
  50. void ScanForNetworkIdentities()
  51. {
  52. List<GameObject> identities = new List<GameObject>();
  53. bool cancelled = false;
  54. try
  55. {
  56. string[] paths = EditorHelper.IterateOverProject("t:prefab").ToArray();
  57. int count = 0;
  58. foreach (string path in paths)
  59. {
  60. // ignore test & example prefabs.
  61. // users sometimes keep the folders in their projects.
  62. if (path.Contains("Mirror/Tests/") ||
  63. path.Contains("Mirror/Examples/"))
  64. {
  65. continue;
  66. }
  67. if (EditorUtility.DisplayCancelableProgressBar("Searching for NetworkIdentities..",
  68. $"Scanned {count}/{paths.Length} prefabs. Found {identities.Count} new ones",
  69. count / (float)paths.Length))
  70. {
  71. cancelled = true;
  72. break;
  73. }
  74. count++;
  75. NetworkIdentity ni = AssetDatabase.LoadAssetAtPath<NetworkIdentity>(path);
  76. if (!ni)
  77. {
  78. continue;
  79. }
  80. if (!networkManager.spawnPrefabs.Contains(ni.gameObject))
  81. {
  82. identities.Add(ni.gameObject);
  83. }
  84. }
  85. }
  86. finally
  87. {
  88. EditorUtility.ClearProgressBar();
  89. if (!cancelled)
  90. {
  91. // RecordObject is needed for "*" to show up in Scene.
  92. // however, this only saves List.Count without the entries.
  93. Undo.RecordObject(networkManager, "NetworkManager: populated prefabs");
  94. // add the entries
  95. networkManager.spawnPrefabs.AddRange(identities);
  96. // sort alphabetically for better UX
  97. networkManager.spawnPrefabs = networkManager.spawnPrefabs.OrderBy(go => go.name).ToList();
  98. // SetDirty is required to save the individual entries properly.
  99. EditorUtility.SetDirty(target);
  100. }
  101. // Loading assets might use a lot of memory, so try to unload them after
  102. Resources.UnloadUnusedAssets();
  103. }
  104. }
  105. static void DrawHeader(Rect headerRect)
  106. {
  107. GUI.Label(headerRect, "Registered Spawnable Prefabs:");
  108. }
  109. internal void DrawChild(Rect r, int index, bool isActive, bool isFocused)
  110. {
  111. SerializedProperty prefab = spawnListProperty.GetArrayElementAtIndex(index);
  112. GameObject go = (GameObject)prefab.objectReferenceValue;
  113. GUIContent label;
  114. if (go == null)
  115. {
  116. label = new GUIContent("Empty", "Drag a prefab with a NetworkIdentity here");
  117. }
  118. else
  119. {
  120. NetworkIdentity identity = go.GetComponent<NetworkIdentity>();
  121. label = new GUIContent(go.name, identity != null ? $"AssetId: [{identity.assetId}]" : "No Network Identity");
  122. }
  123. GameObject newGameObject = (GameObject)EditorGUI.ObjectField(r, label, go, typeof(GameObject), false);
  124. if (newGameObject != go)
  125. {
  126. if (newGameObject != null && !newGameObject.GetComponent<NetworkIdentity>())
  127. {
  128. Debug.LogError($"Prefab {newGameObject} cannot be added as spawnable as it doesn't have a NetworkIdentity.");
  129. return;
  130. }
  131. prefab.objectReferenceValue = newGameObject;
  132. }
  133. }
  134. internal void Changed(ReorderableList list)
  135. {
  136. EditorUtility.SetDirty(target);
  137. }
  138. internal void AddButton(ReorderableList list)
  139. {
  140. spawnListProperty.arraySize += 1;
  141. list.index = spawnListProperty.arraySize - 1;
  142. SerializedProperty obj = spawnListProperty.GetArrayElementAtIndex(spawnListProperty.arraySize - 1);
  143. obj.objectReferenceValue = null;
  144. spawnList.index = spawnList.count - 1;
  145. Changed(list);
  146. }
  147. internal void RemoveButton(ReorderableList list)
  148. {
  149. spawnListProperty.DeleteArrayElementAtIndex(spawnList.index);
  150. if (list.index >= spawnListProperty.arraySize)
  151. {
  152. list.index = spawnListProperty.arraySize - 1;
  153. }
  154. }
  155. }
  156. }