ManaReso.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System;
  5. using System.Collections;
  6. using System.Linq.Expressions;
  7. using System.ComponentModel;
  8. using System.Collections.Generic;
  9. using Object = UnityEngine.Object;
  10. using Component = UnityEngine.Component;
  11. public enum Folder
  12. {
  13. UI,
  14. Skill,
  15. Config,
  16. Object,
  17. Shader,
  18. Garden,
  19. Character,
  20. }
  21. public enum ObjType
  22. {
  23. Null,
  24. Flower,
  25. HudText,
  26. FlowerItem,
  27. }
  28. public class ManaReso : Regist
  29. {
  30. #region 变量
  31. public static Dictionary<string, Object> ObjDic;
  32. public static Dictionary<string, Transform> TraDic;
  33. public static Dictionary<ObjType, List<Transform>> ObjectPool;
  34. #endregion
  35. public override void Instantiate()
  36. {
  37. TraDic = new Dictionary<string, Transform>();
  38. ObjDic = new Dictionary<string, Object>();
  39. ObjectPool = new Dictionary<ObjType, List<Transform>>();
  40. Transform objPool = new GameObject("ObjPool").transform;
  41. objPool.parent = transform;
  42. objPool.SetActive(false);
  43. TraDic.Add(objPool.name, objPool);
  44. }
  45. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  46. {
  47. Object obj;
  48. if (ObjDic.TryGetValue(goName, out obj))
  49. {
  50. return (T)obj;
  51. }
  52. else
  53. {
  54. T t;
  55. if (folder == Folder.UI)
  56. {
  57. t = Bundle.UI.LoadAsset<T>(goName);
  58. if (t == null)
  59. {
  60. t = Bundle.TempUI.LoadAsset<T>(goName);
  61. }
  62. }
  63. else if (folder == Folder.Skill)
  64. {
  65. t = Bundle.Skill.LoadAsset<T>(goName);
  66. if (t == null)
  67. {
  68. t = Bundle.TempSkill.LoadAsset<T>(goName);
  69. }
  70. }
  71. else if (folder == Folder.Config)
  72. {
  73. t = Bundle.Config.LoadAsset<T>(goName);
  74. if (t == null)
  75. {
  76. t = Bundle.TempConfig.LoadAsset<T>(goName);
  77. }
  78. }
  79. else if (folder == Folder.Object)
  80. {
  81. t = Bundle.Object.LoadAsset<T>(goName);
  82. if (t == null)
  83. {
  84. t = Bundle.TempObject.LoadAsset<T>(goName);
  85. }
  86. }
  87. else if (folder == Folder.Shader)
  88. {
  89. t = Bundle.Shader.LoadAsset<T>(goName);
  90. if (t == null)
  91. {
  92. t = Bundle.TempShader.LoadAsset<T>(goName);
  93. }
  94. }
  95. else if (folder == Folder.Garden)
  96. {
  97. t = Bundle.Garden.LoadAsset<T>(goName);
  98. if (t == null)
  99. {
  100. t = Bundle.TempGarden.LoadAsset<T>(goName);
  101. }
  102. }
  103. else if (folder == Folder.Character)
  104. {
  105. t = Bundle.Character.LoadAsset<T>(goName);
  106. if (t == null)
  107. {
  108. t = Bundle.TempCharacter.LoadAsset<T>(goName);
  109. }
  110. }
  111. else
  112. {
  113. throw new Exception();
  114. }
  115. if (t == null)
  116. {
  117. throw new Exception(goName);
  118. }
  119. ObjDic.Add(goName, t);
  120. if (objType != ObjType.Null)
  121. {
  122. ObjectPool.Add(objType, new List<Transform>());
  123. }
  124. return t;
  125. }
  126. }
  127. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null)
  128. {
  129. Transform tra = Get(objType);
  130. if (tra != null)
  131. {
  132. tra.SetParent(par);
  133. tra.position = pos;
  134. return tra;
  135. }
  136. else
  137. {
  138. GameObject go = Load<GameObject>(goName, folder, objType);
  139. go = Instantiate(go, pos, Quaternion.identity, par);
  140. go.name = go.name.Replace("(Clone)", "");
  141. if (compile)
  142. {
  143. Auxiliary.CompileDic(go.transform, TraDic);
  144. }
  145. return go.transform;
  146. }
  147. }
  148. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null)
  149. {
  150. Transform tra = Get(objType);
  151. if (tra == null)
  152. {
  153. GameObject go = Load<GameObject>(goName, folder, objType);
  154. go = Instantiate(go, par, worldSpace);
  155. go.name = go.name.Replace("(Clone)", "");
  156. if (compile)
  157. {
  158. Auxiliary.CompileDic(go.transform, TraDic);
  159. }
  160. return go.transform;
  161. }
  162. else
  163. {
  164. tra.SetParent(par);
  165. return tra;
  166. }
  167. }
  168. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  169. {
  170. Transform tra = Get("Flower", Folder.Garden, false, slot.transform, false, ObjType.Flower);
  171. Flower flower = tra.GetComponent<Flower>();
  172. if (flower == null)
  173. {
  174. flower = tra.AddComponent<Flower>();
  175. flower.ObjType = ObjType.Flower;
  176. flower.LocalPos = flower.transform.localPosition;
  177. }
  178. else
  179. {
  180. flower.GoldBk.SetActive(false);
  181. flower.transform.localPosition = flower.LocalPos;
  182. }
  183. flower.FlowerInfo = flowerInfo;
  184. flower.Slot = slot;
  185. flower.SetCollider(collider);
  186. return flower;
  187. }
  188. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  189. {
  190. Transform tra = Get("Flower", Folder.Garden, false, par, false, ObjType.Flower);
  191. Flower flower = tra.GetComponent<Flower>();
  192. if (flower == null)
  193. {
  194. flower = tra.AddComponent<Flower>();
  195. flower.ObjType = ObjType.Flower;
  196. flower.LocalPos = flower.transform.localPosition;
  197. }
  198. else
  199. {
  200. flower.GoldBk.SetActive(false);
  201. flower.transform.localPosition = flower.LocalPos;
  202. }
  203. flower.FlowerInfo = flowerInfo;
  204. flower.SetCollider(false);
  205. return flower;
  206. }
  207. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene)
  208. {
  209. Vector3 pos;
  210. if (scene)
  211. {
  212. pos = Camera.main.WorldToScreenPoint(posTra.position);
  213. }
  214. else
  215. {
  216. pos = posTra.position;
  217. }
  218. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText);
  219. HudText hudText = tra.GetComponent<HudText>();
  220. if (hudText == null)
  221. {
  222. hudText = tra.AddComponent<HudText>();
  223. hudText.ObjType = ObjType.HudText;
  224. }
  225. hudText.Show(str, color, size);
  226. return hudText;
  227. }
  228. public static Transform GetSkillItem(SkillRoot skillRoot)
  229. {
  230. Transform tra;
  231. if (skillRoot.SkillTab == SkillTab.Elf)
  232. {
  233. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false);
  234. }
  235. else if (skillRoot.SkillTab == SkillTab.Store)
  236. {
  237. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false);
  238. }
  239. else if (skillRoot.SkillTab == SkillTab.Magic)
  240. {
  241. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false);
  242. }
  243. else if (skillRoot.SkillTab == SkillTab.Garden)
  244. {
  245. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false);
  246. }
  247. else
  248. {
  249. throw new Exception();
  250. }
  251. skillRoot.SkillItem = tra;
  252. return tra;
  253. }
  254. public static Transform GetFlowerItemH()
  255. {
  256. Transform tra = Get("FlowerItemH", Folder.UI, false, Get("H_Grid"), false, ObjType.FlowerItem);
  257. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  258. if (objRoot == null)
  259. {
  260. objRoot = tra.AddComponent<ObjRoot>();
  261. }
  262. objRoot.ObjType = ObjType.FlowerItem;
  263. return objRoot.transform;
  264. }
  265. #region TraDic
  266. public static T Get<T>(string goName)
  267. {
  268. Transform tra;
  269. if (TraDic.TryGetValue(goName, out tra))
  270. {
  271. T t = tra.GetComponent<T>();
  272. if (t == null)
  273. {
  274. throw new Exception();
  275. }
  276. return t;
  277. }
  278. else
  279. {
  280. throw new Exception(goName);
  281. }
  282. }
  283. public static Transform Get(string goName)
  284. {
  285. Transform tra;
  286. if (TraDic.TryGetValue(goName, out tra))
  287. {
  288. return tra;
  289. }
  290. else
  291. {
  292. throw new Exception(goName);
  293. }
  294. }
  295. #endregion
  296. #region ObjPool
  297. public static void Save<T>(T t) where T : Component
  298. {
  299. Transform tra = t.transform;
  300. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  301. if (objRoot == null)
  302. {
  303. throw new Exception();
  304. }
  305. List<Transform> traList;
  306. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  307. {
  308. if (traList.Contains(tra))
  309. {
  310. throw new Exception();
  311. }
  312. tra.SetActive(false);
  313. traList.Add(tra);
  314. tra.transform.SetParent(Get("ObjPool"));
  315. }
  316. else
  317. {
  318. throw new Exception();
  319. }
  320. }
  321. public static void Save(GameObject go)
  322. {
  323. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  324. if (objRoot == null)
  325. {
  326. throw new Exception();
  327. }
  328. List<Transform> traList;
  329. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  330. {
  331. if (traList.Contains(go.transform))
  332. {
  333. throw new Exception();
  334. }
  335. go.SetActive(false);
  336. traList.Add(go.transform);
  337. go.transform.SetParent(Get("ObjPool"));
  338. }
  339. else
  340. {
  341. throw new Exception();
  342. }
  343. }
  344. public static void SaveUI<T>(T t) where T : Component
  345. {
  346. Transform tra = t.transform;
  347. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  348. if (objRoot == null)
  349. {
  350. throw new Exception();
  351. }
  352. List<Transform> traList;
  353. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  354. {
  355. if (traList.Contains(tra))
  356. {
  357. throw new Exception();
  358. }
  359. tra.SetActive(false);
  360. traList.Add(tra);
  361. tra.transform.SetParent(Get("J_ObjPool"));
  362. }
  363. else
  364. {
  365. throw new Exception();
  366. }
  367. }
  368. public static void SaveUI(GameObject go)
  369. {
  370. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  371. if (objRoot == null)
  372. {
  373. throw new Exception();
  374. }
  375. List<Transform> traList;
  376. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  377. {
  378. if (traList.Contains(go.transform))
  379. {
  380. throw new Exception();
  381. }
  382. go.SetActive(false);
  383. traList.Add(go.transform);
  384. go.transform.SetParent(Get("J_ObjPool"));
  385. }
  386. else
  387. {
  388. throw new Exception();
  389. }
  390. }
  391. public static Transform Get(ObjType objType)
  392. {
  393. List<Transform> traList;
  394. if (ObjectPool.TryGetValue(objType, out traList))
  395. {
  396. if (traList.Count > 0)
  397. {
  398. Transform tra = traList[0];
  399. tra.SetActive(true);
  400. traList.RemoveAt(0);
  401. return tra.transform;
  402. }
  403. }
  404. return null;
  405. }
  406. #endregion
  407. #region ShortCut
  408. public static void SetText(string goName)
  409. {
  410. Text text = Get<Text>(goName);
  411. text.text = Language.GetStr("UI", goName);
  412. }
  413. public static void SetText(string goName, string str)
  414. {
  415. Text text = Get<Text>(goName);
  416. text.text = str;
  417. }
  418. public static void SetSprite(string goName, Sprite sprite)
  419. {
  420. Image image = Get<Image>(goName);
  421. image.sprite = sprite;
  422. }
  423. public static void SetActive(string goName, bool active)
  424. {
  425. Get(goName).SetActive(active);
  426. }
  427. public static void SetButtonEvent(string goName, params UnityAction[] onClicks)
  428. {
  429. Button button = Get<Button>(goName);
  430. button.onClick = new Button.ButtonClickedEvent();
  431. for (int i = 0; i < onClicks.Length; i++)
  432. {
  433. button.onClick.AddListener(onClicks[i]);
  434. }
  435. }
  436. public static void AddButtonEvent(string goName, params UnityAction[] onClicks)
  437. {
  438. Button button = Get<Button>(goName);
  439. for (int i = 0; i < onClicks.Length; i++)
  440. {
  441. button.onClick.AddListener(onClicks[i]);
  442. }
  443. }
  444. #endregion
  445. }