UI_ParticleImageFit.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using UnityEngine;
  3. public class UI_ParticleImageFit : UI_ScreenFitBase
  4. {
  5. protected override void Start()
  6. {
  7. this.m_MyParticle = base.GetComponent<ParticleSystem>();
  8. this.m_FirstSpeed = this.m_MyParticle.main.startSpeedMultiplier;
  9. this.m_FirstLifeTime = this.m_MyParticle.main.startLifetimeMultiplier;
  10. base.Start();
  11. }
  12. protected override void Update()
  13. {
  14. base.Update();
  15. }
  16. protected override void FitAction()
  17. {
  18. if (!this.FitImage)
  19. {
  20. return;
  21. }
  22. switch (this.m_ParticleType)
  23. {
  24. case UI_ParticleImageFit.ParticleType.Box:
  25. this.FitBox();
  26. break;
  27. case UI_ParticleImageFit.ParticleType.Edge:
  28. this.FitEdge();
  29. break;
  30. }
  31. }
  32. private void FitBox()
  33. {
  34. ParticleSystem.ShapeModule shape = this.m_MyParticle.shape;
  35. Vector3 vector = this.NguiToWorldSize();
  36. Vector3 box = vector;
  37. if (this.m_AsspectRatio.x != 0f && this.m_AsspectRatio.y != 0f)
  38. {
  39. UI_ParticleImageFit.FitType fitType = this.m_FitType;
  40. if (fitType != UI_ParticleImageFit.FitType.Horizontal)
  41. {
  42. if (fitType == UI_ParticleImageFit.FitType.Vertical)
  43. {
  44. box.x = vector.y * (this.m_AsspectRatio.x / this.m_AsspectRatio.y);
  45. }
  46. }
  47. else
  48. {
  49. box.y = vector.x * (this.m_AsspectRatio.y / this.m_AsspectRatio.x);
  50. }
  51. }
  52. shape.box = box;
  53. }
  54. private void FitEdge()
  55. {
  56. ParticleSystem.MainModule main = this.m_MyParticle.main;
  57. Vector3 vector = this.NguiToWorldSize();
  58. if (this.m_SpeedAdjust)
  59. {
  60. if (this.m_FitType == UI_ParticleImageFit.FitType.Horizontal)
  61. {
  62. main.startSpeedMultiplier = vector.x / this.m_FirstLifeTime;
  63. }
  64. else
  65. {
  66. main.startSpeedMultiplier = vector.y / this.m_FirstLifeTime;
  67. }
  68. main.startLifetimeMultiplier = this.m_FirstLifeTime;
  69. }
  70. else
  71. {
  72. if (this.m_FitType == UI_ParticleImageFit.FitType.Horizontal)
  73. {
  74. main.startLifetimeMultiplier = vector.x / this.m_FirstSpeed;
  75. }
  76. else
  77. {
  78. main.startLifetimeMultiplier = vector.y / this.m_FirstSpeed;
  79. }
  80. main.startSpeedMultiplier = this.m_FirstSpeed;
  81. }
  82. }
  83. private Vector3 NguiToWorldSize()
  84. {
  85. Vector3 zero = Vector3.zero;
  86. switch (this.m_FitType)
  87. {
  88. case UI_ParticleImageFit.FitType.Horizontal:
  89. zero.x = (float)Screen.width;
  90. break;
  91. case UI_ParticleImageFit.FitType.Vertical:
  92. zero.y = (float)Screen.height;
  93. break;
  94. case UI_ParticleImageFit.FitType.All:
  95. zero.x = (float)Screen.width;
  96. zero.y = (float)Screen.height;
  97. break;
  98. }
  99. Vector3 vector = UI_ScreenFitBase.PointToScreenPos(zero) * 2f;
  100. Vector3 zero2 = Vector3.zero;
  101. switch (this.m_FitType)
  102. {
  103. case UI_ParticleImageFit.FitType.Horizontal:
  104. zero2.x = (float)Screen.width * ((float)this.FitImage.width / vector.x + 0.5f);
  105. break;
  106. case UI_ParticleImageFit.FitType.Vertical:
  107. zero2.y = (float)Screen.height * ((float)this.FitImage.height / vector.y + 0.5f);
  108. break;
  109. case UI_ParticleImageFit.FitType.All:
  110. zero2.x = (float)Screen.width * ((float)this.FitImage.width / vector.x + 0.5f);
  111. zero2.y = (float)Screen.height * ((float)this.FitImage.height / vector.y + 0.5f);
  112. break;
  113. }
  114. Vector3 vector2 = UI_ScreenFitBase.m_NGUICamera.ScreenToWorldPoint(zero2) - UI_ScreenFitBase.m_NGUICamera.transform.position;
  115. if (this.m_UseFillAmount)
  116. {
  117. if (this.m_FillReverse)
  118. {
  119. vector2 *= 1f - this.FitImage.fillAmount;
  120. }
  121. else
  122. {
  123. vector2 *= this.FitImage.fillAmount;
  124. }
  125. }
  126. else if (this.m_UseSizeRate)
  127. {
  128. vector2.x *= this.SizeRateX;
  129. vector2.y *= this.SizeRateY;
  130. }
  131. return vector2;
  132. }
  133. public UIBasicSprite FitImage;
  134. [SerializeField]
  135. private UI_ParticleImageFit.FitType m_FitType;
  136. [SerializeField]
  137. private UI_ParticleImageFit.ParticleType m_ParticleType;
  138. [SerializeField]
  139. [Header("スプライトのFillAmountを使うかどうか")]
  140. private bool m_UseFillAmount;
  141. [SerializeField]
  142. private bool m_FillReverse;
  143. [SerializeField]
  144. [Header("画像に対して一定の割合を使うか")]
  145. private bool m_UseSizeRate;
  146. [Range(0f, 1f)]
  147. public float SizeRateX = 1f;
  148. [Range(0f, 1f)]
  149. public float SizeRateY = 1f;
  150. [Header("ParticleType->Box時の設定項目")]
  151. [SerializeField]
  152. [Header("パーティクルの縦横比率((0, 0)で比率固定されない)")]
  153. private Vector2 m_AsspectRatio = Vector2.zero;
  154. [Header("ParticleType->Edge時の設定項目")]
  155. [SerializeField]
  156. [Header("速度で調整するか(しない場合は生存時間で調整)")]
  157. private bool m_SpeedAdjust;
  158. private float m_FirstSpeed;
  159. private float m_FirstLifeTime;
  160. private ParticleSystem m_MyParticle;
  161. private enum FitType
  162. {
  163. Horizontal,
  164. Vertical,
  165. All
  166. }
  167. private enum ParticleType
  168. {
  169. Box,
  170. Edge,
  171. Other
  172. }
  173. }