StaticImportant.cs 8.0 KB

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