UICreateService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using UnityEngine.UI;
  5. using System;
  6. using FrameWork.GuideSystem;
  7. public class UICreateService
  8. {
  9. public static void CreatUIManager(Vector2 referenceResolution, CanvasScaler.ScreenMatchMode MatchMode, bool isOnlyUICamera, bool isVertical)
  10. {
  11. //新增五个层级
  12. EditorExpand.AddSortLayerIfNotExist("GameUI");
  13. EditorExpand.AddSortLayerIfNotExist("Fixed");
  14. EditorExpand.AddSortLayerIfNotExist("Normal");
  15. EditorExpand.AddSortLayerIfNotExist("TopBar");
  16. EditorExpand.AddSortLayerIfNotExist("PopUp");
  17. //UIManager
  18. GameObject UIManagerGo = new GameObject("UIManager");
  19. UIManagerGo.layer = LayerMask.NameToLayer("UI");
  20. UIManager UIManager = UIManagerGo.AddComponent<UIManager>();
  21. CreateUICamera(UIManager, "DefaultUI",1, referenceResolution, MatchMode, isOnlyUICamera, isVertical);
  22. ProjectWindowUtil.ShowCreatedAsset(UIManagerGo);
  23. //保存UIManager
  24. ReSaveUIManager(UIManagerGo);
  25. }
  26. public static void CreateUICamera(UIManager UIManager,string key, float cameraDepth, Vector2 referenceResolution, CanvasScaler.ScreenMatchMode MatchMode, bool isOnlyUICamera, bool isVertical)
  27. {
  28. UILayerManager.UICameraData uICameraData = new UILayerManager.UICameraData();
  29. uICameraData.m_key = key;
  30. GameObject UIManagerGo = UIManager.gameObject;
  31. GameObject canvas = new GameObject(key);
  32. RectTransform canvasRt = canvas.AddComponent<RectTransform>();
  33. canvasRt.SetParent(UIManagerGo.transform);
  34. uICameraData.m_root = canvas;
  35. //UIcamera
  36. GameObject cameraGo = new GameObject("UICamera");
  37. cameraGo.transform.SetParent(canvas.transform);
  38. cameraGo.transform.localPosition = new Vector3(0, 0, -5000);
  39. Camera camera = cameraGo.AddComponent<Camera>();
  40. camera.cullingMask = LayerMask.GetMask("UI");
  41. camera.orthographic = true;
  42. camera.depth = cameraDepth;
  43. uICameraData.m_camera = camera;
  44. //Canvas
  45. Canvas canvasComp = canvas.AddComponent<Canvas>();
  46. canvasComp.renderMode = RenderMode.ScreenSpaceCamera;
  47. canvasComp.worldCamera = camera;
  48. //UI Raycaster
  49. canvas.AddComponent<GraphicRaycaster>();
  50. //CanvasScaler
  51. CanvasScaler scaler = canvas.AddComponent<CanvasScaler>();
  52. scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
  53. scaler.referenceResolution = referenceResolution;
  54. scaler.screenMatchMode = MatchMode;
  55. if (!isOnlyUICamera)
  56. {
  57. camera.clearFlags = CameraClearFlags.Depth;
  58. camera.depth = 1;
  59. }
  60. else
  61. {
  62. camera.clearFlags = CameraClearFlags.SolidColor;
  63. camera.backgroundColor = Color.black;
  64. }
  65. if (isVertical)
  66. {
  67. scaler.matchWidthOrHeight = 1;
  68. }
  69. else
  70. {
  71. scaler.matchWidthOrHeight = 0;
  72. }
  73. //挂载点
  74. GameObject goTmp = null;
  75. RectTransform rtTmp = null;
  76. UILayerManager UILayerManager = UIManagerGo.GetComponent<UILayerManager>();
  77. goTmp = new GameObject("GameUI");
  78. goTmp.layer = LayerMask.NameToLayer("UI");
  79. goTmp.transform.SetParent(canvas.transform);
  80. goTmp.transform.localScale = Vector3.one;
  81. rtTmp = goTmp.AddComponent<RectTransform>();
  82. rtTmp.anchorMax = new Vector2(1, 1);
  83. rtTmp.anchorMin = new Vector2(0, 0);
  84. rtTmp.anchoredPosition3D = Vector3.zero;
  85. rtTmp.sizeDelta = Vector2.zero;
  86. uICameraData.m_GameUILayerParent = goTmp.transform;
  87. goTmp = new GameObject("Fixed");
  88. goTmp.layer = LayerMask.NameToLayer("UI");
  89. goTmp.transform.SetParent(canvas.transform);
  90. goTmp.transform.localScale = Vector3.one;
  91. rtTmp = goTmp.AddComponent<RectTransform>();
  92. rtTmp.anchorMax = new Vector2(1, 1);
  93. rtTmp.anchorMin = new Vector2(0, 0);
  94. rtTmp.anchoredPosition3D = Vector3.zero;
  95. rtTmp.sizeDelta = Vector2.zero;
  96. uICameraData.m_FixedLayerParent = goTmp.transform;
  97. goTmp = new GameObject("Normal");
  98. goTmp.layer = LayerMask.NameToLayer("UI");
  99. goTmp.transform.SetParent(canvas.transform);
  100. goTmp.transform.localScale = Vector3.one;
  101. rtTmp = goTmp.AddComponent<RectTransform>();
  102. rtTmp.anchorMax = new Vector2(1, 1);
  103. rtTmp.anchorMin = new Vector2(0, 0);
  104. rtTmp.anchoredPosition3D = Vector3.zero;
  105. rtTmp.sizeDelta = Vector2.zero;
  106. uICameraData.m_NormalLayerParent = goTmp.transform;
  107. goTmp = new GameObject("TopBar");
  108. goTmp.layer = LayerMask.NameToLayer("UI");
  109. goTmp.transform.SetParent(canvas.transform);
  110. goTmp.transform.localScale = Vector3.one;
  111. rtTmp = goTmp.AddComponent<RectTransform>();
  112. rtTmp.anchorMax = new Vector2(1, 1);
  113. rtTmp.anchorMin = new Vector2(0, 0);
  114. rtTmp.anchoredPosition3D = Vector3.zero;
  115. rtTmp.sizeDelta = Vector2.zero;
  116. uICameraData.m_TopbarLayerParent = goTmp.transform;
  117. goTmp = new GameObject("PopUp");
  118. goTmp.layer = LayerMask.NameToLayer("UI");
  119. goTmp.transform.SetParent(canvas.transform);
  120. goTmp.transform.localScale = Vector3.one;
  121. rtTmp = goTmp.AddComponent<RectTransform>();
  122. rtTmp.anchorMax = new Vector2(1, 1);
  123. rtTmp.anchorMin = new Vector2(0, 0);
  124. rtTmp.anchoredPosition3D = Vector3.zero;
  125. rtTmp.sizeDelta = Vector2.zero;
  126. uICameraData.m_PopUpLayerParent = goTmp.transform;
  127. UILayerManager.UICameraList.Add(uICameraData);
  128. //重新保存
  129. ReSaveUIManager(UIManagerGo);
  130. }
  131. static void ReSaveUIManager(GameObject UIManagerGo)
  132. {
  133. string Path = "Resources/UI/UIManager.prefab";
  134. FileTool.CreatFilePath(Application.dataPath + "/" + Path);
  135. PrefabUtility.CreatePrefab("Assets/" + Path, UIManagerGo, ReplacePrefabOptions.ConnectToPrefab);
  136. }
  137. public static void CreatUI(string UIWindowName, string UIcameraKey,UIType UIType,UILayerManager UILayerManager,bool isAutoCreatePrefab)
  138. {
  139. GameObject uiGo = new GameObject(UIWindowName);
  140. Type type = EditorTool.GetType(UIWindowName);
  141. UIWindowBase uiBaseTmp = uiGo.AddComponent(type) as UIWindowBase;
  142. uiGo.layer = LayerMask.NameToLayer("UI");
  143. uiBaseTmp.m_UIType = UIType;
  144. Canvas canvas = uiGo.AddComponent<Canvas>();
  145. if(EditorExpand.isExistShortLayer(UIType.ToString()))
  146. {
  147. canvas.overrideSorting = true;
  148. canvas.sortingLayerName = UIType.ToString();
  149. }
  150. uiGo.AddComponent<GraphicRaycaster>();
  151. RectTransform ui = uiGo.GetComponent<RectTransform>();
  152. ui.sizeDelta = Vector2.zero;
  153. ui.anchorMin = Vector2.zero;
  154. ui.anchorMax = Vector2.one;
  155. GameObject BgGo = new GameObject("BG");
  156. BgGo.layer = LayerMask.NameToLayer("UI");
  157. RectTransform Bg = BgGo.AddComponent<RectTransform>();
  158. Bg.SetParent(ui);
  159. Bg.sizeDelta = Vector2.zero;
  160. Bg.anchorMin = Vector2.zero;
  161. Bg.anchorMax = Vector2.one;
  162. GameObject rootGo = new GameObject("root");
  163. rootGo.layer = LayerMask.NameToLayer("UI");
  164. RectTransform root = rootGo.AddComponent<RectTransform>();
  165. root.SetParent(ui);
  166. root.sizeDelta = Vector2.zero;
  167. root.anchorMin = Vector2.zero;
  168. root.anchorMax = Vector2.one;
  169. uiBaseTmp.m_bgMask = BgGo;
  170. uiBaseTmp.m_uiRoot = rootGo;
  171. if (UILayerManager)
  172. {
  173. UILayerManager.SetLayer(uiBaseTmp);
  174. }
  175. if (isAutoCreatePrefab)
  176. {
  177. string Path = "Resources/UI/" + UIWindowName + "/" + UIWindowName + ".prefab";
  178. FileTool.CreatFilePath(Application.dataPath + "/" + Path);
  179. PrefabUtility.CreatePrefab("Assets/" + Path, uiGo, ReplacePrefabOptions.ConnectToPrefab);
  180. }
  181. ProjectWindowUtil.ShowCreatedAsset(uiGo);
  182. }
  183. public static void CreatUIbyLua(string UIWindowName, UIType UIType, UILayerManager UILayerManager, bool isAutoCreatePrefab)
  184. {
  185. GameObject uiGo = new GameObject(UIWindowName);
  186. UIWindowLuaHelper uiBaseTmp = uiGo.AddComponent<UIWindowLuaHelper>();
  187. uiGo.layer = LayerMask.NameToLayer("UI");
  188. uiBaseTmp.m_UIType = UIType;
  189. uiGo.AddComponent<Canvas>();
  190. uiGo.AddComponent<GraphicRaycaster>();
  191. RectTransform ui = uiGo.GetComponent<RectTransform>();
  192. ui.sizeDelta = Vector2.zero;
  193. ui.anchorMin = Vector2.zero;
  194. ui.anchorMax = Vector2.one;
  195. GameObject BgGo = new GameObject("BG");
  196. BgGo.layer = LayerMask.NameToLayer("UI");
  197. RectTransform Bg = BgGo.AddComponent<RectTransform>();
  198. Bg.SetParent(ui);
  199. Bg.sizeDelta = Vector2.zero;
  200. Bg.anchorMin = Vector2.zero;
  201. Bg.anchorMax = Vector2.one;
  202. GameObject rootGo = new GameObject("root");
  203. rootGo.layer = LayerMask.NameToLayer("UI");
  204. RectTransform root = rootGo.AddComponent<RectTransform>();
  205. root.SetParent(ui);
  206. root.sizeDelta = Vector2.zero;
  207. root.anchorMin = Vector2.zero;
  208. root.anchorMax = Vector2.one;
  209. uiBaseTmp.m_bgMask = BgGo;
  210. uiBaseTmp.m_uiRoot = rootGo;
  211. if (UILayerManager)
  212. {
  213. UILayerManager.SetLayer(uiBaseTmp);
  214. }
  215. if (isAutoCreatePrefab)
  216. {
  217. string Path = "Resources/UI/" + UIWindowName + "/" + UIWindowName + ".prefab";
  218. FileTool.CreatFilePath(Application.dataPath + "/" + Path);
  219. PrefabUtility.CreatePrefab("Assets/" + Path, uiGo, ReplacePrefabOptions.ConnectToPrefab);
  220. }
  221. ProjectWindowUtil.ShowCreatedAsset(uiGo);
  222. }
  223. public static void CreatUIScript(string UIWindowName)
  224. {
  225. string LoadPath = Application.dataPath + "/Script/Core/Editor/res/UIWindowClassTemplate.txt";
  226. string SavePath = Application.dataPath + "/Script/UI/" + UIWindowName + "/" + UIWindowName + ".cs";
  227. string UItemplate = ResourceIOTool.ReadStringByFile(LoadPath);
  228. string classContent = UItemplate.Replace("{0}", UIWindowName);
  229. EditorUtil.WriteStringByFile(SavePath, classContent);
  230. AssetDatabase.Refresh();
  231. }
  232. public static void CreatUILuaScript(string UIWindowName)
  233. {
  234. string LoadPath = Application.dataPath + "/Script/Core/Editor/res/UILuaScriptTemplate.txt";
  235. string SavePath = Application.dataPath + "/Resources/Lua/UI/Lua" + UIWindowName + ".txt";
  236. string UItemplate = ResourceIOTool.ReadStringByFile(LoadPath);
  237. string classContent = UItemplate.Replace("{0}", UIWindowName);
  238. EditorUtil.WriteStringByFile(SavePath, classContent);
  239. AssetDatabase.Refresh();
  240. }
  241. public static void CreateGuideWindow()
  242. {
  243. string UIWindowName = "GuideWindow";
  244. UIType UIType = UIType.TopBar;
  245. GameObject uiGo = new GameObject(UIWindowName);
  246. Type type = EditorTool.GetType(UIWindowName);
  247. GuideWindowBase guideBaseTmp = uiGo.AddComponent(type) as GuideWindowBase;
  248. uiGo.layer = LayerMask.NameToLayer("UI");
  249. guideBaseTmp.m_UIType = UIType;
  250. Canvas can = uiGo.AddComponent<Canvas>();
  251. uiGo.AddComponent<GraphicRaycaster>();
  252. can.overrideSorting = true;
  253. can.sortingLayerName = "Guide";
  254. RectTransform ui = uiGo.GetComponent<RectTransform>();
  255. ui.sizeDelta = Vector2.zero;
  256. ui.anchorMin = Vector2.zero;
  257. ui.anchorMax = Vector2.one;
  258. GameObject BgGo = new GameObject("BG");
  259. BgGo.layer = LayerMask.NameToLayer("UI");
  260. RectTransform Bg = BgGo.AddComponent<RectTransform>();
  261. Bg.SetParent(ui);
  262. Bg.sizeDelta = Vector2.zero;
  263. Bg.anchorMin = Vector2.zero;
  264. Bg.anchorMax = Vector2.one;
  265. GameObject rootGo = new GameObject("root");
  266. rootGo.layer = LayerMask.NameToLayer("UI");
  267. RectTransform root = rootGo.AddComponent<RectTransform>();
  268. root.SetParent(ui);
  269. root.sizeDelta = Vector2.zero;
  270. root.anchorMin = Vector2.zero;
  271. root.anchorMax = Vector2.one;
  272. GameObject mask = new GameObject("mask");
  273. mask.layer = LayerMask.NameToLayer("UI");
  274. RectTransform maskrt = mask.AddComponent<RectTransform>();
  275. Image img = mask.AddComponent<Image>();
  276. img.color = new Color(0, 0, 0, 0.75f);
  277. maskrt.SetParent(root);
  278. maskrt.sizeDelta = Vector2.zero;
  279. maskrt.anchorMin = Vector2.zero;
  280. maskrt.anchorMax = Vector2.one;
  281. GameObject tips = new GameObject("Tips");
  282. tips.layer = LayerMask.NameToLayer("UI");
  283. RectTransform tipsrt = tips.AddComponent<RectTransform>();
  284. tipsrt.SetParent(root);
  285. guideBaseTmp.m_objectList.Add(tips);
  286. GameObject Text_tips = new GameObject("Text_tip");
  287. Text_tips.layer = LayerMask.NameToLayer("UI");
  288. RectTransform txt_tipsrt = Text_tips.AddComponent<RectTransform>();
  289. //Text text = Text_tips.AddComponent<Text>();
  290. txt_tipsrt.SetParent(tipsrt);
  291. guideBaseTmp.m_objectList.Add(Text_tips);
  292. //guideBaseTmp.m_mask = img;
  293. //guideBaseTmp.m_TipText = text;
  294. //guideBaseTmp.m_TipTransfrom = txt_tipsrt;
  295. guideBaseTmp.m_bgMask = BgGo;
  296. guideBaseTmp.m_uiRoot = rootGo;
  297. string Path = "Resources/UI/" + UIWindowName + "/" + UIWindowName + ".prefab";
  298. FileTool.CreatFilePath(Application.dataPath + "/" + Path);
  299. PrefabUtility.CreatePrefab("Assets/" + Path, uiGo, ReplacePrefabOptions.ConnectToPrefab);
  300. ProjectWindowUtil.ShowCreatedAsset(uiGo);
  301. }
  302. }