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
{
protected override bool IsSelfEnableUpdate => false;
[Content] public Dictionary<Type, 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,7 +85,7 @@ namespace Demo.Game
{
if (RootGameController.IsMain)
{
void Foo()
void foo()
{
if (RootGameController.IsAutoPlay)
return;
@@ -99,10 +102,14 @@ namespace Demo.Game
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;
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;
}
@@ -530,15 +536,10 @@ namespace Demo
{
if (IsScriptApply == false)
return;
if (gameObject.activeInHierarchy == false)
return;
using (Profiler.BeginZone($"{GetType().Name}.ScriptUpdate"))
{
if (tickType == TickType.Reset)
{
ResetEnterGameStatus();
// Childs UpdateTicks
foreach (var child in Childs)
{
child.ScriptUpdate(currentTime, deltaTime, tickType);
@@ -547,20 +548,10 @@ 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);
}
}
}
}
protected virtual void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
@@ -621,23 +612,13 @@ namespace Demo
}
// 统计更新能力
{
IsEnableUpdate = this.IsSelfEnableUpdate;
foreach (var child in this.Childs)
if (this.IsSelfEnableUpdate)
{
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;
var type = this.GetType();
if (GetRoot().UpdateChilds.TryGetValue(type, out var scriptables))
scriptables.Add(this);
else
GetRoot().UpdateChilds[type] = new() { this };
}
}
IsScriptApply = true;
@@ -863,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)
{