StaticImportant.cs 8.2 KB

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