2024-09-09 16:18:53 +08:00
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Scripting;
|
2024-11-21 14:37:48 +08:00
|
|
|
using System.IO;
|
|
|
|
|
|
2024-09-09 16:18:53 +08:00
|
|
|
|
2024-11-04 15:27:36 +08:00
|
|
|
#if PLATFORM_WEIXINMINIGAME || PLATFORM_WEBGL || UNITY_EDITOR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if ENABLE_WX_PERF_FEATURE
|
2024-09-09 16:18:53 +08:00
|
|
|
namespace WXSDKPerf
|
|
|
|
|
{
|
|
|
|
|
[Preserve]
|
|
|
|
|
[ComVisible(false)]
|
|
|
|
|
public class WXPerfEngine
|
|
|
|
|
{
|
2024-11-21 14:37:48 +08:00
|
|
|
#if !UNITY_EDITOR
|
2024-09-09 16:18:53 +08:00
|
|
|
static WXPerfEngine_Implementation m_PerfEngineImplementation = null;
|
2024-11-21 14:37:48 +08:00
|
|
|
#endif
|
|
|
|
|
|
2024-09-09 16:18:53 +08:00
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
|
|
|
public static void StartWXPerfEngine()
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
return;
|
2024-11-04 15:27:36 +08:00
|
|
|
#else
|
2024-09-09 16:18:53 +08:00
|
|
|
m_PerfEngineImplementation = new WXPerfEngine_Implementation();
|
|
|
|
|
m_PerfEngineImplementation.StartPerfEngine();
|
2024-11-04 15:27:36 +08:00
|
|
|
#endif
|
2024-09-09 16:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Annotation(string InAnnotationString)
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
return;
|
2024-11-04 15:27:36 +08:00
|
|
|
#else
|
2024-09-09 16:18:53 +08:00
|
|
|
if (m_PerfEngineImplementation == null)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.LogError("Annotation: Invalid m_PerfEngineImplementation! ");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 14:37:48 +08:00
|
|
|
if (InAnnotationString.Contains("CaptureUnityMemorySnapshot"))
|
|
|
|
|
{
|
|
|
|
|
TakeAndUploadUnityMemorySnapshot();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 16:18:53 +08:00
|
|
|
m_PerfEngineImplementation.Annotation(InAnnotationString);
|
2024-11-04 15:27:36 +08:00
|
|
|
#endif
|
2024-11-21 14:37:48 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void TakeAndUploadUnityMemorySnapshot()
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
return;
|
|
|
|
|
#else
|
|
|
|
|
DateTime timestamp = DateTime.Now;
|
|
|
|
|
var dateString = timestamp.ToLocalTime().ToString("yyyy-MM-dd_HH-mm-ss", System.Globalization.CultureInfo.InvariantCulture);
|
|
|
|
|
var snapshotFileName = $"{dateString}.snap";
|
|
|
|
|
|
|
|
|
|
#if UNITY_2018_3_OR_NEWER && !UNITY_2022_2_OR_NEWER
|
|
|
|
|
UnityEngine.Profiling.Memory.Experimental.MemoryProfiler.TakeSnapshot(Path.Combine(Application.persistentDataPath, snapshotFileName),
|
|
|
|
|
WXPerfEngine_Implementation.CaptureSnapshotCallback, (UnityEngine.Profiling.Memory.Experimental.CaptureFlags)31);
|
|
|
|
|
|
|
|
|
|
#elif UNITY_2022_2_OR_NEWER
|
|
|
|
|
Unity.Profiling.Memory.MemoryProfiler.TakeSnapshot(Path.Combine(Application.persistentDataPath, snapshotFileName),
|
|
|
|
|
WXPerfEngine_Implementation.CaptureSnapshotCallback, (Unity.Profiling.Memory.CaptureFlags)31);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetLuaState(IntPtr L)
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
return;
|
|
|
|
|
#else
|
|
|
|
|
if (m_PerfEngineImplementation == null)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Debug.LogError("SetLuaState: WXPerfEngine Not Started yet! Please Call WXSDKPerf.StartWXPerfEngine first! ");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_PerfEngineImplementation.SetLuaState(L);
|
|
|
|
|
#endif
|
2024-09-09 16:18:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-11-04 15:27:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
2024-09-09 16:18:53 +08:00
|
|
|
#endif // ENABLE_WX_PERF_FEATURE
|