Files
2025-01-25 04:38:09 +08:00

110 lines
2.8 KiB
C#

using System;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using UnityEngine.Gonbest.MagicCube;
using UnityEngine.Profiling;
namespace Thousandto.Core.Base
{
/// <summary>
/// 封装Unity的Profiler.
/// </summary>
public class FProfiler
{
/// <summary>
/// 开始采样
/// </summary>
/// <param name="name"></param>
[Conditional("ENABLE_PROFILER")]
public static void Begin(string name)
{
if (!Thread.CurrentThread.IsBackground)
{
Profiler.BeginSample(name);
}
}
[Conditional("ENABLE_PROFILER")]
public static void BeginObj(object obj,string name)
{
if (obj != null)
{
BeginType(obj.GetType(), name);
}
else
{
Begin(name);
}
}
[Conditional("ENABLE_PROFILER")]
public static void BeginType(Type type,string name)
{
if (type != null)
{
Begin(type.Name + name);
}
else
{
Begin(name);
}
}
/// <summary>
/// 结束采样
/// </summary>
[Conditional("ENABLE_PROFILER")]
public static void End()
{
if (!Thread.CurrentThread.IsBackground)
{
Profiler.EndSample();
}
}
/// <summary>
/// 当前信息
/// </summary>
/// <returns></returns>
public static string Infomation()
{
var sb = StringUtils.NewStringBuilder;
sb.AppendLine("Profiler:");
#if ENABLE_PROFILER
sb.AppendFormat("MonoHeapSize:{0}", Profiler.GetMonoHeapSize() / 1048576);
sb.AppendLine();
sb.AppendFormat("GetMonoUsedSize:{0}", Profiler.GetMonoUsedSize() / 1048576);
sb.AppendLine();
sb.AppendFormat("GetTotalAllocatedMemory:{0}", Profiler.GetTotalAllocatedMemory() / 1048576);
sb.AppendLine();
//sb.AppendFormat("GetRuntimeMemorySize:{0}", Profiler.GetRuntimeMemorySize(null) / 1048576);
//sb.AppendLine();
sb.AppendFormat("GetTotalUnusedReservedMemory:{0}", Profiler.GetTotalUnusedReservedMemory() / 1048576);
sb.AppendLine();
#endif
return sb.ToString();
}
/// <summary>
/// 获取某个对象的内存
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int GetObjectRuntimeMemory(UnityEngine.Object obj)
{
#if ENABLE_PROFILER
return Profiler.GetRuntimeMemorySize(obj);
#else
return 0;
#endif
}
}
}