EffectController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class EffectController : SingletonMono<EffectController>
  5. {
  6. public List<EffectAttr> fxSerializedData
  7. {
  8. get
  9. {
  10. return this._fxSerializedData;
  11. }
  12. set
  13. {
  14. this._fxSerializedData = value;
  15. this._fxData = null;
  16. }
  17. }
  18. public Dictionary<int, EffectAttr> fxData
  19. {
  20. get
  21. {
  22. if (this._fxData == null)
  23. {
  24. this._fxData = new Dictionary<int, EffectAttr>();
  25. for (int i = 0; i < this._fxSerializedData.Count; i++)
  26. {
  27. EffectAttr effectAttr = this._fxSerializedData[i];
  28. this._fxData.Add(effectAttr.id, effectAttr);
  29. }
  30. }
  31. return this._fxData;
  32. }
  33. }
  34. private void Start()
  35. {
  36. if (EffectController.AllowPreload)
  37. {
  38. this.Preload();
  39. }
  40. }
  41. public Transform Generate(int effectId, Transform target = null, Vector3 position = default(Vector3), Vector3 rotation = default(Vector3), Vector3 scale = default(Vector3), bool useFxZNum = true)
  42. {
  43. GameObject gameObject = this.UsePool(this.fxData[effectId].effect.name);
  44. Transform transform = (!(gameObject != null)) ? UnityEngine.Object.Instantiate<Transform>(this.fxData[effectId].effect) : gameObject.transform;
  45. if (target != null)
  46. {
  47. transform.parent = target;
  48. transform.localPosition = position;
  49. transform.localRotation = Quaternion.Euler(Vector3.zero);
  50. if (!this.fxData[effectId].isFollow)
  51. {
  52. transform.parent = null;
  53. transform.position = target.position + position;
  54. }
  55. }
  56. else
  57. {
  58. transform.parent = null;
  59. transform.position = position;
  60. }
  61. switch (this.fxData[effectId].rotation)
  62. {
  63. case EffectController.FXRotationCondition.HaveEnemyDirectionY:
  64. transform.localRotation = Quaternion.Euler(new Vector3(rotation.x, (float)((target.localScale.x <= 0f) ? 0 : 180), rotation.z));
  65. break;
  66. case EffectController.FXRotationCondition.RandomRotationZ:
  67. transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, (float)UnityEngine.Random.Range(0, 360)));
  68. break;
  69. case EffectController.FXRotationCondition.Preference:
  70. transform.localRotation = Quaternion.Euler(rotation);
  71. break;
  72. case EffectController.FXRotationCondition.HaveEnemyDirectionZ:
  73. transform.localRotation = Quaternion.Euler(new Vector3(rotation.x, rotation.y, (float)((target.localScale.x <= 0f) ? 0 : 180)));
  74. break;
  75. case EffectController.FXRotationCondition.FollowPlayer:
  76. transform.localRotation = Quaternion.Euler(new Vector3(rotation.x, (float)((target.localScale.x >= 0f) ? 0 : 180), rotation.z));
  77. break;
  78. case EffectController.FXRotationCondition.FollowTargeRotation:
  79. transform.localRotation = target.rotation;
  80. break;
  81. }
  82. transform.localScale = ((!(scale != Vector3.zero)) ? this.fxData[effectId].scale : scale);
  83. if (useFxZNum)
  84. {
  85. Vector3 position2 = transform.position;
  86. position2.z = LayerManager.ZNum.Fx;
  87. transform.position = position2;
  88. }
  89. transform.gameObject.SetActive(true);
  90. return transform;
  91. }
  92. public GameObject UsePool(string effectName)
  93. {
  94. if (this._objectPoolDict.ContainsKey(effectName))
  95. {
  96. return this._objectPoolDict[effectName].GetObject();
  97. }
  98. return null;
  99. }
  100. public static void TerminateEffect(GameObject target)
  101. {
  102. AutoDisableEffect component = target.GetComponent<AutoDisableEffect>();
  103. if (component)
  104. {
  105. component.Disable(target.transform);
  106. }
  107. else
  108. {
  109. UnityEngine.Object.Destroy(target);
  110. }
  111. }
  112. private void Preload()
  113. {
  114. this._objectPoolDict = PoolController.EffectDict;
  115. for (int i = 0; i < this.fxSerializedData.Count; i++)
  116. {
  117. try
  118. {
  119. EffectAttr effectAttr = this.fxSerializedData[i];
  120. if (effectAttr.effect != null)
  121. {
  122. AutoDisableEffect component = effectAttr.effect.GetComponent<AutoDisableEffect>();
  123. if (!this._objectPoolDict.ContainsKey(effectAttr.effect.name) && component != null)
  124. {
  125. ObjectPool objectPool = new ObjectPool();
  126. base.StartCoroutine(objectPool.Init(effectAttr.effect.gameObject, base.gameObject, effectAttr.effectStartCount, effectAttr.maxCount));
  127. this._objectPoolDict.Add(effectAttr.effect.name, objectPool);
  128. }
  129. }
  130. }
  131. catch (Exception)
  132. {
  133. Log.Error(i);
  134. }
  135. }
  136. }
  137. [SerializeField]
  138. private List<EffectAttr> _fxSerializedData;
  139. private Dictionary<int, EffectAttr> _fxData;
  140. private Dictionary<string, ObjectPool> _objectPoolDict;
  141. public static bool AllowPreload = true;
  142. public enum FXRotationCondition
  143. {
  144. HaveEnemyDirectionY = 1,
  145. RandomRotationZ,
  146. Preference,
  147. HaveEnemyDirectionZ,
  148. FollowPlayer,
  149. FollowTargeRotation
  150. }
  151. }