更新优化 扁平化+静态剪枝
This commit is contained in:
@@ -10,6 +10,8 @@ namespace Demo.Game
|
|||||||
{
|
{
|
||||||
public class RootObject : ScriptableObject
|
public class RootObject : ScriptableObject
|
||||||
{
|
{
|
||||||
|
protected override bool IsSelfEnableUpdate => false;
|
||||||
|
[Content] public 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,27 +85,25 @@ namespace Demo.Game
|
|||||||
{
|
{
|
||||||
if (RootGameController.IsMain)
|
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)
|
InputCatch.Clear();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
base.UpdateTicks(currentTime, deltaTime, tickType);
|
foreach (var item in UpdateChilds)
|
||||||
|
{
|
||||||
|
item.ScriptUpdate(currentTime, deltaTime, tickType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -525,20 +531,38 @@ namespace Demo
|
|||||||
Update
|
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;
|
[Content, SerializeField] private int ScriptUpdateCounter = 0;
|
||||||
public void ScriptUpdate(float currentTime, float deltaTime, TickType tickType)
|
public void ScriptUpdate(float currentTime, float deltaTime, TickType tickType)
|
||||||
{
|
{
|
||||||
if (IsScriptApply == false)
|
if (IsScriptApply == false)
|
||||||
return;
|
return;
|
||||||
if (gameObject.activeInHierarchy == false)
|
|
||||||
return;
|
|
||||||
|
|
||||||
using (Profiler.BeginZone($"{GetType().Name}.ScriptUpdate"))
|
using (Profiler.BeginZone(ScriptUpdateZoneName))
|
||||||
{
|
{
|
||||||
if (tickType == TickType.Reset)
|
if (tickType == TickType.Reset)
|
||||||
{
|
{
|
||||||
ResetEnterGameStatus();
|
ResetEnterGameStatus();
|
||||||
// Childs UpdateTicks
|
|
||||||
foreach (var child in Childs)
|
foreach (var child in Childs)
|
||||||
{
|
{
|
||||||
child.ScriptUpdate(currentTime, deltaTime, tickType);
|
child.ScriptUpdate(currentTime, deltaTime, tickType);
|
||||||
@@ -547,18 +571,9 @@ namespace Demo
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// UpdateTicks
|
// UpdateTicks
|
||||||
if (this.IsSelfEnableUpdate && UpdatePerFrame > 0)
|
if (ScriptUpdateCounter % UpdatePerFrame == 0)
|
||||||
{
|
UpdateTicks(currentTime, deltaTime, tickType);
|
||||||
if (ScriptUpdateCounter % UpdatePerFrame == 0)
|
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -621,24 +636,8 @@ namespace Demo
|
|||||||
}
|
}
|
||||||
// 统计更新能力
|
// 统计更新能力
|
||||||
{
|
{
|
||||||
IsEnableUpdate = this.IsSelfEnableUpdate;
|
if (this.IsSelfEnableUpdate)
|
||||||
foreach (var child in this.Childs)
|
GetRoot().UpdateChilds.Add(this);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
IsScriptApply = true;
|
IsScriptApply = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user