123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.UI;
- public class LoadUIModelHelper : MonoSingleton<LoadUIModelHelper>
- {
- /// <summary>
- /// 单键
- /// </summary>
- private static LoadUIModelHelper mInst = null;
- /// <summary>
- /// WindowsObjDict 列表
- /// </summary>
- private Dictionary<string, GameObject> WindowsObjDict = new Dictionary<string, GameObject>();
- /// <summary>
- /// 获取单键实例
- /// </summary>
- /// <returns>单键实例</returns>
- public static LoadUIModelHelper Instance()
- {
- if (null == mInst)
- {
- GameObject obj = new GameObject("LoadUIModelHelper");
- mInst = obj.AddComponent<LoadUIModelHelper>();
- DontDestroyOnLoad(obj);
- }
- return mInst;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- private void Awake()
- {
- mInst = this;
- // 切换关卡时不删除
- DontDestroyOnLoad(this);
- }
- /// <summary>
- /// 单键初始化
- /// </summary>
- protected override void OnAwake()
- {
- base.OnAwake();
- }
- /// <summary>
- /// 单键销毁
- /// </summary>
- protected override void DoOnDestroy()
- {
- base.DoOnDestroy();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="name">模型的名称</param>
- /// <param name="ac">模型加载成功之后的回调</param>
- public void LoadFighterModel(string name, Action<GameObject> 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<Transform>();
- 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);
- }
- });
- }
- }
- /// <summary>
- /// 查找Transform
- /// </summary>
- /// <param name="tran">父级</param>
- /// <param name="name">名称</param>
- /// <returns></returns>
- private Transform FindTransform(Transform tran, string name)
- {
- Transform[] trans = tran.GetComponentsInChildren<Transform>();
- for (int i = 0; i < trans.Length; i++)
- {
- if (trans[i].name == name)
- {
- return trans[i];
- }
- }
- return null;
- }
- /// <summary>
- /// 关闭所有panel
- /// </summary>
- public void CloseAllHeroModel()
- {
- List<string> KeyList = new List<string>(WindowsObjDict.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- CloseSomeOneHeroModel(KeyList[i]);
- }
- }
- /// <summary>
- /// 关闭指定panel
- /// </summary>
- /// <param name="name">panel名称</param>
- public void CloseSomeOneHeroModel(string name)
- {
- if (WindowsObjDict.ContainsKey(name))
- {
- WindowsObjDict[name].SetActive(false);
- }
- }
- /// <summary>
- /// 卸载界面资源
- /// </summary>
- /// <param name="name">界面名称</param>
- public void UnloadSomeOneHeroAssets(string name)
- {
- if (WindowsObjDict.ContainsKey(name))
- {
- Destroy(WindowsObjDict[name]);
- WindowsObjDict.Remove(name);
- }
- }
- /// <summary>
- /// 卸载所有加载的界面资源
- /// </summary>
- public void UnloadAllHeroAssets()
- {
- List<string> KeyList = new List<string>(WindowsObjDict.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- UnloadSomeOneHeroAssets(KeyList[i]);
- }
- }
- /// <summary>
- /// 传递参数为鼠标在屏幕拖拽的距离
- /// 此函数备用~ 旋转人物模型
- /// </summary>
- /// <param name="direction"></param>
- 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<RectTransform>();
- model.transform.Rotate(Vector3.up * speed);
- }
- }
- }
|