StainBehaviour.cs 765 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class StainBehaviour : MonoBehaviour
  6. {
  7. const float StainLifetime = 2.0f;
  8. private float Lifetime = 0f;
  9. private Image StainImage;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. StainImage = gameObject.GetComponent<Image>();
  14. Debug.Assert(StainImage != null);
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. if (Lifetime > StainLifetime)
  20. {
  21. Destroy(gameObject);
  22. }
  23. else
  24. {
  25. StainImage.color = new Color(1f, 1f, 1f, (StainLifetime - Lifetime) / StainLifetime);
  26. Lifetime += Time.deltaTime;
  27. }
  28. }
  29. }