/** * Copyright(c) Live2D Inc. All rights reserved. * * Use of this source code is governed by the Live2D Open Software license * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html. */ using System; using UnityEngine; using UnityEngine.UI; namespace Live2D.Cubism.Samples.AsyncBenchmark { /// /// Automatically adjust the fps to the set value. /// public sealed class BenchmarkController : MonoBehaviour { /// /// Interval time before the model can spawn. /// public readonly float SpawnIntervalTimeSecond = 1.0f; /// /// Target frame rate value. /// [SerializeField] public int TargetFrameRate = 60; /// /// UI for displaying values. /// [SerializeField] public Text ReachedElapsedTimeUi = null; /// /// UI to display the number of instances of the model when the target frame rate is finally reached. /// [SerializeField] public Text InstancesCountUi = null; /// /// Save the maximum frame rate. /// private float HighestRecordedFrameRate { get; set; } /// /// Whether the model is spawnable or not. /// private bool CanModelSpawn { get; set; } /// /// Add delta time. /// private float SpawnTimeCount { get; set; } /// /// Time elapsed since was set to false. /// private float ElapsedTime { get; set; } /// /// Component. /// private FpsCounter FpsCounter { get; set; } /// /// Conponent. /// private ModelSpawner ModelSpawner { get; set; } /// /// Called by Unity. Setting vsync and target frame rate. /// private void Awake() { // Setting vsync and targetFrameRate. QualitySettings.vSyncCount = 0; Application.targetFrameRate = TargetFrameRate + 1; // Getting the component. FpsCounter = GetComponent(); ModelSpawner = GetComponent(); } /// /// Called by Unity. Record the maximum frame rate and manage model spawning. /// private void Update() { RecordFrameRate(); ManageSpawn(); } /// /// Records the maximum frame rate within a given time period. /// private void RecordFrameRate() { /// Get value from Component. var fps = FpsCounter.Fps; // Compare the measured value with the maximum value so far. HighestRecordedFrameRate = fps >= HighestRecordedFrameRate ? fps : HighestRecordedFrameRate; // Whether you have time to make a decision. if (SpawnTimeCount < SpawnIntervalTimeSecond) { SpawnTimeCount += Time.deltaTime; return; } // If the model is not ready to spawn, add the elapsed time. if (!CanModelSpawn && (ModelSpawner.InstancesCount != 0)) { ElapsedTime += SpawnIntervalTimeSecond; // Combine strings and display them in UI. var elapsedTimeString = TimeConversion(Mathf.FloorToInt(ElapsedTime)); ReachedElapsedTimeUi.text = string.Format(" Reached Time:{0}", elapsedTimeString); } // Whether the recorded frame rate has reached the target frame rate. CanModelSpawn = TargetFrameRate <= HighestRecordedFrameRate; // Reset variables and properties. SpawnTimeCount = 0.0f; HighestRecordedFrameRate = 0.0f; } /// /// Convert seconds to "hours:minutes:seconds". /// /// Number of seconds it conversion source. /// String type converted to "hours:minutes:seconds" notation. private string TimeConversion(int second) { // Generate TimeSpan structure type. var timeSpan = new TimeSpan(0, 0, second); return timeSpan.ToString(); } /// /// Managing model spawn. /// private void ManageSpawn() { if (SpawnTimeCount < SpawnIntervalTimeSecond) { return; } // When the model can spawn if (CanModelSpawn) { // Spawn the model. ModelSpawner.IncreaseInstances(); // Reset variable. ElapsedTime = 0; } // When the model can't spawn else { /// Get Instances Count from Component and update UI. var instancesCount = ModelSpawner.InstancesCount; InstancesCountUi.text = string.Format(" Reached Model Count:{0}", instancesCount.ToString()); } } } }