using UnityEditor; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System; using System.Collections; using System.Linq.Expressions; using System.Collections.Generic; using Object = UnityEngine.Object; public enum Folder { UI, Sprite, Object, } public enum ObjectPoolType { Null, HUD, } public class ManagerResource : MonoBehaviour { #region 变量 #region 引用变量 #region UI [NonSerialized] public Transform CanvasTra; //Log [NonSerialized] public Text LogLabel; [NonSerialized] public Image LogBk; [NonSerialized] public Image LogHandleBk; [NonSerialized] public ScrollRect LogScrollRect; //MainPanel [NonSerialized] public Text GoldTitle1; [NonSerialized] public Text GoldLabel1; [NonSerialized] public Text SettingLabel; [NonSerialized] public Text DiamondTitle1; [NonSerialized] public Text DiamondLabel1; [NonSerialized] public Text AchievementLabel; [NonSerialized] public Text OpenManageLabel; [NonSerialized] public Button SettingBtn; [NonSerialized] public Button AchievementBtn; [NonSerialized] public Button OpenManageBtn; [NonSerialized] public GameObject MainPanel; //GamePanel [NonSerialized] public Text ScoreTitle; [NonSerialized] public Text ScoreLabel; [NonSerialized] public Text RippingLabel; [NonSerialized] public Text WateringLabel; [NonSerialized] public Text FertilizingLabel; [NonSerialized] public Text GameBeginLabel; [NonSerialized] public Text GameStatusLabel; [NonSerialized] public Text RemainTimeLabel; [NonSerialized] public Image GamePanelBk; [NonSerialized] public Button RippingBtn; [NonSerialized] public Button WateringBtn; [NonSerialized] public Button FertilizingBtn; [NonSerialized] public Button GameBeginBtn; [NonSerialized] public GameObject OperatePanel; //ManagePanel [NonSerialized] public Text ElfTitle1; [NonSerialized] public Text ElfLabel1; [NonSerialized] public Text VisitTitle; [NonSerialized] public Text VisitLabel; [NonSerialized] public Text GoldTitle2; [NonSerialized] public Text GoldLabel2; [NonSerialized] public Text FlowerTitle1; [NonSerialized] public Text FlowerLabel1; [NonSerialized] public Text DiamondTitle2; [NonSerialized] public Text DiamondLabel2; [NonSerialized] public Image BarBk1; [NonSerialized] public Image BarBk2; [NonSerialized] public Image BarBk3; [NonSerialized] public Image ManagePanelBk; [NonSerialized] public GameObject ManagePanel; //ManagePanel [NonSerialized] public Text ElfLabel2; [NonSerialized] public Text ShopLabel; [NonSerialized] public Text InputLabel; [NonSerialized] public Text MagicLabel; [NonSerialized] public Text GardenLabel; [NonSerialized] public Text FlowerLabel2; [NonSerialized] public Text PlaceHolderLabel; [NonSerialized] public Text CloseManageLabel; [NonSerialized] public Image CommandBk; [NonSerialized] public Button ElfBtn2; [NonSerialized] public Button ShopBtn; [NonSerialized] public Button MagicBtn; [NonSerialized] public Button GardenBtn; [NonSerialized] public Button FlowerBtn2; [NonSerialized] public Button CloseManageBtn; [NonSerialized] public Text HintLabel; [NonSerialized] public Transform HudTra; #endregion [NonSerialized] public List FlowerList; #endregion private Dictionary PrefabDictionary; private Dictionary> ObjectPool; public static ManagerResource Ins; #endregion private void Awake() { Ins = this; FlowerList = new List(); ObjectPool = new Dictionary>(); PrefabDictionary = new Dictionary(); #region 构造场景 Get("MainCamera", Folder.Object); CreateUGUI(); Get("GardenBk", Folder.Object, transform); GameObject player = Get("Player", Folder.Object, transform); player.AddComponent(); Transform flowerTra1 = Get("FlowerTra1", Folder.Object, transform).transform; flowerTra1.gameObject.AddComponent(); Transform flowerTra2 = Get("FlowerTra2", Folder.Object, transform).transform; flowerTra2.gameObject.AddComponent(); Transform flowerTra3 = Get("FlowerTra3", Folder.Object, transform).transform; flowerTra3.gameObject.AddComponent(); #endregion } public void Save(GameObject go) { ObjectPoolRoot objectPoolRoot = go.GetComponent(); if (objectPoolRoot == null) { throw new Exception(); } List tempGoList; if (ObjectPool.TryGetValue(objectPoolRoot.ObjectPoolType, out tempGoList)) { go.SetActive(false); tempGoList.Add(go); } else { throw new Exception(); } } public GameObject Get(string name, Folder folder, ObjectPoolType objectPoolType = ObjectPoolType.Null) { GameObject tempGo = Load(name, folder, objectPoolType); return Instantiate(tempGo, tempGo.transform.position, tempGo.transform.rotation); } public GameObject Get(string name, Folder folder, Vector3 position, Quaternion rotation, Transform parent = null, ObjectPoolType objectPoolType = ObjectPoolType.Null) { GameObject tempGo = Load(name, folder, objectPoolType); return Instantiate(tempGo, position, rotation, parent); } public GameObject Get(string name, Folder folder, Transform parent, bool worldSpace = false, ObjectPoolType objectPoolType = ObjectPoolType.Null) { GameObject tempGo = Load(name, folder, objectPoolType); return Instantiate(tempGo, parent, worldSpace); } public T Load(string name, Folder folder, ObjectPoolType objectPoolType = ObjectPoolType.Null) where T : Object { GameObject tempGo; if (PrefabDictionary.TryGetValue(objectPoolType, out tempGo)) { return (T) Convert.ChangeType(tempGo, typeof(T)); } else { #region 加载Prefab T t; if (folder == Folder.UI) { t = Resources.Load(@"Prefab\UI\" + name); } else if (folder == Folder.Sprite) { t = Resources.Load(@"Sprite\" + name); } else if (folder == Folder.Object) { t = Resources.Load(@"Prefab\Object\" + name); } else { throw new Exception(); } if (objectPoolType != ObjectPoolType.Null) { ObjectPool.Add(objectPoolType, new List()); PrefabDictionary.Add(objectPoolType, (GameObject) Convert.ChangeType(t, typeof(GameObject))); } return t; #endregion } } public Text GetHUD(Color color, Vector3 position, bool permanent = false, string message = "", float speed = 0.5f, float stayTime = 0f) { HUD hud; GameObject tempGo; List tempGoList; if (ObjectPool.TryGetValue(ObjectPoolType.HUD, out tempGoList)) //从对象池中获得 { if (tempGoList.Count > 0) { tempGo = tempGoList[0]; tempGoList.RemoveAt(0); tempGo.SetActive(true); tempGo.transform.position = position; tempGo.transform.rotation = Quaternion.identity; hud = tempGo.GetComponent(); hud.Set(color, permanent, message, speed, stayTime); return tempGo.GetComponent(); } } #region Instantiate tempGo = Get("HUD", Folder.UI, position, Quaternion.identity, HudTra, ObjectPoolType.HUD); hud = tempGo.AddComponent(); hud.Set(color, permanent, message, speed, stayTime); return tempGo.GetComponent(); #endregion } private void CreateUGUI() { Language.Init(SystemLanguage.Chinese); CanvasTra = Get("Canvas", Folder.UI).transform; //实例化Canvas Get("EventSystem", Folder.UI); //实例化EventSystem #region 引用变量赋值 Hashtable hashtable = new Hashtable(); Transform[] transforms = CanvasTra.GetComponentsInChildren(true); for (int i = 0; i < transforms.Length; i++) { hashtable.Add(transforms[i].name, transforms[i]); } //Log LogLabel = ((Transform) hashtable["LogLabel"]).GetComponent(); LogBk = ((Transform) hashtable["LogScrollRectBk"]).GetComponent(); LogHandleBk = ((Transform) hashtable["LogHandleBk"]).GetComponent(); LogScrollRect = ((Transform) hashtable["LogScrollRectBk"]).GetComponent(); //MainPanel GoldTitle1 = ((Transform) hashtable["GoldTitle1"]).GetComponent(); GoldLabel1 = ((Transform) hashtable["GoldLabel1"]).GetComponent(); SettingLabel = ((Transform) hashtable["SettingLabel"]).GetComponent(); DiamondTitle1 = ((Transform) hashtable["DiamondTitle1"]).GetComponent(); DiamondLabel1 = ((Transform) hashtable["DiamondLabel1"]).GetComponent(); AchievementLabel = ((Transform) hashtable["AchievementLabel"]).GetComponent(); OpenManageLabel = ((Transform) hashtable["OpenManageLabel"]).GetComponent(); SettingBtn = ((Transform) hashtable["SettingBtn"]).GetComponent