ManaReso.cs 14 KB

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