123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- 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<string, Object> ObjDic = new Dictionary<string, Object>();
- public static Dictionary<string, Transform> TraDic = new Dictionary<string, Transform>();
- public static Dictionary<ObjType, List<Transform>> ObjectPool = new Dictionary<ObjType, List<Transform>>();
- #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<T>(string goName)
- {
- Transform tra;
- if (TraDic.TryGetValue(goName, out tra))
- {
- T t = tra.GetComponent<T>();
- 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 t, bool warn = true) where T : Component
- {
- Transform tra = t.transform;
- ObjRoot objRoot = tra.GetComponent<ObjRoot>();
- if (objRoot == null)
- {
- throw new Exception();
- }
- List<Transform> 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<ObjRoot>();
- if (objRoot == null)
- {
- throw new Exception();
- }
- List<Transform> 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 t, bool warn = true) where T : Component
- {
- Transform tra = t.transform;
- ObjRoot objRoot = tra.GetComponent<ObjRoot>();
- if (objRoot == null)
- {
- throw new Exception();
- }
- List<Transform> 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<ObjRoot>();
- if (objRoot == null)
- {
- throw new Exception();
- }
- List<Transform> 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<Transform> 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<Text>(goName);
- text.text = Language.GetStr("UI", goName);
- }
- public static void SetText(string goName, string str)
- {
- Text text = Get<Text>(goName);
- text.text = str;
- }
- public static void SetSprite(string goName, Sprite sprite)
- {
- Image image = Get<Image>(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<Button>(goName);
- button.onClick = new Button.ButtonClickedEvent();
- button.onClick.AddListener(onClick);
- }
- public static void AddButtonEvent(string goName, UnityAction onClick)
- {
- Button button = Get<Button>(goName);
- button.onClick.AddListener(onClick);
- }
- public static void PushButtonEvent(string goName, UnityAction onClick)
- {
- Button button = Get<Button>(goName);
- Button.ButtonClickedEvent click = button.onClick;
- button.onClick = new Button.ButtonClickedEvent();
- button.onClick.AddListener(onClick);
- button.onClick.AddListener(click.Invoke);
- }
- public static void AddButtonEventOnetime(string goName, UnityAction onClick)
- {
- Button button = Get<Button>(goName);
- onClick += () =>
- {
- button.onClick.RemoveListener(onClick);
- };
- button.onClick.AddListener(onClick);
- }
- public static void PushButtonEventOnetime(string goName, UnityAction onClick)
- {
- Button button = Get<Button>(goName);
- onClick += () =>
- {
- button.onClick.RemoveListener(onClick);
- };
- Button.ButtonClickedEvent click = button.onClick;
- button.onClick = new Button.ButtonClickedEvent();
- button.onClick.AddListener(onClick);
- button.onClick.AddListener(click.Invoke);
- }
- #endregion
- public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
- {
- Object obj;
- if (ObjDic.TryGetValue(goName, out obj))
- {
- return (T)obj;
- }
- else
- {
- T t;
- if (folder == Folder.UI)
- {
- t = Bundle.UI.LoadAsset<T>(goName);
- }
- else if (folder == Folder.Config)
- {
- t = Bundle.Config.LoadAsset<T>(goName);
- }
- else if (folder == Folder.Object)
- {
- t = Bundle.Object.LoadAsset<T>(goName);
- }
- else if (folder == Folder.Shader)
- {
- t = Bundle.Shader.LoadAsset<T>(goName);
- }
- else if (folder == Folder.Scene)
- {
- t = Bundle.Scene.LoadAsset<T>(goName);
- }
- else
- {
- throw new Exception();
- }
- if (t == null)
- {
- throw new Exception(goName);
- }
- ObjDic.Add(goName, t);
- if (objType != ObjType.Null)
- {
- ObjectPool.Add(objType, new List<Transform>());
- }
- return t;
- }
- }
- public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null)
- {
- Transform tra = Get(objType);
- if (tra != null)
- {
- tra.SetParent(par);
- tra.position = pos;
- return tra;
- }
- else
- {
- GameObject go = Load<GameObject>(goName, folder, objType);
- go = Instantiate(go, pos, Quaternion.identity, par);
- go.name = go.name.Replace("(Clone)", "");
- if (compile)
- {
- Auxiliary.CompileDic(go.transform, TraDic);
- }
- return go.transform;
- }
- }
- public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null)
- {
- Transform tra = Get(objType);
- if (tra == null)
- {
- GameObject go = Load<GameObject>(goName, folder, objType);
- go = Instantiate(go, par, worldSpace);
- go.name = go.name.Replace("(Clone)", "");
- if (compile)
- {
- Auxiliary.CompileDic(go.transform, TraDic);
- }
- return go.transform;
- }
- else
- {
- tra.SetParent(par);
- return tra;
- }
- }
- public static DropGold GetGold()
- {
- Vector3 pos = Vector3.Lerp(Get("LeftPosTraMini").position, Get("RightPosTraMini").position, Random.Range(0, 1f));
- pos.z = -2;
- Transform tra = Get("DropGold", Folder.UI, false, null, pos, ObjType.DropGold);
- DropGold dropGold = tra.GetComponent<DropGold>();
- if (dropGold == null)
- {
- dropGold = tra.AddScript<DropGold>();
- }
- dropGold.ResetStatus();
- return dropGold;
- }
- public static DropDiamond GetDiamond()
- {
- Vector3 pos = Vector3.Lerp(Get("LeftPosTraMini").position, Get("RightPosTraMini").position, Random.Range(0, 1f));
- pos.z = -2;
- Transform tra = Get("DropDiamond", Folder.UI, false, null, pos, ObjType.DropDiamond);
- DropDiamond dropDiamond = tra.GetComponent<DropDiamond>();
- if (dropDiamond == null)
- {
- dropDiamond = tra.AddScript<DropDiamond>();
- }
- dropDiamond.ResetStatus();
- return dropDiamond;
- }
- public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
- {
- Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower);
- Flower flower = tra.GetComponent<Flower>();
- if (flower == null)
- {
- flower = tra.AddScript<Flower>();
- flower.ObjType = ObjType.Flower;
- flower.LocalPos = flower.transform.localPosition;
- }
- else
- {
- flower.GoldBk.SetActive(false);
- flower.transform.localPosition = flower.LocalPos;
- }
- flower.FlowerInfo = flowerInfo;
- flower.Slot = slot;
- flower.SetCollider(collider);
- return flower;
- }
- public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
- {
- Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower);
- Flower flower = tra.GetComponent<Flower>();
- if (flower == null)
- {
- flower = tra.AddScript<Flower>();
- flower.ObjType = ObjType.Flower;
- flower.LocalPos = flower.transform.localPosition;
- }
- else
- {
- flower.GoldBk.SetActive(false);
- flower.transform.localPosition = flower.LocalPos;
- }
- flower.FlowerInfo = flowerInfo;
- flower.SetCollider(false);
- return flower;
- }
- public static Transform GetAchieveItem()
- {
- Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
- ObjRoot objRoot = tra.GetComponent<ObjRoot>();
- if (objRoot == null)
- {
- tra.AddComponent<ObjRoot>().ObjType = ObjType.AchieveItem;
- }
- return tra;
- }
- public static Transform GetSkillItem(SkillRoot skillRoot)
- {
- Transform tra;
- if (skillRoot.SkillTab == SkillTab.Elf)
- {
- tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false);
- }
- else if (skillRoot.SkillTab == SkillTab.Store)
- {
- tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false);
- }
- else if (skillRoot.SkillTab == SkillTab.Magic)
- {
- tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false);
- }
- else if (skillRoot.SkillTab == SkillTab.Garden)
- {
- tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false);
- }
- else
- {
- throw new Exception();
- }
- skillRoot.SkillItem = tra;
- return tra;
- }
- public static Transform GetElf(Flower flower, ObjType obj)
- {
- Transform tra;
- Bounds bounds = flower.FlowerIcon.bounds;
- Vector3 pos = flower.FlowerIcon.transform.position;
- Transform par = flower.transform;
- pos.z -= Random.Range(0.001f, 0.1f);
- tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
- ObjRoot objRoot = tra.GetComponent<ObjRoot>();
- if (objRoot == null)
- {
- tra.AddComponent<ObjRoot>().ObjType = obj;
- tra.GetChild(0).AddScript<Elf>();
- }
- if (Random.Range(0f, 1f) <= 0.5f)
- {
- tra.SetEY(180);
- }
- else
- {
- tra.SetEY(0);
- }
- float offsetX = Mathf.Lerp(-0.75f*bounds.extents.x, 0.75f*bounds.extents.x, Random.Range(0f, 1f));
- float offsetY = Mathf.Lerp(0, 0.75f*bounds.extents.y, Random.Range(0f, 1f));
- tra.position += new Vector3(offsetX, offsetY, 0);
- Elf elf = tra.GetChild(0).GetComponent<Elf>();
-
- elf.Flower = flower;
- elf.Animator.SetTrigger("Play");
- return tra;
- }
- public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene)
- {
- Vector3 pos;
- if (scene)
- {
- pos = Camera.main.WorldToScreenPoint(posTra.position);
- }
- else
- {
- pos = posTra.position;
- }
- Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText);
- HudText hudText = tra.GetComponent<HudText>();
- if (hudText == null)
- {
- hudText = tra.AddComponent<HudText>();
- hudText.ObjType = ObjType.HudText;
- }
- hudText.Show(str, color, size);
- return hudText;
- }
- }
|