BeaconAnimator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace CompassNavigatorPro
  4. {
  5. public class BeaconAnimator : MonoBehaviour
  6. {
  7. public float intensity = 5f;
  8. public float duration;
  9. public Color tintColor;
  10. float startingTime;
  11. Material mat;
  12. Color fullyTransparentColor, originalColor;
  13. // Use this for initialization
  14. void Awake ()
  15. {
  16. mat = GetComponent<Renderer>().material;
  17. fullyTransparentColor = new Color(0,0,0,0);
  18. duration = 1f;
  19. }
  20. void Start() {
  21. startingTime = Time.time;
  22. originalColor = mat.color * tintColor * intensity;
  23. mat.SetColor ("_EmissionColor", tintColor);
  24. UpdateColor ();
  25. }
  26. void OnDisable() {
  27. DestroyBeacon();
  28. }
  29. // Update is called once per frame
  30. void Update ()
  31. {
  32. mat.mainTextureOffset = new Vector2(Time.time * -0.25f, Time.time * -0.25f);
  33. mat.SetTextureOffset ("_EmissionMap", new Vector2 (Time.time * -0.15f, Time.time * -0.2f));
  34. UpdateColor();
  35. }
  36. void UpdateColor() {
  37. float elapsed = duration<=0 ? 1f: Mathf.Clamp01( (Time.time - startingTime) / duration );
  38. if (elapsed>=1f) {
  39. DestroyBeacon();
  40. return;
  41. }
  42. float t = Ease(elapsed);
  43. mat.color = Color.Lerp(fullyTransparentColor, originalColor, t);
  44. }
  45. float Ease(float t) {
  46. return Mathf.Sin (t * Mathf.PI);
  47. }
  48. void DestroyBeacon() {
  49. if (mat!=null) {
  50. DestroyImmediate(mat);
  51. mat = null;
  52. }
  53. if (Application.isPlaying) Destroy(gameObject);
  54. }
  55. }
  56. }