更新优化 扁平化+静态剪枝

This commit is contained in:
2025-12-02 11:34:12 +08:00
parent a8adb45952
commit 24f6da50ee
2 changed files with 57 additions and 57 deletions

View File

@@ -10,6 +10,8 @@ namespace Demo.Game
{
public class RootObject : ScriptableObject
{
protected override bool IsSelfEnableUpdate => false;
[Content] public List<ScriptableObject> UpdateChilds = new();
[Resources] public BasicAudioSystem audioSystem;
[Content] public GameController RootGameController;
@@ -33,6 +35,7 @@ namespace Demo.Game
Keyboard.current.onTextInput -= InputCatchChar;
}
yield return base.UnloadScript();
UpdateChilds.Clear();
}
public void EnableScript(string sourcePath, GameController parent)
@@ -82,27 +85,25 @@ namespace Demo.Game
{
if (RootGameController.IsMain)
{
void Foo()
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)
{
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();
}
InputCatch.Clear();
}
Foo();
}
base.UpdateTicks(currentTime, deltaTime, tickType);
foreach (var item in UpdateChilds)
{
item.ScriptUpdate(currentTime, deltaTime, tickType);
}
}
}
}

View File

@@ -211,10 +211,19 @@ namespace Demo
private bool isEnableScript = false;
public string ScriptName = "";
private string s_ScriptType = null;
public string m_ScriptType
{
get
{
if (s_ScriptType == null)
s_ScriptType = this.GetType().Name;
return s_ScriptType;
}
}
public ScriptableObject Parent;
public readonly List<ScriptableObject> Childs = new();
[Content, SerializeField] private List<ScriptableObject> UpdateChilds = new();
/// <summary>
/// 获取根脚本对象
@@ -324,7 +333,7 @@ namespace Demo
{
MyHierarchyItem = HierarchyWindow.instance.CreateRootItemEntryWithBinders(1)[0];
}
MyHierarchyItem.GetHierarchyItem().title = this.ScriptName + $"<{this.GetType()}>";
MyHierarchyItem.GetHierarchyItem().title = this.ScriptName + $"<{m_ScriptType}>";
MyHierarchyItem.GetHierarchyItem().target = this;
MyHierarchyItem.GetHierarchyItem().ButtonGameObject.GetComponent<Editor.UI.RightClick>().ScriptObjectMenu = OnHierarchyItemRightClick;
}
@@ -356,7 +365,6 @@ namespace Demo
public bool IsParseScript2Expr { get; private set; } = false;
protected virtual bool IsSelfEnableUpdate { get => true; }
public bool IsEnableUpdate { get; private set; } = true;
#region LoadSubScript
@@ -394,7 +402,6 @@ namespace Demo
// Add Child
Childs.Add(child);
UpdateChilds.Add(child);
// Load Child Script
ConventionUtility.StartCoroutine(child.ParseScript2Expr(file.LoadAsText()));
@@ -427,7 +434,6 @@ namespace Demo
// Add Child
Childs.Add(child);
UpdateChilds.Add(child);
return child;
}
@@ -525,20 +531,38 @@ 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;
if (gameObject.activeInHierarchy == false)
return;
using (Profiler.BeginZone($"{GetType().Name}.ScriptUpdate"))
using (Profiler.BeginZone(ScriptUpdateZoneName))
{
if (tickType == TickType.Reset)
{
ResetEnterGameStatus();
// Childs UpdateTicks
foreach (var child in Childs)
{
child.ScriptUpdate(currentTime, deltaTime, tickType);
@@ -547,18 +571,9 @@ namespace Demo
else
{
// UpdateTicks
if (this.IsSelfEnableUpdate && UpdatePerFrame > 0)
{
if (ScriptUpdateCounter % UpdatePerFrame == 0)
using (Profiler.BeginZone($"{this.ScriptName}.UpdateTicks"))
UpdateTicks(currentTime, deltaTime, tickType);
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 0;
}
// Childs UpdateTicks
foreach (var child in UpdateChilds)
{
child.ScriptUpdate(currentTime, deltaTime, tickType);
}
if (ScriptUpdateCounter % UpdatePerFrame == 0)
UpdateTicks(currentTime, deltaTime, tickType);
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 0;
}
}
}
@@ -621,24 +636,8 @@ namespace Demo
}
// 统计更新能力
{
IsEnableUpdate = this.IsSelfEnableUpdate;
foreach (var child in this.Childs)
{
if (IsEnableUpdate)
break;
IsEnableUpdate |= child.IsEnableUpdate;
}
if (IsEnableUpdate == false && Parent != null)
Parent.UpdateChilds.Remove(this);
// 当只存在一个需要更新的子物体并且自身不需要更新时, 直接简化调用
if (this.IsSelfEnableUpdate == false && this.UpdateChilds.Count == 1)
{
Parent.UpdateChilds.Remove(this);
var child = this.UpdateChilds[0];
this.UpdateChilds.Clear();
Parent.UpdateChilds.Add(child);
IsEnableUpdate = false;
}
if (this.IsSelfEnableUpdate)
GetRoot().UpdateChilds.Add(this);
}
IsScriptApply = true;
}