MyGridMapEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class MyGridMapEditor : EditorWindow
  6. {
  7. static GameObject selectObj;
  8. static MyGridMap gridMap;
  9. int row = 10;
  10. int col = 10;
  11. int w = 20;
  12. int h = 20;
  13. bool isShow = false;
  14. static int uiState = 0;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. }
  19. void OnGUI()
  20. {
  21. GUILayout.Label("目标地形预制体");
  22. gridMap = EditorGUILayout.ObjectField(gridMap, typeof(MyGridMap), true) as MyGridMap;
  23. switch(uiState)
  24. {
  25. case 1:
  26. GUILayout.Label("碰撞盒参数");
  27. row = EditorGUILayout.IntField(row);
  28. col = EditorGUILayout.IntField(col);
  29. w = EditorGUILayout.IntField(w);
  30. h = EditorGUILayout.IntField(h);
  31. MyGridMap.Row = row;
  32. MyGridMap.Col = col;
  33. MyGridMap.W = w;
  34. MyGridMap.H = h;
  35. if (GUILayout.Button("生成"))
  36. {
  37. selectObj = Selection.activeGameObject;
  38. gridMap = selectObj.GetOrAddComponent<MyGridMap>();
  39. for (int i = 0; i < row; ++i)
  40. {
  41. for (int j = 0; j < col; ++j)
  42. {
  43. gridMap.AddMyGrid(i, j, w, h);
  44. }
  45. }
  46. }
  47. break;
  48. case 2:
  49. if (GUILayout.Button("第一次匹配到网格"))
  50. {
  51. selectObj = Selection.activeGameObject;
  52. gridMap.MateObjToGrid(selectObj);
  53. }
  54. break;
  55. case 3:
  56. if (GUILayout.Button("添加到网格"))
  57. {
  58. selectObj = Selection.activeGameObject;
  59. gridMap.AddSubObjToGrid(selectObj);
  60. }
  61. break;
  62. case 4:
  63. if (GUILayout.Button("优化地形"))
  64. {
  65. selectObj = Selection.activeGameObject;
  66. gridMap.OptimizeTerrain(selectObj);
  67. }
  68. break;
  69. case 5:
  70. if (GUILayout.Button("去除碰撞"))
  71. {
  72. selectObj = Selection.activeGameObject;
  73. OnRmoveCollider(selectObj);
  74. }
  75. break;
  76. case 6:
  77. if (GUILayout.Button("显示/隐藏"))
  78. {
  79. selectObj = Selection.activeGameObject;
  80. OnShowOrHide(selectObj);
  81. }
  82. break;
  83. }
  84. }
  85. [MenuItem("地图/---网格创建---")]
  86. public static void CreateGridCollider()
  87. {
  88. uiState = 1;
  89. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  90. }
  91. [MenuItem("地图/---第一次网格匹配---")]
  92. public static void MateGrid()
  93. {
  94. uiState = 2;
  95. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  96. }
  97. [MenuItem("地图/---添加匹配---")]
  98. public static void AddGrid()
  99. {
  100. uiState = 3;
  101. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  102. }
  103. [MenuItem("地图/---优化地形---")]
  104. public static void OptimizeTerrain()
  105. {
  106. uiState = 4;
  107. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  108. }
  109. [MenuItem("地图/---去除碰撞---")]
  110. public static void RemoveCollider()
  111. {
  112. uiState = 5;
  113. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  114. }
  115. [MenuItem("地图/---显隐场景---")]
  116. public static void ShowOrHide()
  117. {
  118. uiState = 6;
  119. EditorWindow.GetWindow(typeof(MyGridMapEditor));
  120. }
  121. public void OnRmoveCollider(GameObject obj)
  122. {
  123. Collider[] cs = obj.GetComponentsInChildren<Collider>(true);
  124. for (int i = 0; i < cs.Length; ++i)
  125. {
  126. DestroyImmediate(cs[i]);
  127. }
  128. }
  129. public void OnShowOrHide(GameObject obj)
  130. {
  131. isShow = !isShow;
  132. MySplitTerrainTile[] cs = obj.GetComponentsInChildren<MySplitTerrainTile>(true);
  133. for (int i = 0; i < cs.Length; ++i)
  134. {
  135. cs[i].ShowOrHide(isShow);
  136. }
  137. }
  138. }