OnionCreator.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using UnityEngine;
  3. namespace OnionSkin
  4. {
  5. public class OnionCreator : MonoBehaviour
  6. {
  7. private void Update()
  8. {
  9. if (!this._open)
  10. {
  11. return;
  12. }
  13. OnionCreator.GeneratorMode generatorMode = this.generatorMode;
  14. if (generatorMode != OnionCreator.GeneratorMode.Time)
  15. {
  16. if (generatorMode == OnionCreator.GeneratorMode.Distance)
  17. {
  18. if (Vector3.Distance(this._lastPostion, base.transform.position) > this.rate)
  19. {
  20. this.GeneratorGhost(this._lastPostion, base.transform.position, this.rate);
  21. this._lastPostion = base.transform.position;
  22. }
  23. }
  24. }
  25. else
  26. {
  27. this._onionTimer += Time.deltaTime;
  28. if (this._onionTimer >= this.rate)
  29. {
  30. this._onionTimer = 0f;
  31. this.GeneratorGhost();
  32. }
  33. }
  34. if (!this._autoClose)
  35. {
  36. return;
  37. }
  38. if (Time.time >= this._autoCloseTime)
  39. {
  40. this.Close();
  41. }
  42. }
  43. public void Open(bool _autoClose = true, float _autoCloseTime = 1f, GameObject[] cloneds = null)
  44. {
  45. if (this._open)
  46. {
  47. if (!_autoClose)
  48. {
  49. return;
  50. }
  51. float num = Time.time + _autoCloseTime;
  52. if (num < this._autoCloseTime)
  53. {
  54. return;
  55. }
  56. }
  57. if (cloneds == null)
  58. {
  59. this._needBeClones = new GameObject[]
  60. {
  61. base.gameObject
  62. };
  63. }
  64. else
  65. {
  66. this._needBeClones = cloneds;
  67. }
  68. this._open = true;
  69. if (_autoClose)
  70. {
  71. this._autoClose = true;
  72. this._autoCloseTime = Time.time + _autoCloseTime;
  73. }
  74. if (this.generatorMode == OnionCreator.GeneratorMode.Time)
  75. {
  76. if (this.executionMode == OnionCreator.ExecutionMode.Immediately)
  77. {
  78. this._onionTimer = float.MaxValue;
  79. }
  80. if (this.executionMode == OnionCreator.ExecutionMode.NextUpdate)
  81. {
  82. this._onionTimer = 0f;
  83. }
  84. }
  85. if (this.generatorMode == OnionCreator.GeneratorMode.Distance)
  86. {
  87. if (this.executionMode == OnionCreator.ExecutionMode.NextUpdate)
  88. {
  89. this._lastPostion = base.transform.position;
  90. }
  91. if (this.executionMode == OnionCreator.ExecutionMode.Immediately)
  92. {
  93. this._lastPostion = Vector3.zero;
  94. }
  95. }
  96. }
  97. public void Close()
  98. {
  99. this._open = false;
  100. this._autoClose = false;
  101. }
  102. private void GeneratorGhost()
  103. {
  104. if (this._needBeClones == null || this._needBeClones.Length == 0)
  105. {
  106. return;
  107. }
  108. this.CloneGhostAtPos(base.transform.position);
  109. }
  110. private void GeneratorGhost(Vector3 fromPos, Vector3 toPos, float rate)
  111. {
  112. if (this._needBeClones == null || this._needBeClones.Length == 0)
  113. {
  114. return;
  115. }
  116. float num = Vector3.Distance(fromPos, toPos) / rate;
  117. int num2 = 0;
  118. while ((float)num2 < num)
  119. {
  120. Vector3 pos = Vector3.Slerp(fromPos, toPos, (float)num2 / num);
  121. this.CloneGhostAtPos(pos);
  122. num2++;
  123. }
  124. }
  125. private void CloneGhostAtPos(Vector3 pos)
  126. {
  127. for (int i = 0; i < this._needBeClones.Length; i++)
  128. {
  129. GameObject gameObject = this._needBeClones[i];
  130. MeshRenderer component = gameObject.GetComponent<MeshRenderer>();
  131. if (gameObject.activeSelf && component.enabled)
  132. {
  133. Transform transform = R.Effect.Generate(this._onionId, null, pos + new Vector3(0f, 0f, 0.001f), Vector3.zero, default(Vector3), true);
  134. if (OnionCreator._ghostParent == null)
  135. {
  136. OnionCreator._ghostParent = new GameObject("GhostParent").transform;
  137. OnionCreator._ghostParent.parent = R.Effect.transform;
  138. }
  139. transform.parent = OnionCreator._ghostParent;
  140. transform.localScale = base.transform.localScale;
  141. OnionController component2 = transform.GetComponent<OnionController>();
  142. component2.Clone(gameObject);
  143. component2.SetDisappear(this.disappearCurve, this.disappearTime);
  144. }
  145. }
  146. }
  147. private static Transform _ghostParent;
  148. public float rate = 0.1f;
  149. public float disappearTime = 1f;
  150. public AnimationCurve disappearCurve = AnimationCurve.Linear(0f, 1f, 1f, 0f);
  151. private bool _open;
  152. private bool _autoClose;
  153. private float _autoCloseTime = 1f;
  154. public OnionCreator.GeneratorMode generatorMode = OnionCreator.GeneratorMode.Time;
  155. public OnionCreator.ExecutionMode executionMode;
  156. [SerializeField]
  157. private int _onionId = 218;
  158. private float _onionTimer = 10f;
  159. private Vector3 _lastPostion;
  160. private GameObject[] _needBeClones;
  161. public enum ExecutionMode
  162. {
  163. Immediately,
  164. NextUpdate
  165. }
  166. public enum GeneratorMode
  167. {
  168. Distance,
  169. Time
  170. }
  171. }
  172. }