// HTExplosion v1.0 (July 2012) // HTExplosion.cs library is copyright (c) of Hedgehog Team // Please send feedback or bug reports to the.hedgehog.team@gmail.com using UnityEngine; using System.Collections; /// /// HThis allows the creation of a particle and play an animated sprite from spritesheet. /// public class HTExplosion : MonoBehaviour { #region enumeration /// /// The rendering mode for particles.. /// public enum CameraFacingMode{ /// /// Render the particles as billboards facing the camera with tag "MainCamera". (Default) /// BillBoard, /// /// Render the particles as billboards always facing up along the y-Axis. /// Horizontal, /// /// Render the particles as billboards always facing up along the X-Axis. /// Vertical, /// /// The particle never facinc up the camera. /// Never }; #endregion #region public properties /// /// The sprite sheet material. /// public Material spriteSheetMaterial; /// /// The number of sprtie on the spritesheet. /// public int spriteCount; /// /// The uv animation tile x. /// public int uvAnimationTileX; /// /// The uv animation tile y. /// public int uvAnimationTileY; /// /// The number of images per second to play animation /// public int framesPerSecond; /// /// The initial size of the explosion /// public Vector3 size = new Vector3(1,1,1); /// /// The speed growing. /// public float speedGrowing; /// /// Applied a rondom rotation on z-Axis. /// public bool randomRotation; /// /// The is one shot animation. /// public bool isOneShot=true; /// /// The billboarding mode /// public CameraFacingMode billboarding; // Bilboardin mode /// /// The add light effect. /// public bool addLightEffect=false; /// /// The light range. /// public float lightRange; /// /// The color of the light. /// public Color lightColor; /// /// The light fade speed. /// public float lightFadeSpeed=1; #endregion #region private properties /// /// The material with the sprite speed. /// private Material mat; /// /// The mesh. /// private Mesh mesh; /// /// The mesh render. /// private MeshRenderer meshRender; /// /// The audio source. /// private AudioSource soundEffect; /// /// The start time of the explosion /// private float startTime; /// /// The main camera. /// private Camera mainCam; /// /// The effect end. /// private bool effectEnd=false; /// /// The random Z angle. /// private float randomZAngle; #endregion #region MonoBehaviour methods /// /// Awake this instance. /// void Awake(){ // Creation of the particle CreateParticle(); // We search the main camera mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); // do we have sound effect ? soundEffect = GetComponent("AudioSource") as AudioSource; // Add light if (addLightEffect){ gameObject.AddComponent(); gameObject.GetComponent().color = lightColor; gameObject.GetComponent().range = lightRange; } GetComponent().enabled = false; } // Use this for initialization void Start () { startTime = Time.time; transform.localScale = size; if (randomRotation){ randomZAngle = Random.Range(-180.0f,180.0f); } else{ randomZAngle = 0; } } /// /// Update this instance. /// void Update () { bool end=false; Camera_BillboardingMode(); // Calculate index float index = (Time.time-startTime) * framesPerSecond; if ((index<=spriteCount || !isOneShot) && !effectEnd ){ // repeat when exhausting all frames index = index % (uvAnimationTileX * uvAnimationTileY); if (index== spriteCount){ startTime = Time.time; index=0; } // Size of every tile Vector2 size = new Vector2 (1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY); // split into horizontal and vertical index float uIndex = Mathf.Floor(index % uvAnimationTileX); float vIndex = Mathf.Floor(index / uvAnimationTileX); // build offset Vector2 offset = new Vector2 (uIndex * size.x , 1.0f - size.y - vIndex * size.y); GetComponent().material.SetTextureOffset ("_MainTex", offset); GetComponent().material.SetTextureScale ("_MainTex", size); // growing transform.localScale += new Vector3(speedGrowing,speedGrowing,speedGrowing) * Time.deltaTime ; GetComponent().enabled = true; } else{ effectEnd = true; GetComponent().enabled = false; end = true; if (soundEffect){ if (soundEffect.isPlaying){ end = false; } } if (addLightEffect && end){ if (gameObject.GetComponent().intensity>0){ end = false; } } if (end){ Destroy(gameObject); } } // Light effect if (addLightEffect && lightFadeSpeed!=0){ gameObject.GetComponent().intensity -= lightFadeSpeed*Time.deltaTime; } } #endregion #region private methods /// /// Creates the particle. /// void CreateParticle(){ mesh = gameObject.AddComponent().mesh; meshRender = gameObject.AddComponent(); 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) }; mesh.triangles = new int[] {0,1,2, 2,3,0 }; mesh.uv = new Vector2[] { new Vector2 (0f, 0f), new Vector2 (0f, 1f), new Vector2 (1f, 1f), new Vector2(1f,0f)}; meshRender.castShadows = false; meshRender.receiveShadows = false; mesh.RecalculateNormals(); GetComponent().material= spriteSheetMaterial; } /// /// Camera_s the billboarding mode. /// void Camera_BillboardingMode(){ Vector3 lookAtVector = mainCam.transform.position - transform.position; switch (billboarding){ case CameraFacingMode.BillBoard: transform.LookAt( lookAtVector); break; case CameraFacingMode.Horizontal: lookAtVector.x = lookAtVector.z =0 ; transform.LookAt(mainCam.transform.position- lookAtVector); break; case CameraFacingMode.Vertical: lookAtVector.y=lookAtVector.z =0; transform.LookAt(mainCam.transform.position- lookAtVector); break; } transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y,randomZAngle); } #endregion }