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, Atlas, Atlas2, Effect, Audio, Scene, Config, Discard, } public enum ObjType { Null, Firework, LightwallUI, Player, Page, Flower, Garden, Tutorial, CloseItem, DressRoom, Canvas, GroupA, GroupB, GroupC, GroupD, GroupE, EventSystem, MainCamera, HudText, MailItem, InfoItem, SkillItem, SignItem, FlowerItem, AchieveItem, Music, Star, DropGold, DropDiamond, Bee, Beetle, Butterfly, Dragonfly, } public class AsyncRequest { public UnityAction Callback; public AssetBundleRequest Request; } public class ManaReso : Regist { #region 变量 public static bool AsyncLoadLock; public static bool AsyncInstantiateLock; public static Coroutine CoroLoad; public static Coroutine CoroInstantiate; public static ManaReso Instance; public static List AsyncList = new List(); public static List RequestList = new List(); public static List> InstantiateList = new List>(); public static Dictionary ObjDic = new Dictionary(); public static Dictionary TraDic = new Dictionary(); public static Dictionary ObjPoolDic = new Dictionary(); public static Dictionary> ObjectPool = new Dictionary>(); #endregion public override bool RegistImmed() { if (base.RegistImmed()) { return true; } Instance = this; Transform objPool = new GameObject("ObjPool").transform; objPool.parent = transform; objPool.SetActive(false); TraDic.Add(objPool.name, objPool); CoroLoad = StartCoroutine(IAsyncLoad()); CoroInstantiate = StartCoroutine(IAsyncInstancitate()); return false; } #region TraDic public static T Get(string goName, bool warn = true) { Transform tra; if (TraDic.TryGetValue(goName, out tra)) { T t = tra.GetComponent(); if (t == null) { throw new Exception(goName); } return t; } else { if (warn) { throw new Exception(goName); } else { return default(T); } } } public static T[] Gets(string goName, bool warn = true) { Transform tra; if (TraDic.TryGetValue(goName, out tra)) { T[] t = tra.GetComponentsInChildren(); if (t == null) { throw new Exception(goName); } return t; } else { if (warn) { throw new Exception(goName); } else { return default(T[]); } } } public static Transform Get(string goName, bool warn = true) { Transform tra; if (TraDic.TryGetValue(goName, out tra)) { return tra; } else { if (warn) { throw new Exception(goName); } else { return null; } } } #endregion #region ObjPool public static void Release() { Bundle.Discard.Unload(false); } public static void Save(T t, bool warn = false) where T : Component { Save(t.gameObject); } public static void Save(GameObject go, bool warn = false) { ObjType objType; if (!ObjPoolDic.TryGetValue(go, out objType)) { throw new Exception(); } List traList; if (ObjectPool.TryGetValue(objType, out traList)) { if (traList.Contains(go.transform)) { if (warn) { return; } else { throw new Exception(); } } go.SetActive(false); traList.Add(go.transform); go.transform.SetParent(Get("ObjPool")); } else { throw new Exception(); } } public static bool Contains(T t) where T : Component { return Contains(t.gameObject); } public static bool Contains(GameObject go) { foreach (var kv in ObjectPool) { if (kv.Value.Contains(go.transform)) { return true; } } return false; } 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 Text SetText(string goName) { Text text = Get(goName); text.text = Language.GetStr("UI", goName); return text; } public static Text SetText(string goName, string str) { Text text = Get(goName); text.text = str; return text; } public static Image SetSprite(string goName, Sprite sprite) { Image image = Get(goName); image.sprite = sprite; return image; } public static Transform SetActive(string goName, bool active) { Transform tra = Get(goName); tra.SetActive(active); return tra; } public static Button SetButtonEvent(string goName, UnityAction onClick) { Button button = Get