HTExplosion.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // HTExplosion v1.0 (July 2012)
  2. // HTExplosion.cs library is copyright (c) of Hedgehog Team
  3. // Please send feedback or bug reports to the.hedgehog.team@gmail.com
  4. using UnityEngine;
  5. using System.Collections;
  6. /// <summary>
  7. /// HThis allows the creation of a particle and play an animated sprite from spritesheet.
  8. /// </summary>
  9. public class HTExplosion : MonoBehaviour {
  10. #region enumeration
  11. /// <summary>
  12. /// The rendering mode for particles..
  13. /// </summary>
  14. public enum CameraFacingMode{
  15. /// <summary>
  16. /// Render the particles as billboards facing the camera with tag "MainCamera". (Default)
  17. /// </summary>
  18. BillBoard,
  19. /// <summary>
  20. /// Render the particles as billboards always facing up along the y-Axis.
  21. /// </summary>
  22. Horizontal,
  23. /// <summary>
  24. /// Render the particles as billboards always facing up along the X-Axis.
  25. /// </summary>
  26. Vertical,
  27. /// <summary>
  28. /// The particle never facinc up the camera.
  29. /// </summary>
  30. Never
  31. };
  32. #endregion
  33. #region public properties
  34. /// <summary>
  35. /// The sprite sheet material.
  36. /// </summary>
  37. public Material spriteSheetMaterial;
  38. /// <summary>
  39. /// The number of sprtie on the spritesheet.
  40. /// </summary>
  41. public int spriteCount;
  42. /// <summary>
  43. /// The uv animation tile x.
  44. /// </summary>
  45. public int uvAnimationTileX;
  46. /// <summary>
  47. /// The uv animation tile y.
  48. /// </summary>
  49. public int uvAnimationTileY;
  50. /// <summary>
  51. /// The number of images per second to play animation
  52. /// </summary>
  53. public int framesPerSecond;
  54. /// <summary>
  55. /// The initial size of the explosion
  56. /// </summary>
  57. public Vector3 size = new Vector3(1,1,1);
  58. /// <summary>
  59. /// The speed growing.
  60. /// </summary>
  61. public float speedGrowing;
  62. /// <summary>
  63. /// Applied a rondom rotation on z-Axis.
  64. /// </summary>
  65. public bool randomRotation;
  66. /// <summary>
  67. /// The is one shot animation.
  68. /// </summary>
  69. public bool isOneShot=true;
  70. /// <summary>
  71. /// The billboarding mode
  72. /// </summary>
  73. public CameraFacingMode billboarding; // Bilboardin mode
  74. /// <summary>
  75. /// The add light effect.
  76. /// </summary>
  77. public bool addLightEffect=false;
  78. /// <summary>
  79. /// The light range.
  80. /// </summary>
  81. public float lightRange;
  82. /// <summary>
  83. /// The color of the light.
  84. /// </summary>
  85. public Color lightColor;
  86. /// <summary>
  87. /// The light fade speed.
  88. /// </summary>
  89. public float lightFadeSpeed=1;
  90. #endregion
  91. #region private properties
  92. /// <summary>
  93. /// The material with the sprite speed.
  94. /// </summary>
  95. private Material mat;
  96. /// <summary>
  97. /// The mesh.
  98. /// </summary>
  99. private Mesh mesh;
  100. /// <summary>
  101. /// The mesh render.
  102. /// </summary>
  103. private MeshRenderer meshRender;
  104. /// <summary>
  105. /// The audio source.
  106. /// </summary>
  107. private AudioSource soundEffect;
  108. /// <summary>
  109. /// The start time of the explosion
  110. /// </summary>
  111. private float startTime;
  112. /// <summary>
  113. /// The main camera.
  114. /// </summary>
  115. private Camera mainCam;
  116. /// <summary>
  117. /// The effect end.
  118. /// </summary>
  119. private bool effectEnd=false;
  120. /// <summary>
  121. /// The random Z angle.
  122. /// </summary>
  123. private float randomZAngle;
  124. #endregion
  125. #region MonoBehaviour methods
  126. /// <summary>
  127. /// Awake this instance.
  128. /// </summary>
  129. void Awake(){
  130. // Creation of the particle
  131. CreateParticle();
  132. // We search the main camera
  133. mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
  134. // do we have sound effect ?
  135. soundEffect = GetComponent("AudioSource") as AudioSource;
  136. // Add light
  137. if (addLightEffect){
  138. gameObject.AddComponent<Light>();
  139. gameObject.GetComponent<Light>().color = lightColor;
  140. gameObject.GetComponent<Light>().range = lightRange;
  141. }
  142. GetComponent<Renderer>().enabled = false;
  143. }
  144. // Use this for initialization
  145. void Start () {
  146. startTime = Time.time;
  147. transform.localScale = size;
  148. if (randomRotation){
  149. randomZAngle = Random.Range(-180.0f,180.0f);
  150. }
  151. else{
  152. randomZAngle = 0;
  153. }
  154. }
  155. /// <summary>
  156. /// Update this instance.
  157. /// </summary>
  158. void Update () {
  159. bool end=false;
  160. Camera_BillboardingMode();
  161. // Calculate index
  162. float index = (Time.time-startTime) * framesPerSecond;
  163. if ((index<=spriteCount || !isOneShot) && !effectEnd ){
  164. // repeat when exhausting all frames
  165. index = index % (uvAnimationTileX * uvAnimationTileY);
  166. if (index== spriteCount){
  167. startTime = Time.time;
  168. index=0;
  169. }
  170. // Size of every tile
  171. Vector2 size = new Vector2 (1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY);
  172. // split into horizontal and vertical index
  173. float uIndex = Mathf.Floor(index % uvAnimationTileX);
  174. float vIndex = Mathf.Floor(index / uvAnimationTileX);
  175. // build offset
  176. Vector2 offset = new Vector2 (uIndex * size.x , 1.0f - size.y - vIndex * size.y);
  177. GetComponent<Renderer>().material.SetTextureOffset ("_MainTex", offset);
  178. GetComponent<Renderer>().material.SetTextureScale ("_MainTex", size);
  179. // growing
  180. transform.localScale += new Vector3(speedGrowing,speedGrowing,speedGrowing) * Time.deltaTime ;
  181. GetComponent<Renderer>().enabled = true;
  182. }
  183. else{
  184. effectEnd = true;
  185. GetComponent<Renderer>().enabled = false;
  186. end = true;
  187. if (soundEffect){
  188. if (soundEffect.isPlaying){
  189. end = false;
  190. }
  191. }
  192. if (addLightEffect && end){
  193. if (gameObject.GetComponent<Light>().intensity>0){
  194. end = false;
  195. }
  196. }
  197. if (end){
  198. Destroy(gameObject);
  199. }
  200. }
  201. // Light effect
  202. if (addLightEffect && lightFadeSpeed!=0){
  203. gameObject.GetComponent<Light>().intensity -= lightFadeSpeed*Time.deltaTime;
  204. }
  205. }
  206. #endregion
  207. #region private methods
  208. /// <summary>
  209. /// Creates the particle.
  210. /// </summary>
  211. void CreateParticle(){
  212. mesh = gameObject.AddComponent<MeshFilter>().mesh;
  213. meshRender = gameObject.AddComponent<MeshRenderer>();
  214. mesh.vertices = new Vector3[] { new Vector3(-0.5f,-0.5f,0f),new Vector3(-0.5f,0.5f,0f), new Vector3(0.5f,0.5f,0f), new Vector3(0.5f,-0.5f,0f) };
  215. mesh.triangles = new int[] {0,1,2, 2,3,0 };
  216. mesh.uv = new Vector2[] { new Vector2 (0f, 0f), new Vector2 (0f, 1f), new Vector2 (1f, 1f), new Vector2(1f,0f)};
  217. meshRender.castShadows = false;
  218. meshRender.receiveShadows = false;
  219. mesh.RecalculateNormals();
  220. GetComponent<Renderer>().material= spriteSheetMaterial;
  221. }
  222. /// <summary>
  223. /// Camera_s the billboarding mode.
  224. /// </summary>
  225. void Camera_BillboardingMode(){
  226. Vector3 lookAtVector = mainCam.transform.position - transform.position;
  227. switch (billboarding){
  228. case CameraFacingMode.BillBoard:
  229. transform.LookAt( lookAtVector);
  230. break;
  231. case CameraFacingMode.Horizontal:
  232. lookAtVector.x = lookAtVector.z =0 ;
  233. transform.LookAt(mainCam.transform.position- lookAtVector);
  234. break;
  235. case CameraFacingMode.Vertical:
  236. lookAtVector.y=lookAtVector.z =0;
  237. transform.LookAt(mainCam.transform.position- lookAtVector);
  238. break;
  239. }
  240. transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y,randomZAngle);
  241. }
  242. #endregion
  243. }