using System; using System.Diagnostics; using System.Threading; using UnityEngine; using UnityEngine.Gonbest.MagicCube; using UnityEngine.Profiling; namespace Thousandto.Core.Base { /// /// 封装Unity的Profiler. /// public class FProfiler { /// /// 开始采样 /// /// [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); } } /// /// 结束采样 /// [Conditional("ENABLE_PROFILER")] public static void End() { if (!Thread.CurrentThread.IsBackground) { Profiler.EndSample(); } } /// /// 当前信息 /// /// 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(); } /// /// 获取某个对象的内存 /// /// /// public static int GetObjectRuntimeMemory(UnityEngine.Object obj) { #if ENABLE_PROFILER return Profiler.GetRuntimeMemorySize(obj); #else return 0; #endif } } }