StaticImportant.cs 8.6 KB

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