XffectComponent.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Xft
  6. {
  7. [ExecuteInEditMode]
  8. [AddComponentMenu("Xffect/XffectComponent")]
  9. public class XffectComponent : MonoBehaviour
  10. {
  11. public Camera MyCamera
  12. {
  13. get
  14. {
  15. if (this.mCamera == null || !this.mCamera.gameObject.activeInHierarchy || !this.mCamera.enabled)
  16. {
  17. this.FindMyCamera();
  18. }
  19. return this.mCamera;
  20. }
  21. set
  22. {
  23. this.mCamera = value;
  24. }
  25. }
  26. public void FindMyCamera()
  27. {
  28. int num = 1 << base.gameObject.layer;
  29. Camera[] array = UnityEngine.Object.FindObjectsOfType(typeof(Camera)) as Camera[];
  30. int i = 0;
  31. int num2 = array.Length;
  32. while (i < num2)
  33. {
  34. Camera camera = array[i];
  35. if ((camera.cullingMask & num) != 0)
  36. {
  37. this.mCamera = camera;
  38. return;
  39. }
  40. i++;
  41. }
  42. }
  43. private void Awake()
  44. {
  45. this.FindMyCamera();
  46. }
  47. private void DestoryMeshObject(GameObject obj)
  48. {
  49. UnityEngine.Object.Destroy(obj);
  50. }
  51. private MeshFilter CreateMeshObj(Material mat)
  52. {
  53. GameObject gameObject = new GameObject("xftmesh " + mat.name);
  54. gameObject.layer = base.gameObject.layer;
  55. this.MeshList.Add(gameObject);
  56. gameObject.AddComponent<MeshFilter>();
  57. gameObject.AddComponent<MeshRenderer>();
  58. XffectComponent.SetActive(gameObject, true);
  59. MeshFilter meshFilter = (MeshFilter)gameObject.GetComponent(typeof(MeshFilter));
  60. MeshRenderer meshRenderer = (MeshRenderer)gameObject.GetComponent(typeof(MeshRenderer));
  61. meshRenderer.castShadows = false;
  62. meshRenderer.receiveShadows = false;
  63. meshRenderer.GetComponent<Renderer>().sharedMaterial = mat;
  64. if (this.UseWith2DSprite)
  65. {
  66. meshRenderer.sortingLayerName = this.SortingLayerName;
  67. meshRenderer.sortingOrder = this.SortingOrder;
  68. }
  69. meshFilter.sharedMesh = new Mesh();
  70. return meshFilter;
  71. }
  72. public void Initialize()
  73. {
  74. if (this.EflList.Count > 0)
  75. {
  76. return;
  77. }
  78. List<GameObject> list = new List<GameObject>();
  79. IEnumerator enumerator = base.transform.GetEnumerator();
  80. try
  81. {
  82. while (enumerator.MoveNext())
  83. {
  84. object obj = enumerator.Current;
  85. Transform transform = (Transform)obj;
  86. if (transform.gameObject.name.Contains("xftmesh"))
  87. {
  88. list.Add(transform.gameObject);
  89. }
  90. }
  91. }
  92. finally
  93. {
  94. IDisposable disposable;
  95. if ((disposable = (enumerator as IDisposable)) != null)
  96. {
  97. disposable.Dispose();
  98. }
  99. }
  100. IEnumerator enumerator2 = base.transform.GetEnumerator();
  101. try
  102. {
  103. while (enumerator2.MoveNext())
  104. {
  105. object obj2 = enumerator2.Current;
  106. Transform transform2 = (Transform)obj2;
  107. EffectLayer effectLayer = (EffectLayer)transform2.GetComponent(typeof(EffectLayer));
  108. if (!(effectLayer == null))
  109. {
  110. if (effectLayer.Material == null)
  111. {
  112. UnityEngine.Debug.LogWarning("effect layer: " + effectLayer.gameObject.name + " has no material, please assign a material first!");
  113. }
  114. else
  115. {
  116. Material material = effectLayer.Material;
  117. material.renderQueue = material.shader.renderQueue;
  118. material.renderQueue += effectLayer.Depth;
  119. this.EflList.Add(effectLayer);
  120. if (this.MergeSameMaterialMesh)
  121. {
  122. if (!this.MatDic.ContainsKey(material.name))
  123. {
  124. MeshFilter meshFilter = this.CreateMeshObj(material);
  125. this.MatDic[material.name] = new VertexPool(meshFilter.sharedMesh, material);
  126. }
  127. }
  128. else
  129. {
  130. MeshFilter meshFilter2 = this.CreateMeshObj(material);
  131. this.MatList.Add(new VertexPool(meshFilter2.sharedMesh, material));
  132. }
  133. }
  134. }
  135. }
  136. }
  137. finally
  138. {
  139. IDisposable disposable2;
  140. if ((disposable2 = (enumerator2 as IDisposable)) != null)
  141. {
  142. disposable2.Dispose();
  143. }
  144. }
  145. foreach (GameObject obj3 in list)
  146. {
  147. this.DestoryMeshObject(obj3);
  148. }
  149. foreach (GameObject gameObject in this.MeshList)
  150. {
  151. gameObject.transform.parent = base.transform;
  152. gameObject.transform.position = Vector3.zero;
  153. gameObject.transform.rotation = Quaternion.identity;
  154. Vector3 zero = Vector3.zero;
  155. zero.x = 1f / Mathf.Clamp(gameObject.transform.parent.lossyScale.x, 1f, float.MaxValue);
  156. zero.y = 1f / Mathf.Clamp(gameObject.transform.parent.lossyScale.y, 1f, float.MaxValue);
  157. zero.z = 1f / Mathf.Clamp(gameObject.transform.parent.lossyScale.z, 1f, float.MaxValue);
  158. gameObject.transform.localScale = zero * this.Scale;
  159. }
  160. for (int i = 0; i < this.EflList.Count; i++)
  161. {
  162. EffectLayer effectLayer2 = this.EflList[i];
  163. if (this.MergeSameMaterialMesh)
  164. {
  165. effectLayer2.Vertexpool = this.MatDic[effectLayer2.Material.name];
  166. }
  167. else if (this.EflList.Count != this.MatList.Count)
  168. {
  169. UnityEngine.Debug.LogError("something wrong with the no merge mesh mat list!");
  170. effectLayer2.Vertexpool = this.MatList[0];
  171. }
  172. else
  173. {
  174. effectLayer2.Vertexpool = this.MatList[i];
  175. }
  176. }
  177. base.transform.localScale = Vector3.one;
  178. foreach (EffectLayer effectLayer3 in this.EflList)
  179. {
  180. effectLayer3.StartCustom();
  181. }
  182. this.EventList.Clear();
  183. IEnumerator enumerator6 = base.transform.GetEnumerator();
  184. try
  185. {
  186. while (enumerator6.MoveNext())
  187. {
  188. object obj4 = enumerator6.Current;
  189. Transform transform3 = (Transform)obj4;
  190. XftEventComponent component = transform3.GetComponent<XftEventComponent>();
  191. if (!(component == null))
  192. {
  193. this.EventList.Add(component);
  194. component.Initialize(this);
  195. }
  196. }
  197. }
  198. finally
  199. {
  200. IDisposable disposable3;
  201. if ((disposable3 = (enumerator6 as IDisposable)) != null)
  202. {
  203. disposable3.Dispose();
  204. }
  205. }
  206. this.Initialized = true;
  207. }
  208. private void Start()
  209. {
  210. if (!this.Initialized)
  211. {
  212. this.Initialize();
  213. }
  214. this.LastTime = (double)Time.realtimeSinceStartup;
  215. }
  216. public void Update()
  217. {
  218. this.CurTime = (double)Time.realtimeSinceStartup;
  219. float num = (float)(this.CurTime - this.LastTime);
  220. if (num > 0.1f)
  221. {
  222. num = 0.0333f;
  223. }
  224. if (this.Paused)
  225. {
  226. this.LastTime = this.CurTime;
  227. return;
  228. }
  229. if (!this.UpdateWhenOffScreen && !this.IsInCameraView() && !this.EditView)
  230. {
  231. this.LastTime = this.CurTime;
  232. return;
  233. }
  234. if (!this.IgnoreTimeScale)
  235. {
  236. num *= Time.timeScale;
  237. }
  238. this.ElapsedTime += num;
  239. for (int i = 0; i < this.EflList.Count; i++)
  240. {
  241. if (this.EflList[i] == null)
  242. {
  243. return;
  244. }
  245. EffectLayer effectLayer = this.EflList[i];
  246. if (this.ElapsedTime > effectLayer.StartTime && XffectComponent.IsActive(effectLayer.gameObject))
  247. {
  248. effectLayer.FixedUpdateCustom(num);
  249. }
  250. }
  251. for (int j = 0; j < this.EventList.Count; j++)
  252. {
  253. XftEventComponent xftEventComponent = this.EventList[j];
  254. if (XffectComponent.IsActive(xftEventComponent.gameObject))
  255. {
  256. xftEventComponent.UpdateCustom(num);
  257. }
  258. }
  259. this.LastTime = this.CurTime;
  260. }
  261. public void ResetEditorEvents()
  262. {
  263. for (int i = 0; i < this.EventList.Count; i++)
  264. {
  265. XftEventComponent xftEventComponent = this.EventList[i];
  266. xftEventComponent.ResetCustom();
  267. }
  268. }
  269. private void DoFinish()
  270. {
  271. this.mIsActive = false;
  272. for (int i = 0; i < this.EflList.Count; i++)
  273. {
  274. EffectLayer effectLayer = this.EflList[i];
  275. effectLayer.Reset();
  276. }
  277. this.DeActive();
  278. this.ElapsedTime = 0f;
  279. if (this.AutoDestroy)
  280. {
  281. UnityEngine.Object.Destroy(base.gameObject);
  282. }
  283. }
  284. private bool IsInCameraView()
  285. {
  286. for (int i = 0; i < this.MeshList.Count; i++)
  287. {
  288. GameObject gameObject = this.MeshList[i];
  289. if (gameObject != null)
  290. {
  291. MeshRenderer component = gameObject.GetComponent<MeshRenderer>();
  292. if (component.isVisible)
  293. {
  294. return true;
  295. }
  296. }
  297. }
  298. for (int j = 0; j < this.EflList.Count; j++)
  299. {
  300. EffectLayer effectLayer = this.EflList[j];
  301. Vector3 position = effectLayer.ClientTransform.position;
  302. Vector3 vector = this.MyCamera.WorldToViewportPoint(position);
  303. if (vector.x >= 0f && vector.x <= 1f && vector.y >= 0f && vector.y <= 1f && vector.z > 0f)
  304. {
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. private void UpdateMeshObj()
  311. {
  312. for (int i = 0; i < this.MeshList.Count; i++)
  313. {
  314. GameObject gameObject = this.MeshList[i];
  315. if (gameObject != null)
  316. {
  317. gameObject.transform.position = Vector3.zero;
  318. gameObject.transform.rotation = Quaternion.identity;
  319. Vector3 zero = Vector3.zero;
  320. zero.x = 1f / gameObject.transform.parent.lossyScale.x;
  321. zero.y = 1f / gameObject.transform.parent.lossyScale.y;
  322. zero.z = 1f;
  323. gameObject.transform.localScale = zero * this.Scale;
  324. }
  325. }
  326. if (this.MergeSameMaterialMesh)
  327. {
  328. foreach (KeyValuePair<string, VertexPool> keyValuePair in this.MatDic)
  329. {
  330. keyValuePair.Value.LateUpdate();
  331. }
  332. }
  333. else
  334. {
  335. for (int j = 0; j < this.MatList.Count; j++)
  336. {
  337. VertexPool vertexPool = this.MatList[j];
  338. vertexPool.LateUpdate();
  339. }
  340. }
  341. }
  342. private void LateUpdate()
  343. {
  344. if (!this.UpdateWhenOffScreen && !this.IsInCameraView() && !this.EditView)
  345. {
  346. return;
  347. }
  348. this.UpdateMeshObj();
  349. if (this.ElapsedTime > this.LifeTime && this.LifeTime >= 0f)
  350. {
  351. this.DoFinish();
  352. }
  353. else if (this.LifeTime < 0f && this.EflList.Count > 0)
  354. {
  355. float deltaTime = (float)(this.CurTime - this.LastTime);
  356. bool flag = true;
  357. for (int i = 0; i < this.EflList.Count; i++)
  358. {
  359. EffectLayer effectLayer = this.EflList[i];
  360. if (!effectLayer.EmitOver(deltaTime))
  361. {
  362. flag = false;
  363. }
  364. }
  365. if (flag)
  366. {
  367. this.DoFinish();
  368. }
  369. }
  370. }
  371. private void OnDrawGizmosSelected()
  372. {
  373. }
  374. public void Active()
  375. {
  376. if (!this.Initialized)
  377. {
  378. this.Initialize();
  379. }
  380. base.gameObject.SetActive(true);
  381. this.Reset();
  382. }
  383. public void DeActive()
  384. {
  385. for (int i = 0; i < this.EventList.Count; i++)
  386. {
  387. XftEventComponent xftEventComponent = this.EventList[i];
  388. xftEventComponent.ResetCustom();
  389. }
  390. base.gameObject.SetActive(false);
  391. }
  392. public bool IsPlaying
  393. {
  394. get
  395. {
  396. return this.mIsActive;
  397. }
  398. }
  399. public void Reset()
  400. {
  401. this.mIsActive = true;
  402. this.ElapsedTime = 0f;
  403. this.Start();
  404. for (int i = 0; i < this.EflList.Count; i++)
  405. {
  406. EffectLayer effectLayer = this.EflList[i];
  407. effectLayer.Reset();
  408. }
  409. for (int j = 0; j < this.EventList.Count; j++)
  410. {
  411. XftEventComponent xftEventComponent = this.EventList[j];
  412. xftEventComponent.ResetCustom();
  413. }
  414. }
  415. public void StopSmoothly(float fadeTime)
  416. {
  417. for (int i = 0; i < this.EflList.Count; i++)
  418. {
  419. EffectLayer effectLayer = this.EflList[i];
  420. effectLayer.StopSmoothly(fadeTime);
  421. }
  422. }
  423. public void ActiveNoInterrupt()
  424. {
  425. if (XffectComponent.IsActive(base.gameObject))
  426. {
  427. return;
  428. }
  429. this.Active();
  430. }
  431. public void SetScale(Vector2 scale, string eflName)
  432. {
  433. foreach (EffectLayer effectLayer in this.EflList)
  434. {
  435. if (effectLayer.gameObject.name == eflName)
  436. {
  437. effectLayer.SetScale(scale);
  438. }
  439. }
  440. }
  441. public void SetColor(Color color, string eflName)
  442. {
  443. foreach (EffectLayer effectLayer in this.EflList)
  444. {
  445. if (effectLayer.gameObject.name == eflName)
  446. {
  447. effectLayer.SetColor(color);
  448. }
  449. }
  450. }
  451. public void SetRotation(float angle, string eflName)
  452. {
  453. foreach (EffectLayer effectLayer in this.EflList)
  454. {
  455. if (effectLayer.gameObject.name == eflName)
  456. {
  457. effectLayer.SetRotation(angle);
  458. }
  459. }
  460. }
  461. public void SetGravityGoal(Transform goal, string eflName)
  462. {
  463. foreach (EffectLayer effectLayer in this.EflList)
  464. {
  465. if (effectLayer.gameObject.name == eflName && effectLayer.GravityAffectorEnable)
  466. {
  467. effectLayer.SetArractionAffectorGoal(goal);
  468. }
  469. }
  470. }
  471. public void SetCollisionGoalPos(Transform pos, string eflName)
  472. {
  473. foreach (EffectLayer effectLayer in this.EflList)
  474. {
  475. if (effectLayer.gameObject.name == eflName && effectLayer.UseCollisionDetection)
  476. {
  477. effectLayer.SetCollisionGoalPos(pos);
  478. }
  479. }
  480. }
  481. public void ResetCamera(Camera cam)
  482. {
  483. }
  484. public void SetClient(Transform client)
  485. {
  486. foreach (EffectLayer effectLayer in this.EflList)
  487. {
  488. effectLayer.SetClient(client);
  489. }
  490. }
  491. public void SetDirectionAxis(Vector3 axis)
  492. {
  493. foreach (EffectLayer effectLayer in this.EflList)
  494. {
  495. effectLayer.OriVelocityAxis = axis;
  496. }
  497. }
  498. public void SetEmitPosition(Vector3 pos)
  499. {
  500. foreach (EffectLayer effectLayer in this.EflList)
  501. {
  502. effectLayer.EmitPoint = pos;
  503. }
  504. }
  505. public void SetCollisionGoalPos(Transform pos)
  506. {
  507. foreach (EffectLayer effectLayer in this.EflList)
  508. {
  509. if (effectLayer.UseCollisionDetection)
  510. {
  511. effectLayer.SetCollisionGoalPos(pos);
  512. }
  513. }
  514. }
  515. public void SetArractionAffectorGoal(Transform goal)
  516. {
  517. foreach (EffectLayer effectLayer in this.EflList)
  518. {
  519. if (effectLayer.GravityAffectorEnable)
  520. {
  521. effectLayer.SetArractionAffectorGoal(goal);
  522. }
  523. }
  524. }
  525. public void SetArractionAffectorGoal(Transform goal, string eflName)
  526. {
  527. foreach (EffectLayer effectLayer in this.EflList)
  528. {
  529. if (effectLayer.gameObject.name == eflName && effectLayer.GravityAffectorEnable)
  530. {
  531. effectLayer.SetArractionAffectorGoal(goal);
  532. }
  533. }
  534. }
  535. public EffectNode EmitByPos(Vector3 pos)
  536. {
  537. if (this.EflList.Count > 1)
  538. {
  539. UnityEngine.Debug.LogWarning("EmitByPos only support one effect layer!");
  540. }
  541. EffectLayer effectLayer = this.EflList[0];
  542. return effectLayer.EmitByPos(pos);
  543. }
  544. public void StopEmit()
  545. {
  546. for (int i = 0; i < this.EflList.Count; i++)
  547. {
  548. EffectLayer effectLayer = this.EflList[i];
  549. effectLayer.StopEmit();
  550. }
  551. }
  552. public static bool IsActive(GameObject obj)
  553. {
  554. return obj.activeSelf;
  555. }
  556. public static void SetActive(GameObject obj, bool flag)
  557. {
  558. obj.SetActive(flag);
  559. }
  560. public static string CurVersion = "4.4.1";
  561. public string MyVersion = "4.4.1";
  562. private Dictionary<string, VertexPool> MatDic = new Dictionary<string, VertexPool>();
  563. private List<EffectLayer> EflList = new List<EffectLayer>();
  564. private List<XftEventComponent> EventList = new List<XftEventComponent>();
  565. private List<VertexPool> MatList = new List<VertexPool>();
  566. public float LifeTime = -1f;
  567. public bool IgnoreTimeScale;
  568. protected float ElapsedTime;
  569. protected bool Initialized;
  570. protected List<GameObject> MeshList = new List<GameObject>();
  571. protected double LastTime;
  572. protected double CurTime;
  573. public bool EditView;
  574. public float Scale = 1f;
  575. public int MaxFps = 60;
  576. public bool AutoDestroy;
  577. public bool MergeSameMaterialMesh = true;
  578. public bool Paused;
  579. public bool UpdateWhenOffScreen = true;
  580. public bool UseWith2DSprite;
  581. public string SortingLayerName = "Default";
  582. public int SortingOrder;
  583. public float PlaybackTime = 1f;
  584. protected bool mIsActive;
  585. protected Camera mCamera;
  586. }
  587. }