CriticalTextBehavior.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using JetBrains.Annotations;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. public class CriticalTextBehavior : MonoBehaviour
  7. {
  8. private const float Lifetime = 1.0f;
  9. private const float FadeinThreshold = 0.1f;
  10. private float Age = 0.0f;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. gameObject.GetComponent<TextMeshProUGUI>().color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
  15. gameObject.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 1f);
  16. gameObject.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 1f);
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. Age += Time.deltaTime;
  22. if (Age > Lifetime)
  23. {
  24. Destroy(gameObject);
  25. }
  26. else
  27. {
  28. float currentAlpha = (Age < FadeinThreshold) ? Mathf.InverseLerp(0.0f, FadeinThreshold, Age) : Mathf.InverseLerp(Lifetime, FadeinThreshold, Age);
  29. gameObject.GetComponent<TextMeshProUGUI>().color = new Color(1.0f, 1.0f, 1.0f, currentAlpha);
  30. float scale = Mathf.Lerp(0.8f, 1.0f, currentAlpha);
  31. gameObject.transform.localScale = new Vector3(scale, scale, scale);
  32. float currentPosAlpha = Mathf.InverseLerp(0.0f, Lifetime, Age);
  33. }
  34. }
  35. }