NetworkManagerEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEditor;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. namespace Mirror
  5. {
  6. [CustomEditor(typeof(NetworkManager), true)]
  7. [CanEditMultipleObjects]
  8. public class NetworkManagerEditor : Editor
  9. {
  10. SerializedProperty spawnListProperty;
  11. ReorderableList spawnList;
  12. protected NetworkManager networkManager;
  13. protected void Init()
  14. {
  15. if (spawnList == null)
  16. {
  17. networkManager = target as NetworkManager;
  18. spawnListProperty = serializedObject.FindProperty("spawnPrefabs");
  19. spawnList = new ReorderableList(serializedObject, spawnListProperty)
  20. {
  21. drawHeaderCallback = DrawHeader,
  22. drawElementCallback = DrawChild,
  23. onReorderCallback = Changed,
  24. onRemoveCallback = RemoveButton,
  25. onChangedCallback = Changed,
  26. onAddCallback = AddButton,
  27. // this uses a 16x16 icon. other sizes make it stretch.
  28. elementHeight = 16
  29. };
  30. }
  31. }
  32. public override void OnInspectorGUI()
  33. {
  34. Init();
  35. DrawDefaultInspector();
  36. EditorGUI.BeginChangeCheck();
  37. spawnList.DoLayoutList();
  38. if (EditorGUI.EndChangeCheck())
  39. {
  40. serializedObject.ApplyModifiedProperties();
  41. }
  42. }
  43. static void DrawHeader(Rect headerRect)
  44. {
  45. GUI.Label(headerRect, "Registered Spawnable Prefabs:");
  46. }
  47. internal void DrawChild(Rect r, int index, bool isActive, bool isFocused)
  48. {
  49. SerializedProperty prefab = spawnListProperty.GetArrayElementAtIndex(index);
  50. GameObject go = (GameObject)prefab.objectReferenceValue;
  51. GUIContent label;
  52. if (go == null)
  53. {
  54. label = new GUIContent("Empty", "Drag a prefab with a NetworkIdentity here");
  55. }
  56. else
  57. {
  58. NetworkIdentity identity = go.GetComponent<NetworkIdentity>();
  59. label = new GUIContent(go.name, identity != null ? "AssetId: [" + identity.assetId + "]" : "No Network Identity");
  60. }
  61. GameObject newGameObject = (GameObject)EditorGUI.ObjectField(r, label, go, typeof(GameObject), false);
  62. if (newGameObject != go)
  63. {
  64. if (newGameObject != null && !newGameObject.GetComponent<NetworkIdentity>())
  65. {
  66. Debug.LogError("Prefab " + newGameObject + " cannot be added as spawnable as it doesn't have a NetworkIdentity.");
  67. return;
  68. }
  69. prefab.objectReferenceValue = newGameObject;
  70. }
  71. }
  72. internal void Changed(ReorderableList list)
  73. {
  74. EditorUtility.SetDirty(target);
  75. }
  76. internal void AddButton(ReorderableList list)
  77. {
  78. spawnListProperty.arraySize += 1;
  79. list.index = spawnListProperty.arraySize - 1;
  80. SerializedProperty obj = spawnListProperty.GetArrayElementAtIndex(spawnListProperty.arraySize - 1);
  81. obj.objectReferenceValue = null;
  82. spawnList.index = spawnList.count - 1;
  83. Changed(list);
  84. }
  85. internal void RemoveButton(ReorderableList list)
  86. {
  87. spawnListProperty.DeleteArrayElementAtIndex(spawnList.index);
  88. if (list.index >= spawnListProperty.arraySize)
  89. {
  90. list.index = spawnListProperty.arraySize - 1;
  91. }
  92. }
  93. }
  94. }