FpsCounter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace Live2D.Cubism.Samples.AsyncBenchmark
  10. {
  11. /// <summary>
  12. /// Measures Fps for on-screen display.
  13. /// </summary>
  14. public sealed class FpsCounter : MonoBehaviour
  15. {
  16. /// <summary>
  17. /// UI component representing current model count.
  18. /// </summary>
  19. [SerializeField]
  20. public Text FpsUi;
  21. /// <summary>
  22. /// Frame rate propertie to get from external sources.
  23. /// </summary>
  24. public float Fps { get; private set; }
  25. /// <summary>
  26. /// Time for FPS calculation.
  27. /// </summary>
  28. private float DeltaTime { get; set; }
  29. #region Unity Event Handling
  30. /// <summary>
  31. /// Called by Unity. Initializes fields.
  32. /// </summary>
  33. private void Update()
  34. {
  35. // Update delta time.
  36. DeltaTime += (Time.deltaTime - DeltaTime) * 0.1f;
  37. // Compute FPS and update UI.
  38. var fps = 1.0f / DeltaTime;
  39. // Save the value to the property.
  40. Fps = fps;
  41. FpsUi.text = string.Format("({0:0.} fps)", fps);
  42. }
  43. #endregion
  44. }
  45. }