F3DPulsewave.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System.Collections;
  3. public class F3DPulsewave : MonoBehaviour
  4. {
  5. public float FadeOutDelay; // Color fade delay in ms
  6. public float FadeOutTime; // Color fade speed
  7. public float ScaleTime; // Scaling speed
  8. public Vector3 ScaleSize; // The size wave will be scaled to
  9. public bool DebugLoop; // Constant looping flag mainly used in preview scene
  10. new Transform transform; // Cached transform
  11. MeshRenderer meshRenderer; // Cached mesh renderer
  12. int timerID = -1; // Timer reference
  13. bool isFadeOut; // Fading flag
  14. bool isEnabled; // Enabled flag
  15. Color defaultColor; // Default wave color
  16. Color color; // Current wave color
  17. int tintColorRef; // Shader property reference
  18. void Awake()
  19. {
  20. // Cache components
  21. transform = GetComponent<Transform>();
  22. meshRenderer = GetComponent<MeshRenderer>();
  23. // Get shader property
  24. tintColorRef = Shader.PropertyToID("_TintColor");
  25. // Store default color
  26. defaultColor = meshRenderer.material.GetColor(tintColorRef);
  27. }
  28. void Start()
  29. {
  30. // Fire up manually
  31. if (DebugLoop)
  32. OnSpawned();
  33. }
  34. // OnSpawned called by pool manager
  35. void OnSpawned()
  36. {
  37. // Set scale to zero
  38. transform.localScale = new Vector3(0f, 0f, 0f);
  39. // Set required flags and set delayed fade flag using timer
  40. isEnabled = true;
  41. isFadeOut = false;
  42. timerID = F3DTime.time.AddTimer(FadeOutDelay, OnFadeOut);
  43. // Reset default color
  44. meshRenderer.material.SetColor(tintColorRef, defaultColor);
  45. color = defaultColor;
  46. }
  47. // OnDespawned called by pool manager
  48. void OnDespawned()
  49. {
  50. // Remove timer
  51. if (timerID >= 0)
  52. {
  53. F3DTime.time.RemoveTimer(timerID);
  54. timerID = -1;
  55. }
  56. }
  57. // Toggle fading state
  58. void OnFadeOut()
  59. {
  60. isFadeOut = true;
  61. }
  62. void Update ()
  63. {
  64. // Enabled state
  65. if (isEnabled)
  66. {
  67. // Scale the wave
  68. transform.localScale = Vector3.Lerp(transform.localScale, ScaleSize, Time.deltaTime * ScaleTime);
  69. // Check the fading state
  70. if (isFadeOut)
  71. {
  72. // Lerp color and update the shader
  73. color = Color.Lerp(color, new Color(0, 0, 0, -0.1f), Time.deltaTime * FadeOutTime);
  74. meshRenderer.material.SetColor(tintColorRef, color);
  75. // Make sure alpha value is not overshooting
  76. if (color.a <= 0f)
  77. {
  78. // Disable the update loop
  79. isEnabled = false;
  80. // Reset the sequence in case of the debug loop flag
  81. if(DebugLoop)
  82. {
  83. OnDespawned();
  84. OnSpawned();
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }