DynamicUpdateRate.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Samples.FineControl
  6. {
  7. /// <summary>
  8. /// Demonstrates how to save some performance by updating Animancer at a lower frequency
  9. /// when the character is far away from the camera.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// <strong>Sample:</strong>
  14. /// <see href="https://kybernetik.com.au/animancer/docs/samples/fine-control/update-rate">
  15. /// Update Rate</see>
  16. /// </remarks>
  17. ///
  18. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.FineControl/DynamicUpdateRate
  19. ///
  20. [AddComponentMenu(Strings.SamplesMenuPrefix + "Fine Control - Dynamic Update Rate")]
  21. [AnimancerHelpUrl(typeof(DynamicUpdateRate))]
  22. public class DynamicUpdateRate : MonoBehaviour
  23. {
  24. /************************************************************************************************************************/
  25. [SerializeField] private LowUpdateRate _LowUpdateRate;
  26. [SerializeField] private TextMesh _TextMesh;
  27. [SerializeField, Meters] private float _SlowUpdateDistance = 5;
  28. private Transform _Camera;
  29. /************************************************************************************************************************/
  30. protected virtual void Awake()
  31. {
  32. // Finding the Camera.main is a slow operation so we don't want to repeat it every update.
  33. _Camera = Camera.main.transform;
  34. }
  35. /************************************************************************************************************************/
  36. protected virtual void Update()
  37. {
  38. // Compare the squared distance to the camera with the squared threshold.
  39. // This is more efficient than calculating the distance because it avoids the square root calculation.
  40. Vector3 offset = _Camera.position - transform.position;
  41. float squaredDistance = offset.sqrMagnitude;
  42. // Low update rate enabled = true if the distance is further away.
  43. // Low update rate enabled = false if the distance is closer.
  44. float squaredSlowUpdateDistance = _SlowUpdateDistance * _SlowUpdateDistance;
  45. _LowUpdateRate.enabled = squaredDistance > squaredSlowUpdateDistance;
  46. // For the sake of this sample, use a TextMesh to show the current details.
  47. float distance = Mathf.Sqrt(squaredDistance);
  48. string updating = _LowUpdateRate.enabled ? "Slowly" : "Normally";
  49. _TextMesh.text = $"Distance {distance:0.00}\nUpdating {updating}\n\nDynamic Rate";
  50. }
  51. /************************************************************************************************************************/
  52. }
  53. }