You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.2 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. public class TowerEditor : EditorWindow {
  5. CastleAnimator rootObject;
  6. GameObject[] SelectedObjects;
  7. Vector2 scrollPos;
  8. public enum OPTIONS { SelectBases, SelectTowers, SelectBridges }
  9. public OPTIONS mode;
  10. public CastleAnimator.ScaleDirection scaleDirection;
  11. public CastleAnimator.ScaleDirection prevScaleDirection;
  12. public int index = 0;
  13. public int prevIndex = -1;
  14. public GameObject SelectedBase;
  15. // Add menu named "My Window" to the Window menu
  16. [MenuItem("Paco/TowerEditor")]
  17. static void Init()
  18. {
  19. // Get existing open window or if none, make a new one:
  20. TowerEditor window = (TowerEditor)EditorWindow.GetWindow(typeof(TowerEditor));
  21. window.Show();
  22. }
  23. #region GUI Functions
  24. void OnGUI()
  25. {
  26. GUILayout.Label("Root Object", EditorStyles.boldLabel);
  27. rootObject = (CastleAnimator)EditorGUILayout.ObjectField(rootObject, typeof(CastleAnimator), true);
  28. GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });
  29. mode = (OPTIONS)EditorGUILayout.EnumPopup(mode);
  30. //if (GUILayout.Button("FIX SCALE POINTS")) FixScalePoints();
  31. GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });
  32. switch (mode) {
  33. case OPTIONS.SelectBases:
  34. BaseGUI();
  35. break;
  36. case OPTIONS.SelectTowers:
  37. TowerGUI();
  38. break;
  39. case OPTIONS.SelectBridges:
  40. BridgeGUI();
  41. break;
  42. }
  43. }
  44. public void OnInspectorUpdate()
  45. {
  46. // This will only get called 10 times per second.
  47. Repaint();
  48. }
  49. public void BaseGUI()
  50. {
  51. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 100));
  52. foreach (Transform selection in Selection.transforms) {
  53. GUILayout.Label(selection.name);
  54. }
  55. EditorGUILayout.EndScrollView();
  56. GUI.enabled = (rootObject != null);
  57. if (GUILayout.Button("Add Bases")) {
  58. AddBase(Selection.transforms);
  59. }
  60. GUI.enabled = true;
  61. }
  62. public void TowerGUI()
  63. {
  64. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 100));
  65. foreach (Transform selection in Selection.transforms) {
  66. GUILayout.Label(selection.name);
  67. }
  68. EditorGUILayout.EndScrollView();
  69. EditorGUILayout.BeginHorizontal();
  70. GUI.enabled = (rootObject != null);
  71. string[] options = { "Select CastleObject" };
  72. if (rootObject != null)
  73. options = rootObject.GetBaseNames();
  74. index = EditorGUILayout.Popup(index, options);
  75. if (GUILayout.Button("Add Tower")) {
  76. AddTower(index, Selection.transforms);
  77. }
  78. GUI.enabled = true;
  79. EditorGUILayout.EndHorizontal();
  80. if (index != prevIndex) {
  81. ChangeTower(index);
  82. prevIndex = index;
  83. }
  84. }
  85. public void BridgeGUI()
  86. {
  87. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(position.height - 118));
  88. foreach (Transform selection in Selection.transforms) {
  89. if (rootObject != null && !rootObject.Bases[index].tower.TowerPieces.Contains(selection.gameObject)) {
  90. GUILayout.BeginHorizontal();
  91. GUILayout.Label(selection.name);
  92. GUILayout.Label((CheckInUse(selection))?"(In Use)": "", EditorStyles.boldLabel);
  93. GUILayout.EndHorizontal();
  94. } else if (rootObject == null) {
  95. GUILayout.Label(selection.name);
  96. }
  97. }
  98. EditorGUILayout.EndScrollView();
  99. GUI.enabled = (rootObject != null);
  100. scaleDirection = (CastleAnimator.ScaleDirection)EditorGUILayout.EnumPopup(scaleDirection);
  101. EditorGUILayout.BeginHorizontal();
  102. string[] options = { "Select CastleObject" };
  103. if (rootObject != null)
  104. options = rootObject.GetBaseNames();
  105. index = EditorGUILayout.Popup(index, options);
  106. if (GUILayout.Button("Add Bridge")) {
  107. AddBridge(index,scaleDirection , Selection.transforms);
  108. }
  109. GUI.enabled = true;
  110. EditorGUILayout.EndHorizontal();
  111. if (index != prevIndex || scaleDirection != prevScaleDirection) {
  112. ChangeBridge(index, scaleDirection);
  113. prevIndex = index;
  114. prevScaleDirection = scaleDirection;
  115. }
  116. }
  117. #endregion GUI Functions
  118. private void AddBase(Transform[] newBases)
  119. {
  120. foreach (Transform selectedBase in newBases) {
  121. rootObject.AddBase(selectedBase);
  122. }
  123. }
  124. private void FixScalePoints()
  125. {
  126. if (rootObject == null)
  127. return;
  128. for (int i = 0; i < rootObject.Bases.Count; i++) {
  129. CastleAnimator.Base tempBase = rootObject.Bases[i];
  130. tempBase.tower.ScalePoints = new GameObject[4];
  131. for (int j = 0; j < 4; j++) {
  132. tempBase.tower.ScalePoints[j] = new GameObject();
  133. tempBase.tower.ScalePoints[j].transform.parent = tempBase.gameObject.transform;
  134. tempBase.tower.ScalePoints[j].transform.localPosition = Vector3.zero;
  135. tempBase.tower.ScalePoints[j].name = "ScalePoint" + rootObject.Bases.Count + ", " + i;
  136. }
  137. rootObject.Bases[i] = tempBase;
  138. }
  139. }
  140. private void AddTower(int index, Transform[] newTower)
  141. {
  142. rootObject.AddTower(rootObject.Bases[index], newTower);
  143. }
  144. private void AddBridge(int index, CastleAnimator.ScaleDirection dir, Transform[] newBridge)
  145. {
  146. rootObject.AddBridge(rootObject.Bases[index],dir , newBridge);
  147. }
  148. private void ChangeTower(int index)
  149. {
  150. if (rootObject == null)
  151. return;
  152. CastleAnimator.Base curBase = rootObject.Bases[index];
  153. List<GameObject> Tower = new List<GameObject>();
  154. Tower.Add(curBase.gameObject);
  155. foreach (GameObject towerPiece in curBase.tower.TowerPieces)
  156. Tower.Add(towerPiece);
  157. Selection.objects = Tower.ToArray();
  158. }
  159. private void ChangeBridge(int index, CastleAnimator.ScaleDirection direction)
  160. {
  161. if (rootObject == null)
  162. return;
  163. CastleAnimator.Base curBase = rootObject.Bases[index];
  164. List<GameObject> Bridge = new List<GameObject>();
  165. //Bridge.Add(curBase.gameObject);
  166. foreach (GameObject towerPiece in curBase.tower.TowerPieces)
  167. Bridge.Add(towerPiece);
  168. foreach (Transform child in curBase.tower.ScalePoints[(int)direction].transform)
  169. Bridge.Add(child.gameObject);
  170. Selection.objects = Bridge.ToArray();
  171. }
  172. private bool CheckInUse(Transform selection)
  173. {
  174. if (rootObject == null)
  175. return false;
  176. foreach (CastleAnimator.Base Base in rootObject.Bases) {
  177. foreach(GameObject ScalePoint in Base.tower.ScalePoints) {
  178. if (selection.IsChildOf(ScalePoint.transform))
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. }