StaticImportant.cs 8.1 KB

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