TrailFade.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. public class TrailFade : MonoBehaviour
  4. {
  5. private void OnEnable()
  6. {
  7. this.thisTrail.material.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, 1f));
  8. if (this.fadeInTime < 0.01f)
  9. {
  10. this.fadeInTime = 0.01f;
  11. }
  12. this.percent = this.timeElapsed / this.fadeInTime;
  13. }
  14. private void Update()
  15. {
  16. this.timeElapsed += Time.deltaTime;
  17. if (this.timeElapsed <= this.fadeInTime)
  18. {
  19. this.percent = this.timeElapsed / this.fadeInTime;
  20. this.thisTrail.material.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, this.percent));
  21. }
  22. if (this.timeElapsed > this.fadeInTime && this.timeElapsed < this.fadeInTime + this.stayTime)
  23. {
  24. this.thisTrail.material.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, 1f));
  25. }
  26. if (this.timeElapsed >= this.fadeInTime + this.stayTime && this.timeElapsed < this.fadeInTime + this.stayTime + this.fadeOutTime)
  27. {
  28. this.timeElapsedLast += Time.deltaTime;
  29. this.percent = 1f - this.timeElapsedLast / this.fadeOutTime;
  30. this.thisTrail.material.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, this.percent));
  31. }
  32. }
  33. private void OnDisable()
  34. {
  35. this.thisTrail.material.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, 0f));
  36. }
  37. private float timeElapsed;
  38. private float timeElapsedLast;
  39. private float percent;
  40. [SerializeField]
  41. public float fadeInTime = 0.1f;
  42. [SerializeField]
  43. public float stayTime = 1f;
  44. [SerializeField]
  45. public float fadeOutTime = 0.7f;
  46. [SerializeField]
  47. public TrailRenderer thisTrail;
  48. }