StaticImportant.cs 6.5 KB

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