Unity scheduled operations and timing

1. Don’t use While loop for timing operations in Unity, use asynchronous async

public async Task ScheduleTask()
{
isBool = false;
await Task.Delay(mTime);
isBool = true;
Debug.Log

}

Loop timing call:

void Start()
{
isBool = true;
}

// Update is called once per frame
async void Update()
{
if (isBool)
await ScheduleTask();
}

private bool isBool = false;
private readonly int mTime=5000;
public async Task ScheduleTask()
{
isBool = false;
await Task.Delay(mTime);
isBool = true;
Debug.Log($"{mTime} 已过去了");
}

2. Timing

Three ways of timing

1.Time.time

pseudocode:

        float t=Time.time;

        Test();//Call time-consuming operations

        Debug.Log($"Time consuming: {Time.time-t}");

2.Stopwatch (reference library System.Diagnostics)

Pseudocode: Stopwatch stopwatch= new Stopwatch();

        stopwatch.Start();

        Test();//Call time-consuming operations

        stopwatch.Stop();

        Debug.Log($"Time consuming: {stopwatch.ElapsedMilliseconds}");

3.Profiler(UnityEngine.Profiling)

       Profiler.BeginSample( "TestList" );

       Test();//Call time-consuming operations

       Profiler.EndSample();

      Check the time consumption of TestList in Profiler

Guess you like

Origin blog.csdn.net/qq_35385242/article/details/133344518