StaticImportant.cs 7.3 KB

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