BenchmarkController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 System;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace Live2D.Cubism.Samples.AsyncBenchmark
  11. {
  12. /// <summary>
  13. /// Automatically adjust the fps to the set value.
  14. /// </summary>
  15. public sealed class BenchmarkController : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// Interval time before the model can spawn.
  19. /// </summary>
  20. public readonly float SpawnIntervalTimeSecond = 1.0f;
  21. /// <summary>
  22. /// Target frame rate value.
  23. /// </summary>
  24. [SerializeField]
  25. public int TargetFrameRate = 60;
  26. /// <summary>
  27. /// UI for displaying <see cref="ElapsedTime"/> values.
  28. /// </summary>
  29. [SerializeField]
  30. public Text ReachedElapsedTimeUi = null;
  31. /// <summary>
  32. /// UI to display the number of instances of the model when the target frame rate is finally reached.
  33. /// </summary>
  34. [SerializeField]
  35. public Text InstancesCountUi = null;
  36. /// <summary>
  37. /// Save the maximum frame rate.
  38. /// </summary>
  39. private float HighestRecordedFrameRate { get; set; }
  40. /// <summary>
  41. /// Whether the model is spawnable or not.
  42. /// </summary>
  43. private bool CanModelSpawn { get; set; }
  44. /// <summary>
  45. /// Add delta time.
  46. /// </summary>
  47. private float SpawnTimeCount { get; set; }
  48. /// <summary>
  49. /// Time elapsed since <see cref="CanModelSpawn"/> was set to false.
  50. /// </summary>
  51. private float ElapsedTime { get; set; }
  52. /// <summary>
  53. /// <see cref="AsyncBenchmark.FpsCounter"/> Component.
  54. /// </summary>
  55. private FpsCounter FpsCounter { get; set; }
  56. /// <summary>
  57. /// <see cref="AsyncBenchmark.ModelSpawner"/> Conponent.
  58. /// </summary>
  59. private ModelSpawner ModelSpawner { get; set; }
  60. /// <summary>
  61. /// Called by Unity. Setting vsync and target frame rate.
  62. /// </summary>
  63. private void Awake()
  64. {
  65. // Setting vsync and targetFrameRate.
  66. QualitySettings.vSyncCount = 0;
  67. Application.targetFrameRate = TargetFrameRate + 1;
  68. // Getting the component.
  69. FpsCounter = GetComponent<FpsCounter>();
  70. ModelSpawner = GetComponent<ModelSpawner>();
  71. }
  72. /// <summary>
  73. /// Called by Unity. Record the maximum frame rate and manage model spawning.
  74. /// </summary>
  75. private void Update()
  76. {
  77. RecordFrameRate();
  78. ManageSpawn();
  79. }
  80. /// <summary>
  81. /// Records the maximum frame rate within a given time period.
  82. /// </summary>
  83. private void RecordFrameRate()
  84. {
  85. /// Get value from <see cref="FpsCounter"/> Component.
  86. var fps = FpsCounter.Fps;
  87. // Compare the measured value with the maximum value so far.
  88. HighestRecordedFrameRate = fps >= HighestRecordedFrameRate
  89. ? fps
  90. : HighestRecordedFrameRate;
  91. // Whether you have time to make a decision.
  92. if (SpawnTimeCount < SpawnIntervalTimeSecond)
  93. {
  94. SpawnTimeCount += Time.deltaTime;
  95. return;
  96. }
  97. // If the model is not ready to spawn, add the elapsed time.
  98. if (!CanModelSpawn && (ModelSpawner.InstancesCount != 0))
  99. {
  100. ElapsedTime += SpawnIntervalTimeSecond;
  101. // Combine strings and display them in UI.
  102. var elapsedTimeString = TimeConversion(Mathf.FloorToInt(ElapsedTime));
  103. ReachedElapsedTimeUi.text = string.Format(" Reached Time:{0}", elapsedTimeString);
  104. }
  105. // Whether the recorded frame rate has reached the target frame rate.
  106. CanModelSpawn = TargetFrameRate <= HighestRecordedFrameRate;
  107. // Reset variables and properties.
  108. SpawnTimeCount = 0.0f;
  109. HighestRecordedFrameRate = 0.0f;
  110. }
  111. /// <summary>
  112. /// Convert seconds to "hours:minutes:seconds".
  113. /// </summary>
  114. /// <param name="second">Number of seconds it conversion source.</param>
  115. /// <returns>String type converted to "hours:minutes:seconds" notation.</returns>
  116. private string TimeConversion(int second)
  117. {
  118. // Generate TimeSpan structure type.
  119. var timeSpan = new TimeSpan(0, 0, second);
  120. return timeSpan.ToString();
  121. }
  122. /// <summary>
  123. /// Managing model spawn.
  124. /// </summary>
  125. private void ManageSpawn()
  126. {
  127. if (SpawnTimeCount < SpawnIntervalTimeSecond)
  128. {
  129. return;
  130. }
  131. // When the model can spawn
  132. if (CanModelSpawn)
  133. {
  134. // Spawn the model.
  135. ModelSpawner.IncreaseInstances();
  136. // Reset variable.
  137. ElapsedTime = 0;
  138. }
  139. // When the model can't spawn
  140. else
  141. {
  142. /// Get Instances Count from <see cref="ModelSpawner"/> Component and update UI.
  143. var instancesCount = ModelSpawner.InstancesCount;
  144. InstancesCountUi.text = string.Format(" Reached Model Count:{0}", instancesCount.ToString());
  145. }
  146. }
  147. }
  148. }