SpriteTransparencyOverLap.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using ExtensionMethods;
  3. using UnityEngine;
  4. [RequireComponent(typeof(SpriteRenderer))]
  5. public class SpriteTransparencyOverLap : BaseBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this._render = base.GetComponent<SpriteRenderer>();
  10. this._bounds = this._render.bounds;
  11. this._rect = new Rect(this._bounds.min, this._bounds.size);
  12. this._color = this._render.color;
  13. }
  14. private void Update()
  15. {
  16. Vector3 position = R.Camera.Camera.WorldToViewportPoint(R.Player.Transform.position);
  17. bool flag = this._bounds.IntersectRay(R.Camera.Camera.ViewportPointToRay(position));
  18. if ((flag && this._color.a > this.finalAlpha) || (!flag && this._color.a < 1f))
  19. {
  20. float num = (1f - this.finalAlpha) / 0.5f;
  21. float num2 = this._color.a + (float)((!flag) ? 1 : -1) * num * Time.deltaTime;
  22. num2 = Mathf.Clamp(num2, this.finalAlpha, 1f);
  23. if (Math.Abs(num2 - this._color.a) > 1.401298E-45f)
  24. {
  25. this._color = this._color.SetAlpha(num2);
  26. this._render.color = this._color;
  27. }
  28. }
  29. }
  30. private SpriteRenderer _render;
  31. private Bounds _bounds;
  32. private Rect _rect;
  33. private Color _color;
  34. public float finalAlpha = 0.5f;
  35. }