using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; using UnityEngine.UI; public class LoadUIModelHelper : MonoSingleton { /// /// 单键 /// private static LoadUIModelHelper mInst = null; /// /// WindowsObjDict 列表 /// private Dictionary WindowsObjDict = new Dictionary(); /// /// 获取单键实例 /// /// 单键实例 public static LoadUIModelHelper Instance() { if (null == mInst) { GameObject obj = new GameObject("LoadUIModelHelper"); mInst = obj.AddComponent(); DontDestroyOnLoad(obj); } return mInst; } /// /// 初始化 /// private void Awake() { mInst = this; // 切换关卡时不删除 DontDestroyOnLoad(this); } /// /// 单键初始化 /// protected override void OnAwake() { base.OnAwake(); } /// /// 单键销毁 /// protected override void DoOnDestroy() { base.DoOnDestroy(); } /// /// /// /// 模型的名称 /// 模型加载成功之后的回调 public void LoadFighterModel(string name, Action ac) { //--------------- if (WindowsObjDict.ContainsKey(name)) { GameObject model = WindowsObjDict[name]; model.transform.localRotation = Quaternion.identity; model.transform.Rotate(Vector3.up * 180); model.SetActive(true); if (ac != null) ac(model); } else { ResourceHelper.Instance.LoadAssetBundle(name, (AssetBundle ab) => { GameObject obj = (GameObject)ab.LoadAsset(name); if (obj != null) { GameObject model = (GameObject)Instantiate(obj); model.transform.SetParent(this.transform); model.transform.localScale = Vector3.one; model.transform.localPosition = new Vector3(0, -2, 5); model.transform.localRotation = Quaternion.identity; model.transform.Rotate(Vector3.up * 180); Transform[] all = model.GetComponentsInChildren(); foreach (Transform rc in all) { rc.gameObject.layer = LayerMask.NameToLayer("UIModel"); //指定Layer } Transform fightFlag = FindTransform(model.transform, "IsFighting"); if (fightFlag != null) { fightFlag.gameObject.SetActive(false); } WindowsObjDict.Add(name, model); if (ac != null) ac(model); } else { LogHelper.LogWarning("读取召唤兽模型失败 " + name); } }); } } /// /// 查找Transform /// /// 父级 /// 名称 /// private Transform FindTransform(Transform tran, string name) { Transform[] trans = tran.GetComponentsInChildren(); for (int i = 0; i < trans.Length; i++) { if (trans[i].name == name) { return trans[i]; } } return null; } /// /// 关闭所有panel /// public void CloseAllHeroModel() { List KeyList = new List(WindowsObjDict.Keys); for (int i = 0; i < KeyList.Count; i++) { CloseSomeOneHeroModel(KeyList[i]); } } /// /// 关闭指定panel /// /// panel名称 public void CloseSomeOneHeroModel(string name) { if (WindowsObjDict.ContainsKey(name)) { WindowsObjDict[name].SetActive(false); } } /// /// 卸载界面资源 /// /// 界面名称 public void UnloadSomeOneHeroAssets(string name) { if (WindowsObjDict.ContainsKey(name)) { Destroy(WindowsObjDict[name]); WindowsObjDict.Remove(name); } } /// /// 卸载所有加载的界面资源 /// public void UnloadAllHeroAssets() { List KeyList = new List(WindowsObjDict.Keys); for (int i = 0; i < KeyList.Count; i++) { UnloadSomeOneHeroAssets(KeyList[i]); } } /// /// 传递参数为鼠标在屏幕拖拽的距离 /// 此函数备用~ 旋转人物模型 /// /// public void OnRotation(string modelName, Vector2 direction, int operateWidth) { if (WindowsObjDict.ContainsKey(modelName)) { GameObject model = WindowsObjDict[modelName]; float speed = 360.0f * (Mathf.Abs(direction.x) / operateWidth) * Mathf.CeilToInt(direction.normalized.x) * -1; RectTransform rc = model.gameObject.GetComponent(); model.transform.Rotate(Vector3.up * speed); } } }