ManaReso.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. if (t == null)
  62. {
  63. t = Bundle.TempUI.LoadAsset<T>(goName);
  64. }
  65. }
  66. else if (folder == Folder.Skill)
  67. {
  68. t = Bundle.Skill.LoadAsset<T>(goName);
  69. if (t == null)
  70. {
  71. t = Bundle.TempSkill.LoadAsset<T>(goName);
  72. }
  73. }
  74. else if (folder == Folder.Config)
  75. {
  76. t = Bundle.Config.LoadAsset<T>(goName);
  77. if (t == null)
  78. {
  79. t = Bundle.TempConfig.LoadAsset<T>(goName);
  80. }
  81. }
  82. else if (folder == Folder.Object)
  83. {
  84. t = Bundle.Object.LoadAsset<T>(goName);
  85. if (t == null)
  86. {
  87. t = Bundle.TempObject.LoadAsset<T>(goName);
  88. }
  89. }
  90. else if (folder == Folder.Shader)
  91. {
  92. t = Bundle.Shader.LoadAsset<T>(goName);
  93. if (t == null)
  94. {
  95. t = Bundle.TempShader.LoadAsset<T>(goName);
  96. }
  97. }
  98. else if (folder == Folder.Garden)
  99. {
  100. t = Bundle.Garden.LoadAsset<T>(goName);
  101. if (t == null)
  102. {
  103. t = Bundle.TempGarden.LoadAsset<T>(goName);
  104. }
  105. }
  106. else if (folder == Folder.Character)
  107. {
  108. t = Bundle.Character.LoadAsset<T>(goName);
  109. if (t == null)
  110. {
  111. t = Bundle.TempCharacter.LoadAsset<T>(goName);
  112. }
  113. }
  114. else
  115. {
  116. throw new Exception();
  117. }
  118. if (t == null)
  119. {
  120. throw new Exception(goName);
  121. }
  122. ObjDic.Add(goName, t);
  123. if (objType != ObjType.Null)
  124. {
  125. ObjectPool.Add(objType, new List<Transform>());
  126. }
  127. return t;
  128. }
  129. }
  130. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null)
  131. {
  132. Transform tra = Get(objType);
  133. if (tra != null)
  134. {
  135. tra.SetParent(par);
  136. tra.position = pos;
  137. return tra;
  138. }
  139. else
  140. {
  141. GameObject go = Load<GameObject>(goName, folder, objType);
  142. go = Instantiate(go, pos, Quaternion.identity, par);
  143. go.name = go.name.Replace("(Clone)", "");
  144. if (compile)
  145. {
  146. Auxiliary.CompileDic(go.transform, TraDic);
  147. }
  148. return go.transform;
  149. }
  150. }
  151. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null)
  152. {
  153. Transform tra = Get(objType);
  154. if (tra == null)
  155. {
  156. GameObject go = Load<GameObject>(goName, folder, objType);
  157. go = Instantiate(go, par, worldSpace);
  158. go.name = go.name.Replace("(Clone)", "");
  159. if (compile)
  160. {
  161. Auxiliary.CompileDic(go.transform, TraDic);
  162. }
  163. return go.transform;
  164. }
  165. else
  166. {
  167. tra.SetParent(par);
  168. return tra;
  169. }
  170. }
  171. public static DropGold GetGold()
  172. {
  173. Vector3 pos = Vector3.Lerp(Get("LeftTraMini").position, Get("RightTraMini").position, Random.Range(0, 1f));
  174. pos.z = -2;
  175. Transform tra = Get("DropGold", Folder.UI, false, null, pos, ObjType.DropGold);
  176. DropGold dropGold = tra.GetComponent<DropGold>();
  177. if (dropGold == null)
  178. {
  179. dropGold = tra.AddComponent<DropGold>();
  180. }
  181. dropGold.ResetSta();
  182. return dropGold;
  183. }
  184. public static DropDiamond GetDiamond()
  185. {
  186. Vector3 pos = Vector3.Lerp(Get("LeftTraMini").position, Get("RightTraMini").position, Random.Range(0, 1f));
  187. pos.z = -2;
  188. Transform tra = Get("DropDiamond", Folder.UI, false, null, pos, ObjType.DropDiamond);
  189. DropDiamond dropDiamond = tra.GetComponent<DropDiamond>();
  190. if (dropDiamond == null)
  191. {
  192. dropDiamond = tra.AddComponent<DropDiamond>();
  193. }
  194. dropDiamond.ResetSta();
  195. return dropDiamond;
  196. }
  197. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  198. {
  199. Transform tra = Get("Flower", Folder.Garden, false, slot.transform, false, ObjType.Flower);
  200. Flower flower = tra.GetComponent<Flower>();
  201. if (flower == null)
  202. {
  203. flower = tra.AddComponent<Flower>();
  204. flower.ObjType = ObjType.Flower;
  205. flower.LocalPos = flower.transform.localPosition;
  206. }
  207. else
  208. {
  209. flower.GoldBk.SetActive(false);
  210. flower.transform.localPosition = flower.LocalPos;
  211. }
  212. flower.FlowerInfo = flowerInfo;
  213. flower.Slot = slot;
  214. flower.SetCollider(collider);
  215. return flower;
  216. }
  217. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  218. {
  219. Transform tra = Get("Flower", Folder.Garden, false, par, false, ObjType.Flower);
  220. Flower flower = tra.GetComponent<Flower>();
  221. if (flower == null)
  222. {
  223. flower = tra.AddComponent<Flower>();
  224. flower.ObjType = ObjType.Flower;
  225. flower.LocalPos = flower.transform.localPosition;
  226. }
  227. else
  228. {
  229. flower.GoldBk.SetActive(false);
  230. flower.transform.localPosition = flower.LocalPos;
  231. }
  232. flower.FlowerInfo = flowerInfo;
  233. flower.SetCollider(false);
  234. return flower;
  235. }
  236. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene)
  237. {
  238. Vector3 pos;
  239. if (scene)
  240. {
  241. pos = Camera.main.WorldToScreenPoint(posTra.position);
  242. }
  243. else
  244. {
  245. pos = posTra.position;
  246. }
  247. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText);
  248. HudText hudText = tra.GetComponent<HudText>();
  249. if (hudText == null)
  250. {
  251. hudText = tra.AddComponent<HudText>();
  252. hudText.ObjType = ObjType.HudText;
  253. }
  254. hudText.Show(str, color, size);
  255. return hudText;
  256. }
  257. public static Transform GetSkillItem(SkillRoot skillRoot)
  258. {
  259. Transform tra;
  260. if (skillRoot.SkillTab == SkillTab.Elf)
  261. {
  262. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false);
  263. }
  264. else if (skillRoot.SkillTab == SkillTab.Store)
  265. {
  266. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false);
  267. }
  268. else if (skillRoot.SkillTab == SkillTab.Magic)
  269. {
  270. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false);
  271. }
  272. else if (skillRoot.SkillTab == SkillTab.Garden)
  273. {
  274. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false);
  275. }
  276. else
  277. {
  278. throw new Exception();
  279. }
  280. skillRoot.SkillItem = tra;
  281. return tra;
  282. }
  283. public static Transform GetFlowerItemH()
  284. {
  285. Transform tra = Get("FlowerItemH", Folder.UI, false, Get("H_Grid"), false, ObjType.FlowerItem);
  286. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  287. if (objRoot == null)
  288. {
  289. objRoot = tra.AddComponent<ObjRoot>();
  290. }
  291. objRoot.ObjType = ObjType.FlowerItem;
  292. return objRoot.transform;
  293. }
  294. #region TraDic
  295. public static T Get<T>(string goName)
  296. {
  297. Transform tra;
  298. if (TraDic.TryGetValue(goName, out tra))
  299. {
  300. T t = tra.GetComponent<T>();
  301. if (t == null)
  302. {
  303. throw new Exception();
  304. }
  305. return t;
  306. }
  307. else
  308. {
  309. throw new Exception(goName);
  310. }
  311. }
  312. public static Transform Get(string goName)
  313. {
  314. Transform tra;
  315. if (TraDic.TryGetValue(goName, out tra))
  316. {
  317. return tra;
  318. }
  319. else
  320. {
  321. throw new Exception(goName);
  322. }
  323. }
  324. #endregion
  325. #region ObjPool
  326. public static void Save<T>(T t) where T : Component
  327. {
  328. Transform tra = t.transform;
  329. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  330. if (objRoot == null)
  331. {
  332. throw new Exception();
  333. }
  334. List<Transform> traList;
  335. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  336. {
  337. if (traList.Contains(tra))
  338. {
  339. throw new Exception();
  340. }
  341. tra.SetActive(false);
  342. traList.Add(tra);
  343. tra.transform.SetParent(Get("ObjPool"));
  344. }
  345. else
  346. {
  347. throw new Exception();
  348. }
  349. }
  350. public static void Save(GameObject go)
  351. {
  352. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  353. if (objRoot == null)
  354. {
  355. throw new Exception();
  356. }
  357. List<Transform> traList;
  358. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  359. {
  360. if (traList.Contains(go.transform))
  361. {
  362. throw new Exception();
  363. }
  364. go.SetActive(false);
  365. traList.Add(go.transform);
  366. go.transform.SetParent(Get("ObjPool"));
  367. }
  368. else
  369. {
  370. throw new Exception();
  371. }
  372. }
  373. public static void SaveUI<T>(T t) where T : Component
  374. {
  375. Transform tra = t.transform;
  376. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  377. if (objRoot == null)
  378. {
  379. throw new Exception();
  380. }
  381. List<Transform> traList;
  382. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  383. {
  384. if (traList.Contains(tra))
  385. {
  386. throw new Exception();
  387. }
  388. tra.SetActive(false);
  389. traList.Add(tra);
  390. tra.transform.SetParent(Get("J_ObjPool"));
  391. }
  392. else
  393. {
  394. throw new Exception();
  395. }
  396. }
  397. public static void SaveUI(GameObject go)
  398. {
  399. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  400. if (objRoot == null)
  401. {
  402. throw new Exception();
  403. }
  404. List<Transform> traList;
  405. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  406. {
  407. if (traList.Contains(go.transform))
  408. {
  409. throw new Exception();
  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. }