阶段性打包
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using System.Collections;
|
||||
using SepCore.Event;
|
||||
using GameFramework.Event;
|
||||
using SepCore.EnemyManager;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using Unity.Profiling;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace SepCore.UI
|
||||
@@ -39,6 +41,14 @@ namespace SepCore.UI
|
||||
[SerializeField] private GameObject _movePromptRoot;
|
||||
[SerializeField] private TMP_Text _movePromptText;
|
||||
|
||||
[SerializeField] private TMP_Text _cpuTimeCostText;
|
||||
|
||||
[SerializeField] private float _performanceStatsRefreshInterval = 0.2f;
|
||||
|
||||
private ProfilerRecorder _cpuTimeRecorder;
|
||||
private float _performanceStatsRefreshTimer;
|
||||
private Coroutine _performanceStatsCoroutine;
|
||||
|
||||
public void RefreshUI(HudContext hudContext)
|
||||
{
|
||||
RefreshMovePrompt(hudContext?.MovePrompt);
|
||||
@@ -73,6 +83,41 @@ namespace SepCore.UI
|
||||
_enemy = GameEntry.EnemyManager;
|
||||
}
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
StartPerformanceStatsCollection();
|
||||
}
|
||||
|
||||
protected override void OnPause()
|
||||
{
|
||||
StopPerformanceStatsCoroutine();
|
||||
base.OnPause();
|
||||
}
|
||||
|
||||
protected override void OnResume()
|
||||
{
|
||||
base.OnResume();
|
||||
|
||||
StartPerformanceStatsCoroutine();
|
||||
}
|
||||
|
||||
private void StartPerformanceStatsCollection()
|
||||
{
|
||||
DisposePerformanceStatsRecorders();
|
||||
_cpuTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", 15);
|
||||
|
||||
StartPerformanceStatsCoroutine();
|
||||
}
|
||||
|
||||
private void StartPerformanceStatsCoroutine()
|
||||
{
|
||||
StopPerformanceStatsCoroutine();
|
||||
_performanceStatsRefreshTimer = 0f;
|
||||
_performanceStatsCoroutine = StartCoroutine(RefreshPerformanceStatsAtEndOfFrame());
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(PlayerLevelUpEventArgs.EventId, PlayerLevelUp);
|
||||
@@ -81,9 +126,29 @@ namespace SepCore.UI
|
||||
GameEntry.Event.Unsubscribe(PlayerCoinChangeEventArgs.EventId, PlayerCoinChange);
|
||||
GameEntry.Event.Unsubscribe(PlayerLevelUpEventArgs.EventId, PlayerLevelUp);
|
||||
|
||||
StopPerformanceStatsCoroutine();
|
||||
DisposePerformanceStatsRecorders();
|
||||
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
|
||||
private void StopPerformanceStatsCoroutine()
|
||||
{
|
||||
if (_performanceStatsCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_performanceStatsCoroutine);
|
||||
_performanceStatsCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposePerformanceStatsRecorders()
|
||||
{
|
||||
if (_cpuTimeRecorder.Valid)
|
||||
{
|
||||
_cpuTimeRecorder.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
||||
{
|
||||
base.OnUpdate(elapseSeconds, realElapseSeconds);
|
||||
@@ -100,6 +165,51 @@ namespace SepCore.UI
|
||||
|
||||
#endregion
|
||||
|
||||
private IEnumerator RefreshPerformanceStatsAtEndOfFrame()
|
||||
{
|
||||
WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
|
||||
while (true)
|
||||
{
|
||||
yield return waitForEndOfFrame;
|
||||
|
||||
_performanceStatsRefreshTimer -= Time.unscaledDeltaTime;
|
||||
if (_performanceStatsRefreshTimer > 0f) continue;
|
||||
|
||||
_performanceStatsRefreshTimer = Mathf.Max(0.05f, _performanceStatsRefreshInterval);
|
||||
RefreshPerformanceStats();
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshPerformanceStats()
|
||||
{
|
||||
if (_cpuTimeCostText == null) return;
|
||||
|
||||
bool hasCpuFrame = TryGetLastValue(_cpuTimeRecorder, out long cpuFrameNanoseconds);
|
||||
|
||||
if (!hasCpuFrame)
|
||||
{
|
||||
_cpuTimeCostText.text =
|
||||
"CPU Frame: -- ms\nSimulation: -- ms\nJob Wait: -- ms";
|
||||
return;
|
||||
}
|
||||
|
||||
float cpuFrameMilliseconds = cpuFrameNanoseconds / 1_000_000f;
|
||||
|
||||
_cpuTimeCostText.SetText("CPU Frame: {0:2} ms",cpuFrameMilliseconds);
|
||||
}
|
||||
|
||||
private static bool TryGetLastValue(ProfilerRecorder recorder, out long value)
|
||||
{
|
||||
if (recorder.Valid && recorder.Count > 0)
|
||||
{
|
||||
value = recorder.LastValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void PlayerHpChange(object sender, GameEventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user