ZUICreationWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using UnityEditor;
  8. using UnityEditor.Events;
  9. public class ZUICreationWindow : EditorWindow {
  10. private bool tryCreatingAgain;
  11. private Vector2 scrollPos;
  12. [MenuItem("Tools/ZUI/Creation Window...", false, 0)]
  13. public static void OpenWindow()
  14. {
  15. GetWindowWithRect(typeof(ZUICreationWindow), new Rect(0,0, 120, 235));
  16. }
  17. void OnGUI()
  18. {
  19. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  20. EditorGUILayout.BeginVertical();
  21. EditorGUILayout.BeginHorizontal();
  22. GUILayout.FlexibleSpace();
  23. if (GUILayout.Button("Setup", GUILayout.Height(40), GUILayout.Width(100)))
  24. Setup();
  25. GUILayout.FlexibleSpace();
  26. EditorGUILayout.EndHorizontal();
  27. #region Separator
  28. GUILayout.Space(3);
  29. EditorGUILayout.BeginHorizontal();
  30. GUILayout.FlexibleSpace();
  31. GUILayout.Box("", GUILayout.Height(3), GUILayout.Width(90));
  32. GUILayout.FlexibleSpace();
  33. EditorGUILayout.EndHorizontal();
  34. GUILayout.Space(3);
  35. #endregion
  36. EditorGUILayout.BeginHorizontal();
  37. GUILayout.FlexibleSpace();
  38. if (GUILayout.Button("Menu", GUILayout.Height(40), GUILayout.Width(100)))
  39. CreateMenu();
  40. GUILayout.FlexibleSpace();
  41. EditorGUILayout.EndHorizontal();
  42. EditorGUILayout.BeginHorizontal();
  43. GUILayout.FlexibleSpace();
  44. if (GUILayout.Button("Pop-up", GUILayout.Height(40), GUILayout.Width(100)))
  45. CreatePopup();
  46. GUILayout.FlexibleSpace();
  47. EditorGUILayout.EndHorizontal();
  48. EditorGUILayout.BeginHorizontal();
  49. GUILayout.FlexibleSpace();
  50. if (GUILayout.Button("Side-menu", GUILayout.Height(40), GUILayout.Width(100)))
  51. CreateSidemenu();
  52. GUILayout.FlexibleSpace();
  53. EditorGUILayout.EndHorizontal();
  54. EditorGUILayout.BeginHorizontal();
  55. GUILayout.FlexibleSpace();
  56. if (GUILayout.Button("Elements Group", GUILayout.Height(40), GUILayout.Width(100)))
  57. CreateElementsGroup();
  58. GUILayout.FlexibleSpace();
  59. EditorGUILayout.EndHorizontal();
  60. EditorGUILayout.EndHorizontal();
  61. EditorGUILayout.EndScrollView();
  62. }
  63. void Setup()
  64. {
  65. if (!FindObjectOfType<ZUIManager>())
  66. {
  67. GameObject mm = new GameObject("ZUIManager", typeof(ZUIManager));
  68. mm.transform.SetAsFirstSibling();
  69. Undo.RegisterCreatedObjectUndo(mm, "ZUI Setup");
  70. }
  71. if (!FindObjectOfType<SFXManager>())
  72. {
  73. GameObject sfxm = new GameObject("SFXManager", typeof(SFXManager));
  74. Undo.RegisterCreatedObjectUndo(sfxm, "ZUI Setup");
  75. sfxm.transform.SetSiblingIndex(1);
  76. SFXManager sfx = sfxm.GetComponent<SFXManager>();
  77. sfx.Sources = new List<AudioSource>();
  78. for (int i = 0; i < 10; i++)
  79. {
  80. GameObject s = new GameObject("Source (" + (i + 1) + ")", typeof(AudioSource));
  81. Undo.RegisterCreatedObjectUndo(s, "ZUI Setup");
  82. AudioSource audioSource = s.GetComponent<AudioSource>();
  83. audioSource.playOnAwake = false;
  84. sfx.Sources.Add(audioSource);
  85. s.transform.SetParent(sfxm.transform);
  86. }
  87. }
  88. }
  89. void CreateMenu()
  90. {
  91. Canvas c = CheckManagers(true);
  92. if (c != null)
  93. {
  94. ZUIManager manager = FindObjectOfType<ZUIManager>();
  95. Menu[] allMenus = FindObjectsOfType<Menu>();
  96. GameObject menu = Instantiate((GameObject)EditorGUIUtility.Load("ZUI/Templates/Menu.prefab"), c.transform);
  97. Undo.RegisterCreatedObjectUndo(menu, "Create Menu");
  98. menu.name = "Menu (" + (allMenus.Length + 1) + ")";
  99. RectTransform menuRT = menu.GetComponent<RectTransform>();
  100. menuRT.offsetMin = menuRT.offsetMax = Vector2.zero;
  101. Selection.activeGameObject = menu;
  102. Undo.RecordObject(manager, "Create Menu");
  103. manager.AllMenus.Add(menu.GetComponent<Menu>());
  104. }
  105. else
  106. {
  107. if (tryCreatingAgain)
  108. CreateMenu();
  109. }
  110. }
  111. void CreatePopup()
  112. {
  113. Canvas c = CheckManagers(true);
  114. if (c != null)
  115. {
  116. ZUIManager manager = FindObjectOfType<ZUIManager>();
  117. Popup[] allPopups = FindObjectsOfType<Popup>();
  118. GameObject popup = Instantiate((GameObject)EditorGUIUtility.Load("ZUI/Templates/Popup.prefab"), c.transform);
  119. Undo.RegisterCreatedObjectUndo(popup, "Create Pop-up");
  120. popup.name = "Popup (" + (allPopups.Length + 1) + ")";
  121. RectTransform popRT = popup.GetComponent<RectTransform>();
  122. popRT.offsetMin = popRT.offsetMax = Vector2.zero;
  123. Selection.activeGameObject = popup;
  124. Undo.RecordObject(manager, "Create Pop-up");
  125. manager.AllPopups.Add(popup.GetComponent<Popup>());
  126. Button b = popup.GetComponentInChildren<Button>();
  127. if (b)
  128. {
  129. UnityAction closeMethod = new UnityAction(FindObjectOfType<ZUIManager>().ClosePopup);
  130. UnityEventTools.AddPersistentListener(b.onClick, closeMethod);
  131. }
  132. }
  133. else
  134. {
  135. if (tryCreatingAgain)
  136. CreatePopup();
  137. }
  138. }
  139. void CreateSidemenu()
  140. {
  141. Canvas c = CheckManagers(true);
  142. if (c != null)
  143. {
  144. ZUIManager manager = FindObjectOfType<ZUIManager>();
  145. SideMenu[] allSideMenus = FindObjectsOfType<SideMenu>();
  146. GameObject menu = Instantiate((GameObject)EditorGUIUtility.Load("ZUI/Templates/SideMenu.prefab"), c.transform);
  147. Undo.RegisterCreatedObjectUndo(menu, "Create Side-menu");
  148. menu.name = "SideMenu (" + (allSideMenus.Length + 1) + ")";
  149. RectTransform smRT = menu.GetComponent<RectTransform>();
  150. smRT.offsetMin = smRT.offsetMax = Vector2.zero;
  151. Selection.activeGameObject = menu;
  152. Undo.RecordObject(manager, "Create Side-menu");
  153. manager.AllSideMenus.Add(menu.GetComponent<SideMenu>());
  154. Button b = menu.GetComponentInChildren<Button>();
  155. if (b)
  156. {
  157. UnityAction closeMethod = new UnityAction(FindObjectOfType<ZUIManager>().CloseSideMenu);
  158. UnityEventTools.AddPersistentListener(b.onClick, closeMethod);
  159. }
  160. }
  161. else
  162. {
  163. if (tryCreatingAgain)
  164. CreateSidemenu();
  165. }
  166. }
  167. void CreateElementsGroup()
  168. {
  169. Canvas c = CheckManagers(false);
  170. if (c != null)
  171. {
  172. UIElementsGroup[] allElementGroups = FindObjectsOfType<UIElementsGroup>();
  173. GameObject group = Instantiate((GameObject)EditorGUIUtility.Load("ZUI/Templates/UIElementsGroup.prefab"), c.transform);
  174. Undo.RegisterCreatedObjectUndo(group, "Create Elements Group");
  175. group.name = "ElementsGroup (" + (allElementGroups.Length + 1) + ")";
  176. RectTransform egRT = group.GetComponent<RectTransform>();
  177. egRT.offsetMin = egRT.offsetMax = Vector2.zero;
  178. Selection.activeGameObject = group;
  179. }
  180. else
  181. {
  182. if (tryCreatingAgain)
  183. CreateElementsGroup();
  184. }
  185. }
  186. Canvas CheckManagers(bool checkZUImanager)
  187. {
  188. if (checkZUImanager)
  189. {
  190. ZUIManager[] managers = FindObjectsOfType<ZUIManager>();
  191. if (managers.Length == 0)
  192. {
  193. Setup();
  194. tryCreatingAgain = true;
  195. Debug.Log("Couldn't find ZUIManager, had to setup the scene.");
  196. return null;
  197. }
  198. if (managers.Length > 1)
  199. {
  200. EditorUtility.DisplayDialog("Error", "More than 1 \"ZUIManager\" found, please make sure there's only one.", "OK");
  201. return null;
  202. }
  203. }
  204. Canvas c = null;
  205. if (Selection.activeGameObject)
  206. c = Selection.activeGameObject.GetComponentInParent<Canvas>();
  207. if (c == null)
  208. c = FindObjectOfType<Canvas>();
  209. if (c == null)
  210. {
  211. GameObject canv = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
  212. Undo.RegisterCreatedObjectUndo(canv, "Create Canvas");
  213. canv.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
  214. if (!FindObjectOfType<EventSystem>())
  215. {
  216. new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
  217. }
  218. tryCreatingAgain = true;
  219. }
  220. return c;
  221. }
  222. //Canvas CheckManagers(bool checkZUImanager)
  223. //{
  224. // if (checkZUImanager)
  225. // {
  226. // ZUIManager[] managers = FindObjectsOfType<ZUIManager>();
  227. // if (managers.Length == 0)
  228. // {
  229. // if (EditorUtility.DisplayDialog("Error", "No \"ZUIManager\" found in the scene, please click on \"Setup\" to add one.", "Setup", "Cancel"))
  230. // {
  231. // Setup();
  232. // tryCreatingAgain = true;
  233. // }
  234. // else
  235. // tryCreatingAgain = false;
  236. // return null;
  237. // }
  238. // if (managers.Length > 1)
  239. // {
  240. // EditorUtility.DisplayDialog("Error", "More than 1 \"ZUIManager\" found, please make sure there's only one.", "OK");
  241. // return null;
  242. // }
  243. // }
  244. // Canvas c = null;
  245. // if (Selection.activeGameObject)
  246. // c = Selection.activeGameObject.GetComponentInParent<Canvas>();
  247. // if (c == null)
  248. // c = FindObjectOfType<Canvas>();
  249. // if (c == null)
  250. // {
  251. // if (EditorUtility.DisplayDialog("Error", "No canvas found in the scene, please make sure there's at least one.", "Create Canvas", "Cancel"))
  252. // {
  253. // GameObject canv = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
  254. // Undo.RegisterCreatedObjectUndo(canv, "Create Canvas");
  255. // canv.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
  256. // if (!FindObjectOfType<EventSystem>())
  257. // {
  258. // new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
  259. // }
  260. // tryCreatingAgain = true;
  261. // }
  262. // else
  263. // {
  264. // tryCreatingAgain = false;
  265. // }
  266. // }
  267. // return c;
  268. //}
  269. }