SFX_BeamScannerController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. // ReSharper disable once CheckNamespace
  5. namespace QFX.SFX
  6. {
  7. public class SFX_BeamScannerController : MonoBehaviour
  8. {
  9. public GameObject Scanner;
  10. public SFX_AnimationModule AppearAnimation;
  11. public bool IsDetectionEnabled;
  12. public Transform DetectionAnchor;
  13. public SFX_ObjectFinder ObjectFinder;
  14. public bool OverrideColor;
  15. [ColorUsageAttribute(true, true, 0f, 8f, 0.125f, 3f)]
  16. public Color NormalColor;
  17. [ColorUsageAttribute(true, true, 0f, 8f, 0.125f, 3f)]
  18. public Color DetectionColor;
  19. private Material _scannerMaterial;
  20. private float _startTime;
  21. private bool _isEnabled;
  22. private bool _wasDetected;
  23. private void Start()
  24. {
  25. _scannerMaterial = Scanner.GetComponent<Renderer>().material;
  26. var eval = AppearAnimation.Evaluate(0);
  27. if (_scannerMaterial != null)
  28. _scannerMaterial.SetFloat("_AppearProgress", eval);
  29. }
  30. private void OnEnable()
  31. {
  32. _isEnabled = true;
  33. _startTime = Time.time;
  34. if (OverrideColor && _scannerMaterial != null)
  35. {
  36. _scannerMaterial.SetColor("_TintColor", NormalColor);
  37. _scannerMaterial.SetColor("_DepthColor", NormalColor);
  38. }
  39. }
  40. private void OnDisable()
  41. {
  42. _isEnabled = false;
  43. }
  44. private void Update()
  45. {
  46. if (!_isEnabled)
  47. return;
  48. if (IsDetectionEnabled)
  49. {
  50. var colliders = ObjectFinder.FindObjects(DetectionAnchor.position);
  51. bool isObjectInRadius = colliders.Any();
  52. if (isObjectInRadius && !_wasDetected)
  53. {
  54. _wasDetected = true;
  55. }
  56. else if (!isObjectInRadius && _wasDetected)
  57. {
  58. _wasDetected = false;
  59. }
  60. if (OverrideColor && _scannerMaterial != null)
  61. {
  62. _scannerMaterial.SetColor("_TintColor", _wasDetected ? DetectionColor : NormalColor);
  63. _scannerMaterial.SetColor("_DepthColor", _wasDetected ? DetectionColor : NormalColor);
  64. }
  65. }
  66. var time = Time.time - _startTime;
  67. var appearValue = AppearAnimation.Evaluate(time);
  68. if (_scannerMaterial != null)
  69. _scannerMaterial.SetFloat("_AppearProgress", appearValue);
  70. }
  71. }
  72. }