Compare commits

2 Commits

Author SHA1 Message Date
716a23fad2 扁平化优化更新 2025-12-02 14:25:04 +08:00
24f6da50ee 更新优化 扁平化+静态剪枝 2025-12-02 11:34:12 +08:00
3 changed files with 40 additions and 53 deletions

View File

@@ -10,6 +10,8 @@ namespace Demo.Game
{ {
public class RootObject : ScriptableObject public class RootObject : ScriptableObject
{ {
protected override bool IsSelfEnableUpdate => false;
[Content] public Dictionary<Type, List<ScriptableObject>> UpdateChilds = new();
[Resources] public BasicAudioSystem audioSystem; [Resources] public BasicAudioSystem audioSystem;
[Content] public GameController RootGameController; [Content] public GameController RootGameController;
@@ -33,6 +35,7 @@ namespace Demo.Game
Keyboard.current.onTextInput -= InputCatchChar; Keyboard.current.onTextInput -= InputCatchChar;
} }
yield return base.UnloadScript(); yield return base.UnloadScript();
UpdateChilds.Clear();
} }
public void EnableScript(string sourcePath, GameController parent) public void EnableScript(string sourcePath, GameController parent)
@@ -82,7 +85,7 @@ namespace Demo.Game
{ {
if (RootGameController.IsMain) if (RootGameController.IsMain)
{ {
void Foo() void foo()
{ {
if (RootGameController.IsAutoPlay) if (RootGameController.IsAutoPlay)
return; return;
@@ -99,10 +102,14 @@ namespace Demo.Game
InputCatch.Clear(); InputCatch.Clear();
} }
} }
foo();
Foo(); }
foreach (var (type, items) in UpdateChilds)
{
using (Profiler.BeginZone($"{type.Name}.ScriptUpdate"))
foreach (var item in items)
item.ScriptUpdate(currentTime, deltaTime, tickType);
} }
base.UpdateTicks(currentTime, deltaTime, tickType);
} }
} }
} }

View File

@@ -211,10 +211,19 @@ namespace Demo
private bool isEnableScript = false; private bool isEnableScript = false;
public string ScriptName = ""; 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 ScriptableObject Parent;
public readonly List<ScriptableObject> Childs = new(); public readonly List<ScriptableObject> Childs = new();
[Content, SerializeField] private List<ScriptableObject> UpdateChilds = new();
/// <summary> /// <summary>
/// 获取根脚本对象 /// 获取根脚本对象
@@ -324,7 +333,7 @@ namespace Demo
{ {
MyHierarchyItem = HierarchyWindow.instance.CreateRootItemEntryWithBinders(1)[0]; 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().target = this;
MyHierarchyItem.GetHierarchyItem().ButtonGameObject.GetComponent<Editor.UI.RightClick>().ScriptObjectMenu = OnHierarchyItemRightClick; MyHierarchyItem.GetHierarchyItem().ButtonGameObject.GetComponent<Editor.UI.RightClick>().ScriptObjectMenu = OnHierarchyItemRightClick;
} }
@@ -356,7 +365,6 @@ namespace Demo
public bool IsParseScript2Expr { get; private set; } = false; public bool IsParseScript2Expr { get; private set; } = false;
protected virtual bool IsSelfEnableUpdate { get => true; } protected virtual bool IsSelfEnableUpdate { get => true; }
public bool IsEnableUpdate { get; private set; } = true;
#region LoadSubScript #region LoadSubScript
@@ -394,7 +402,6 @@ namespace Demo
// Add Child // Add Child
Childs.Add(child); Childs.Add(child);
UpdateChilds.Add(child);
// Load Child Script // Load Child Script
ConventionUtility.StartCoroutine(child.ParseScript2Expr(file.LoadAsText())); ConventionUtility.StartCoroutine(child.ParseScript2Expr(file.LoadAsText()));
@@ -427,7 +434,6 @@ namespace Demo
// Add Child // Add Child
Childs.Add(child); Childs.Add(child);
UpdateChilds.Add(child);
return child; return child;
} }
@@ -530,37 +536,22 @@ namespace Demo
{ {
if (IsScriptApply == false) if (IsScriptApply == false)
return; return;
if (gameObject.activeInHierarchy == false)
return;
using (Profiler.BeginZone($"{GetType().Name}.ScriptUpdate")) if (tickType == TickType.Reset)
{ {
if (tickType == TickType.Reset) ResetEnterGameStatus();
foreach (var child in Childs)
{ {
ResetEnterGameStatus(); child.ScriptUpdate(currentTime, deltaTime, tickType);
// Childs UpdateTicks
foreach (var child in Childs)
{
child.ScriptUpdate(currentTime, deltaTime, tickType);
}
}
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);
}
} }
} }
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) protected virtual void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
@@ -621,23 +612,13 @@ namespace Demo
} }
// 统计更新能力 // 统计更新能力
{ {
IsEnableUpdate = this.IsSelfEnableUpdate; if (this.IsSelfEnableUpdate)
foreach (var child in this.Childs)
{ {
if (IsEnableUpdate) var type = this.GetType();
break; if (GetRoot().UpdateChilds.TryGetValue(type, out var scriptables))
IsEnableUpdate |= child.IsEnableUpdate; scriptables.Add(this);
} else
if (IsEnableUpdate == false && Parent != null) GetRoot().UpdateChilds[type] = new() { this };
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;
} }
} }
IsScriptApply = true; IsScriptApply = true;
@@ -863,7 +844,6 @@ namespace Demo
{ {
base.UpdateTicks(currentTime, deltaTime, tickType); base.UpdateTicks(currentTime, deltaTime, tickType);
// 存在严重的性能开销, 在解决之前将不会允许其快速自动更新 // 存在严重的性能开销, 在解决之前将不会允许其快速自动更新
using (Profiler.BeginZone($"{nameof(TimelineScriptObject)}.{nameof(MyTimelineItem.ResizeOnTimeline)}"))
{ {
if (UIResizeOnTimelineCount > 0.1 || tickType != TickType.Update) if (UIResizeOnTimelineCount > 0.1 || tickType != TickType.Update)
{ {