一.Unity定时操作不要在用While循环了,使用异步async
public async Task ScheduleTask()
{
isBool = false;
await Task.Delay(mTime);
isBool = true;
Debug.Log
}
循环定时调用:
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} 已过去了");
}
二.计时
计时的三种方式
1.Time.time
伪代码:
float t=Time.time;
Test();//调用耗时操作
Debug.Log($"耗时:{Time.time-t}");
2.Stopwatch(引用库System.Diagnostics)
伪代码: Stopwatch stopwatch= new Stopwatch();
stopwatch.Start();
Test();//调用耗时操作
stopwatch.Stop();
Debug.Log($"耗时:{stopwatch.ElapsedMilliseconds}");
3.Profiler(UnityEngine.Profiling)
Profiler.BeginSample( "TestList" );
Test();//调用耗时操作
Profiler.EndSample();
在Profiler中查看TestList的耗时