阶段2/IUpdateable接口适配

This commit is contained in:
2025-12-01 17:54:03 +08:00
parent 07fa883537
commit f37658a481
4 changed files with 131 additions and 10 deletions

View File

@@ -107,6 +107,15 @@ namespace Demo
EnterGameLocalScaling = Vector3.one;
[Content, SerializeField] private bool IsSetObjectDisable = false;
[Content] public int UpdatePerFrame = 1;
[Content, SerializeField, Header("UpdateMode")]
protected UpdateMode _updateMode = UpdateMode.Permanent;
[Content, SerializeField]
protected float _activeStartTime = 0f;
[Content, SerializeField]
protected float _activeEndTime = float.MaxValue;
/// <summary>
/// 设置坐标
@@ -162,6 +171,17 @@ namespace Demo
{
UpdatePerFrame = Mathf.Max(1, frame);
}
/// <summary>
/// 子类可重写定义自己的Update模式
/// </summary>
protected virtual UpdateMode GetUpdateMode() => UpdateMode.Permanent;
/// <summary>
/// 子类可重写,定义自己的活跃时间范围
/// </summary>
protected virtual (float start, float end) GetActiveTimeRange()
=> (0f, float.MaxValue);
private void ScriptableObjectDoReset()
{
@@ -579,7 +599,8 @@ namespace Demo
#else
MonoBehaviour,
#endif
IHierarchyItemClickEventListener
IHierarchyItemClickEventListener,
IUpdateable
{
protected virtual IEnumerator DoSomethingDuringApplyScript()
{
@@ -891,4 +912,47 @@ namespace Demo
return new(color.x, color.y, color.z);
}
}
/// <summary>
/// IUpdateable接口实现
/// </summary>
public partial class ScriptableObject
{
/// <summary>
/// IUpdateable.FlatOptimizationUpdate实现 - 扁平化优化Update入口直接更新对象自身无递归遍历子对象
/// </summary>
public void FlatOptimizationUpdate(float currentTime, float deltaTime, TickType tickType)
{
if (IsScriptApply == false)
return;
if (gameObject.activeInHierarchy == false)
return;
// 只更新自己,不递归子节点
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;
}
}
/// <summary>
/// IUpdateable.GetUpdateName实现
/// </summary>
public string GetUpdateName()
{
return $"{ScriptName}<{GetType().Name}>";
}
/// <summary>
/// IUpdateable.IsUpdateReady实现
/// </summary>
public bool IsUpdateReady => IsScriptApply;
}
}

View File

@@ -8,9 +8,9 @@ namespace Demo.Game
public interface IUpdateable
{
/// <summary>
/// 直接Update调用无递归
/// 扁平化优化Update调用直接更新对象自身(无递归遍历子对象)
/// </summary>
void DoUpdate(float currentTime, float deltaTime, ScriptableObject.TickType tickType);
void FlatOptimizationUpdate(float currentTime, float deltaTime, ScriptableObject.TickType tickType);
/// <summary>
/// 对象名称,用于调试

View File

@@ -113,7 +113,7 @@ namespace Demo.Game
{
if (permanentObjects[i]?.IsUpdateReady == true)
{
permanentObjects[i].DoUpdate(currentTime, deltaTime, tickType);
permanentObjects[i].FlatOptimizationUpdate(currentTime, deltaTime, tickType);
}
}
@@ -126,7 +126,7 @@ namespace Demo.Game
continue;
}
activeObjects[i].DoUpdate(currentTime, deltaTime, tickType);
activeObjects[i].FlatOptimizationUpdate(currentTime, deltaTime, tickType);
}
}
}