扁平化优化更新

This commit is contained in:
2025-12-02 14:25:04 +08:00
parent 24f6da50ee
commit 716a23fad2
3 changed files with 40 additions and 53 deletions

View File

@@ -11,7 +11,7 @@ namespace Demo.Game
public class RootObject : ScriptableObject
{
protected override bool IsSelfEnableUpdate => false;
[Content] public List<ScriptableObject> UpdateChilds = new();
[Content] public Dictionary<Type, List<ScriptableObject>> UpdateChilds = new();
[Resources] public BasicAudioSystem audioSystem;
[Content] public GameController RootGameController;
@@ -85,24 +85,30 @@ namespace Demo.Game
{
if (RootGameController.IsMain)
{
if (RootGameController.IsAutoPlay)
return;
if (RootGameController.MainAudio.IsPlaying() == false)
return;
// 更新缓存时间
CurrentUpdateTickTimePoint = currentTime;
// 将距离当前更近的输入调整到更靠前的位置
InputCatch.Sort((x, y) => Mathf.Abs(x.TimePoint - currentTime).CompareTo(y.TimePoint - currentTime));
if (InputCatch.Count > 50)
InputCatch = InputCatch.GetRange(0, 50);
if (tickType == TickType.Start)
void foo()
{
InputCatch.Clear();
if (RootGameController.IsAutoPlay)
return;
if (RootGameController.MainAudio.IsPlaying() == false)
return;
// 更新缓存时间
CurrentUpdateTickTimePoint = currentTime;
// 将距离当前更近的输入调整到更靠前的位置
InputCatch.Sort((x, y) => Mathf.Abs(x.TimePoint - currentTime).CompareTo(y.TimePoint - currentTime));
if (InputCatch.Count > 50)
InputCatch = InputCatch.GetRange(0, 50);
if (tickType == TickType.Start)
{
InputCatch.Clear();
}
}
foo();
}
foreach (var item in UpdateChilds)
foreach (var (type, items) in UpdateChilds)
{
item.ScriptUpdate(currentTime, deltaTime, tickType);
using (Profiler.BeginZone($"{type.Name}.ScriptUpdate"))
foreach (var item in items)
item.ScriptUpdate(currentTime, deltaTime, tickType);
}
}
}

View File

@@ -531,51 +531,27 @@ namespace Demo
Update
}
private string s_ScriptUpdateZoneName = null;
private string s_UpdateTicksZoneName = null;
public string ScriptUpdateZoneName
{
get
{
if (s_ScriptUpdateZoneName == null)
s_ScriptUpdateZoneName = $"{m_ScriptType}.ScriptUpdate";
return s_ScriptUpdateZoneName;
}
}
public string UpdateTicksZoneName
{
get
{
if (s_UpdateTicksZoneName == null)
s_UpdateTicksZoneName = $"{m_ScriptType}.UpdateTicks";
return s_UpdateTicksZoneName;
}
}
[Content, SerializeField] private int ScriptUpdateCounter = 0;
public void ScriptUpdate(float currentTime, float deltaTime, TickType tickType)
{
if (IsScriptApply == false)
return;
using (Profiler.BeginZone(ScriptUpdateZoneName))
if (tickType == TickType.Reset)
{
if (tickType == TickType.Reset)
ResetEnterGameStatus();
foreach (var child in Childs)
{
ResetEnterGameStatus();
foreach (var child in Childs)
{
child.ScriptUpdate(currentTime, deltaTime, tickType);
}
}
else
{
// UpdateTicks
if (ScriptUpdateCounter % UpdatePerFrame == 0)
UpdateTicks(currentTime, deltaTime, tickType);
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 0;
child.ScriptUpdate(currentTime, deltaTime, tickType);
}
}
else
{
// UpdateTicks
if (ScriptUpdateCounter % UpdatePerFrame == 0)
UpdateTicks(currentTime, deltaTime, tickType);
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 0;
}
}
protected virtual void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
@@ -637,7 +613,13 @@ namespace Demo
// 统计更新能力
{
if (this.IsSelfEnableUpdate)
GetRoot().UpdateChilds.Add(this);
{
var type = this.GetType();
if (GetRoot().UpdateChilds.TryGetValue(type, out var scriptables))
scriptables.Add(this);
else
GetRoot().UpdateChilds[type] = new() { this };
}
}
IsScriptApply = true;
}
@@ -862,7 +844,6 @@ namespace Demo
{
base.UpdateTicks(currentTime, deltaTime, tickType);
// 存在严重的性能开销, 在解决之前将不会允许其快速自动更新
using (Profiler.BeginZone($"{nameof(TimelineScriptObject)}.{nameof(MyTimelineItem.ResizeOnTimeline)}"))
{
if (UIResizeOnTimelineCount > 0.1 || tickType != TickType.Update)
{