StaticImportant.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using AIPagedLod;
  3. using LitJson;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using UnityEngine.UI;
  9. using UnityEngine.Events;
  10. using Unity.VisualScripting;
  11. using DG.Tweening;
  12. using UnityAsync;
  13. using System.Threading.Tasks;
  14. using UnityEngine.UIElements;
  15. public class StaticImportant : MonoBehaviour
  16. {
  17. public Vector3 foucusPos;
  18. public float targetDistance;
  19. public float rotateXAngle;
  20. public float rotateYAngle;
  21. public bool findChangeObjs = false;
  22. public string[] FarObjName;
  23. public string[] NearObjName;
  24. public GameObject[] FarShowObj;
  25. public GameObject[] nearShowObj;
  26. public float showDistance = 100;
  27. private bool showNear = false;
  28. public bool waitCreatModels = false;
  29. public string model_ab_Name;
  30. bool onShow = true;
  31. public ModelAniTool _ModelAni;
  32. private List<GameObject> models = new List<GameObject>();
  33. private void Awake()
  34. {
  35. foucusPos = this.transform.position;
  36. if (!waitCreatModels)
  37. {
  38. for (int i = 0; i < this.transform.childCount; i++)
  39. {
  40. models.Add(this.transform.GetChild(i).gameObject);
  41. }
  42. if (findChangeObjs)
  43. {
  44. FarShowObj = new GameObject[FarObjName.Length];
  45. nearShowObj = new GameObject[NearObjName.Length];
  46. for (int i = 0; i < FarObjName.Length; i++)
  47. {
  48. FarShowObj[i] = this.transform.Find(FarObjName[i]).gameObject;
  49. }
  50. for (int i = 0; i < nearShowObj.Length; i++)
  51. {
  52. nearShowObj[i] = this.transform.Find(NearObjName[i]).gameObject;
  53. }
  54. }
  55. }
  56. }
  57. private void Start()
  58. {
  59. if (waitCreatModels)
  60. {
  61. StartCoroutine(CreatModels());
  62. }
  63. }
  64. public void Show()
  65. {
  66. if (onShow) return;
  67. onShow = true;
  68. for (int i = 0; i < models.Count; i++)
  69. {
  70. models[i].SetActive(true);
  71. }
  72. CheckChangeModels();
  73. }
  74. public void Hide()
  75. {
  76. if (!onShow) return;
  77. onShow = false;
  78. for (int i = 0; i < models.Count; i++)
  79. {
  80. models[i].SetActive(false);
  81. }
  82. }
  83. public void ForceSacnMode()
  84. {
  85. gameObject.SetActive(true);
  86. SetCameraToCenter();
  87. }
  88. private void Update()
  89. {
  90. if (waitCreatModels)
  91. {
  92. return;
  93. }
  94. if (CameraManager.instance.mainCamera.transform.position.y < showDistance)
  95. {
  96. if (showNear != true)
  97. {
  98. showNear = true;
  99. for (int i = 0; i < FarShowObj.Length; i++)
  100. {
  101. FarShowObj[i].SetActive(false);
  102. }
  103. for (int i = 0; i < nearShowObj.Length; i++)
  104. {
  105. nearShowObj[i].SetActive(true);
  106. }
  107. }
  108. }
  109. else
  110. {
  111. if (showNear != false)
  112. {
  113. showNear = false;
  114. for (int i = 0; i < FarShowObj.Length; i++)
  115. {
  116. FarShowObj[i].SetActive(true);
  117. }
  118. for (int i = 0; i < nearShowObj.Length; i++)
  119. {
  120. nearShowObj[i].SetActive(false);
  121. }
  122. }
  123. }
  124. }
  125. private void CheckChangeModels()
  126. {
  127. if (CameraManager.instance.mainCamera.transform.position.y < showDistance)
  128. {
  129. showNear = true;
  130. for (int i = 0; i < FarShowObj.Length; i++)
  131. {
  132. FarShowObj[i].SetActive(false);
  133. }
  134. for (int i = 0; i < nearShowObj.Length; i++)
  135. {
  136. nearShowObj[i].SetActive(true);
  137. }
  138. }
  139. else
  140. {
  141. showNear = false;
  142. for (int i = 0; i < FarShowObj.Length; i++)
  143. {
  144. FarShowObj[i].SetActive(true);
  145. }
  146. for (int i = 0; i < nearShowObj.Length; i++)
  147. {
  148. nearShowObj[i].SetActive(false);
  149. }
  150. }
  151. }
  152. public void SetCameraToCenter()
  153. {
  154. CameraBird birdCamera = CameraManager.instance.mainCamera.GetComponent<CameraBird>();
  155. if (birdCamera != null)
  156. {
  157. MeshRenderer fader = birdCamera.transform.GetChild(0).GetComponent<MeshRenderer>();
  158. fader.gameObject.SetActive(true);
  159. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  160. {
  161. fader.material.DOColor(Color.clear, 1.5f).onComplete = () => { fader.gameObject.SetActive(false); };
  162. };
  163. birdCamera.target.localPosition = foucusPos;
  164. Debug.Log(birdCamera.target.position);
  165. birdCamera.currentDistance = targetDistance;
  166. birdCamera.rotateXAngle = rotateXAngle;
  167. birdCamera.rotateYAngle = rotateYAngle;
  168. birdCamera.Blink();
  169. }
  170. ModelCameraCtrl._Instance.SetCameraPos(birdCamera.target, targetDistance, new Vector2(rotateXAngle, rotateYAngle));
  171. }
  172. public void SetCameraToCenter(Vector3 centerPos, float distance, Vector2 rota)
  173. {
  174. targetDistance = distance;
  175. rotateXAngle = rota.x;
  176. rotateYAngle = rota.y;
  177. CameraBird birdCamera = CameraManager.instance.mainCamera.GetComponent<CameraBird>();
  178. if (birdCamera != null)
  179. {
  180. MeshRenderer fader = birdCamera.transform.GetChild(0).GetComponent<MeshRenderer>();
  181. fader.gameObject.SetActive(true);
  182. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  183. {
  184. fader.material.DOColor(Color.clear, 1.5f).onComplete = () => { fader.gameObject.SetActive(false); };
  185. };
  186. birdCamera.target.localPosition = centerPos;
  187. Debug.Log(birdCamera.target.position);
  188. birdCamera.currentDistance = targetDistance;
  189. birdCamera.rotateXAngle = rotateXAngle;
  190. birdCamera.rotateYAngle = rotateYAngle;
  191. birdCamera.Blink();
  192. }
  193. }
  194. IEnumerator CreatModels()
  195. {
  196. WaitForEndOfFrame wait = new WaitForEndOfFrame();
  197. WWW www = WWW.LoadFromCacheOrDownload($"{Application.streamingAssetsPath}/{model_ab_Name}", 0);
  198. yield return www;
  199. if (www.isDone)
  200. {
  201. var tempAb = www.assetBundle;
  202. GameObject[] objs = tempAb.LoadAllAssets<GameObject>();
  203. for (int i = 0; i < objs.Length; i++)
  204. {
  205. GameObject insObj = Instantiate(objs[i], this.transform);
  206. insObj.layer = 9;
  207. for (int j = 0; j < insObj.transform.childCount; j++)
  208. {
  209. insObj.transform.GetChild(j).gameObject.layer = 9;
  210. }
  211. models.Add(insObj);
  212. yield return wait;
  213. }
  214. if (findChangeObjs)
  215. {
  216. FarShowObj = new GameObject[FarObjName.Length];
  217. nearShowObj = new GameObject[NearObjName.Length];
  218. for (int i = 0; i < FarObjName.Length; i++)
  219. {
  220. FarShowObj[i] = this.transform.Find(FarObjName[i] + "(Clone)").gameObject;
  221. }
  222. for (int i = 0; i < nearShowObj.Length; i++)
  223. {
  224. nearShowObj[i] = this.transform.Find(NearObjName[i] + "(Clone)").gameObject;
  225. }
  226. }
  227. waitCreatModels = false;
  228. if (_ModelAni != null)
  229. {
  230. _ModelAni.FindObje();
  231. }
  232. }
  233. else
  234. {
  235. Debug.LogError($"{model_ab_Name}下载失败:{www.error}");
  236. }
  237. www.Dispose();
  238. }
  239. }