[Unity优化系列]“CustomSampler”,可以测量任意代码块的GC Alloc和执行时间

[Unity优化系列]“CustomSampler”,可以测量任意代码块的GC Alloc和执行时间,并在Profiler上显示

示例

using UnityEngine;
using UnityEngine.Profiling;

public class Example : MonoBehaviour
{
    private void Start()
    {
        var sampler = CustomSampler.Create( "Pokemon" );
        sampler.Begin();

        for ( int i = 0; i < 10000; i++ )
        {
            Debug.Log( "啊啊" );
        }

        sampler.End();
    }
}
  • 通过使用CustomSampler,可以测量任何代码或GC Alloc的执行时间并在Profiler中显示它。
  • 与 Profiler.BeginSample 类似,但开销比Profiler.BeginSample少。
posted @ 2021-08-17 14:30  黑羽青衣  阅读(207)  评论(0编辑  收藏  举报