LoadUIModelHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class LoadUIModelHelper : MonoSingleton<LoadUIModelHelper>
  8. {
  9. /// <summary>
  10. /// 单键
  11. /// </summary>
  12. private static LoadUIModelHelper mInst = null;
  13. /// <summary>
  14. /// WindowsObjDict 列表
  15. /// </summary>
  16. private Dictionary<string, GameObject> WindowsObjDict = new Dictionary<string, GameObject>();
  17. /// <summary>
  18. /// 获取单键实例
  19. /// </summary>
  20. /// <returns>单键实例</returns>
  21. public static LoadUIModelHelper Instance()
  22. {
  23. if (null == mInst)
  24. {
  25. GameObject obj = new GameObject("LoadUIModelHelper");
  26. mInst = obj.AddComponent<LoadUIModelHelper>();
  27. DontDestroyOnLoad(obj);
  28. }
  29. return mInst;
  30. }
  31. /// <summary>
  32. /// 初始化
  33. /// </summary>
  34. private void Awake()
  35. {
  36. mInst = this;
  37. // 切换关卡时不删除
  38. DontDestroyOnLoad(this);
  39. }
  40. /// <summary>
  41. /// 单键初始化
  42. /// </summary>
  43. protected override void OnAwake()
  44. {
  45. base.OnAwake();
  46. }
  47. /// <summary>
  48. /// 单键销毁
  49. /// </summary>
  50. protected override void DoOnDestroy()
  51. {
  52. base.DoOnDestroy();
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. /// <param name="name">模型的名称</param>
  58. /// <param name="ac">模型加载成功之后的回调</param>
  59. public void LoadFighterModel(string name, Action<GameObject> ac)
  60. {
  61. //---------------
  62. if (WindowsObjDict.ContainsKey(name))
  63. {
  64. GameObject model = WindowsObjDict[name];
  65. model.transform.localRotation = Quaternion.identity;
  66. model.transform.Rotate(Vector3.up * 180);
  67. model.SetActive(true);
  68. if (ac != null)
  69. ac(model);
  70. }
  71. else
  72. {
  73. ResourceHelper.Instance.LoadAssetBundle(name, (AssetBundle ab) =>
  74. {
  75. GameObject obj = (GameObject)ab.LoadAsset(name);
  76. if (obj != null)
  77. {
  78. GameObject model = (GameObject)Instantiate(obj);
  79. model.transform.SetParent(this.transform);
  80. model.transform.localScale = Vector3.one;
  81. model.transform.localPosition = new Vector3(0, -2, 5);
  82. model.transform.localRotation = Quaternion.identity;
  83. model.transform.Rotate(Vector3.up * 180);
  84. Transform[] all = model.GetComponentsInChildren<Transform>();
  85. foreach (Transform rc in all)
  86. {
  87. rc.gameObject.layer = LayerMask.NameToLayer("UIModel"); //指定Layer
  88. }
  89. Transform fightFlag = FindTransform(model.transform, "IsFighting");
  90. if (fightFlag != null)
  91. {
  92. fightFlag.gameObject.SetActive(false);
  93. }
  94. WindowsObjDict.Add(name, model);
  95. if (ac != null)
  96. ac(model);
  97. }
  98. else
  99. {
  100. LogHelper.LogWarning("读取召唤兽模型失败 " + name);
  101. }
  102. });
  103. }
  104. }
  105. /// <summary>
  106. /// 查找Transform
  107. /// </summary>
  108. /// <param name="tran">父级</param>
  109. /// <param name="name">名称</param>
  110. /// <returns></returns>
  111. private Transform FindTransform(Transform tran, string name)
  112. {
  113. Transform[] trans = tran.GetComponentsInChildren<Transform>();
  114. for (int i = 0; i < trans.Length; i++)
  115. {
  116. if (trans[i].name == name)
  117. {
  118. return trans[i];
  119. }
  120. }
  121. return null;
  122. }
  123. /// <summary>
  124. /// 关闭所有panel
  125. /// </summary>
  126. public void CloseAllHeroModel()
  127. {
  128. List<string> KeyList = new List<string>(WindowsObjDict.Keys);
  129. for (int i = 0; i < KeyList.Count; i++)
  130. {
  131. CloseSomeOneHeroModel(KeyList[i]);
  132. }
  133. }
  134. /// <summary>
  135. /// 关闭指定panel
  136. /// </summary>
  137. /// <param name="name">panel名称</param>
  138. public void CloseSomeOneHeroModel(string name)
  139. {
  140. if (WindowsObjDict.ContainsKey(name))
  141. {
  142. WindowsObjDict[name].SetActive(false);
  143. }
  144. }
  145. /// <summary>
  146. /// 卸载界面资源
  147. /// </summary>
  148. /// <param name="name">界面名称</param>
  149. public void UnloadSomeOneHeroAssets(string name)
  150. {
  151. if (WindowsObjDict.ContainsKey(name))
  152. {
  153. Destroy(WindowsObjDict[name]);
  154. WindowsObjDict.Remove(name);
  155. }
  156. }
  157. /// <summary>
  158. /// 卸载所有加载的界面资源
  159. /// </summary>
  160. public void UnloadAllHeroAssets()
  161. {
  162. List<string> KeyList = new List<string>(WindowsObjDict.Keys);
  163. for (int i = 0; i < KeyList.Count; i++)
  164. {
  165. UnloadSomeOneHeroAssets(KeyList[i]);
  166. }
  167. }
  168. /// <summary>
  169. /// 传递参数为鼠标在屏幕拖拽的距离
  170. /// 此函数备用~ 旋转人物模型
  171. /// </summary>
  172. /// <param name="direction"></param>
  173. public void OnRotation(string modelName, Vector2 direction, int operateWidth)
  174. {
  175. if (WindowsObjDict.ContainsKey(modelName))
  176. {
  177. GameObject model = WindowsObjDict[modelName];
  178. float speed = 360.0f * (Mathf.Abs(direction.x) / operateWidth) * Mathf.CeilToInt(direction.normalized.x) * -1;
  179. RectTransform rc = model.gameObject.GetComponent<RectTransform>();
  180. model.transform.Rotate(Vector3.up * speed);
  181. }
  182. }
  183. }