StaticImportant.cs 8.5 KB

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