using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System; using System.Collections; using System.Linq.Expressions; using System.ComponentModel; using System.Collections.Generic; using Object = UnityEngine.Object; using Random = UnityEngine.Random; using Component = UnityEngine.Component; public enum Folder { UI, Scene, Config, Object, Shader, } public enum ObjType { Null, Flower, HudText, FlowerItem, AchieveItem, DropGold, DropDiamond, BeeRed, BeeBlue, BeeWhite, BeePurple, BeeYellow, ButterflyRed, ButterflyBlue, ButterflyWhite, ButterflyPurple, ButterflyYellow, } public class ManaReso : Regist { #region 变量 public static Dictionary ObjDic = new Dictionary(); public static Dictionary TraDic = new Dictionary(); public static Dictionary> ObjectPool = new Dictionary>(); #endregion public override void Instantiate() { Transform objPool = new GameObject("ObjPool").transform; objPool.parent = transform; objPool.SetActive(false); TraDic.Add(objPool.name, objPool); } #region TraDic public static T Get(string goName) { Transform tra; if (TraDic.TryGetValue(goName, out tra)) { T t = tra.GetComponent(); if (t == null) { throw new Exception(); } return t; } else { throw new Exception(goName); } } public static Transform Get(string goName) { Transform tra; if (TraDic.TryGetValue(goName, out tra)) { return tra; } else { throw new Exception(goName); } } #endregion #region ObjPool public static void Save(T t, bool warn = true) where T : Component { Transform tra = t.transform; ObjRoot objRoot = tra.GetComponent(); if (objRoot == null) { throw new Exception(); } List traList; if (ObjectPool.TryGetValue(objRoot.ObjType, out traList)) { if (traList.Contains(tra)) { if (warn) { throw new Exception(); } else { return; } } tra.SetActive(false); traList.Add(tra); tra.transform.SetParent(Get("ObjPool")); } else { throw new Exception(); } } public static void Save(GameObject go, bool warn = true) { ObjRoot objRoot = go.GetComponent(); if (objRoot == null) { throw new Exception(); } List traList; if (ObjectPool.TryGetValue(objRoot.ObjType, out traList)) { if (traList.Contains(go.transform)) { if (warn) { throw new Exception(); } else { return; } } go.SetActive(false); traList.Add(go.transform); go.transform.SetParent(Get("ObjPool")); } else { throw new Exception(); } } public static void SaveUI(T t, bool warn = true) where T : Component { Transform tra = t.transform; ObjRoot objRoot = tra.GetComponent(); if (objRoot == null) { throw new Exception(); } List traList; if (ObjectPool.TryGetValue(objRoot.ObjType, out traList)) { if (traList.Contains(tra)) { if (warn) { throw new Exception(); } else { return; } } tra.SetActive(false); traList.Add(tra); tra.transform.SetParent(Get("J_ObjPool")); } else { throw new Exception(); } } public static void SaveUI(GameObject go, bool warn = true) { ObjRoot objRoot = go.GetComponent(); if (objRoot == null) { throw new Exception(); } List traList; if (ObjectPool.TryGetValue(objRoot.ObjType, out traList)) { if (traList.Contains(go.transform)) { if (warn) { throw new Exception(); } else { return; } } go.SetActive(false); traList.Add(go.transform); go.transform.SetParent(Get("J_ObjPool")); } else { throw new Exception(); } } public static Transform Get(ObjType objType) { List traList; if (ObjectPool.TryGetValue(objType, out traList)) { if (traList.Count > 0) { Transform tra = traList[0]; tra.SetActive(true); traList.RemoveAt(0); return tra.transform; } } return null; } #endregion #region ShortCut public static void SetText(string goName) { Text text = Get(goName); text.text = Language.GetStr("UI", goName); } public static void SetText(string goName, string str) { Text text = Get(goName); text.text = str; } public static void SetSprite(string goName, Sprite sprite) { Image image = Get(goName); image.sprite = sprite; } public static void SetActive(string goName, bool active) { Get(goName).SetActive(active); } public static void SetButtonEvent(string goName, UnityAction onClick) { Button button = Get