StaticImportant.cs 7.6 KB

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