阶段3/添加手动启用调度器方法, Scheduler仍未启用

This commit is contained in:
2025-12-02 09:50:22 +08:00
parent f37658a481
commit cccf1f7577
2 changed files with 166 additions and 59 deletions

View File

@@ -107,16 +107,19 @@ namespace Demo
EnterGameLocalScaling = Vector3.one;
[Content, SerializeField] private bool IsSetObjectDisable = false;
[Content] public int UpdatePerFrame = 1;
[Content, SerializeField, Header("UpdateMode")]
[Content, SerializeField, Header("UpdateMode")]
protected UpdateMode _updateMode = UpdateMode.Permanent;
[Content, SerializeField]
[Content, SerializeField]
protected float _activeStartTime = 0f;
[Content, SerializeField]
[Content, SerializeField]
protected float _activeEndTime = float.MaxValue;
[Content, SerializeField]
private bool _useUpdateScheduler = false; // 控制是否使用新的UpdateScheduler
/// <summary>
/// 设置坐标
/// </summary>
@@ -171,16 +174,25 @@ namespace Demo
{
UpdatePerFrame = Mathf.Max(1, frame);
}
/// <summary>
/// 启用新的UpdateScheduler用于逐步迁移
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public void EnableUpdateScheduler()
{
_useUpdateScheduler = true;
}
/// <summary>
/// 子类可重写定义自己的Update模式
/// </summary>
protected virtual UpdateMode GetUpdateMode() => UpdateMode.Permanent;
/// <summary>
/// 子类可重写,定义自己的活跃时间范围
/// </summary>
protected virtual (float start, float end) GetActiveTimeRange()
protected virtual (float start, float end) GetActiveTimeRange()
=> (0f, float.MaxValue);
private void ScriptableObjectDoReset()
@@ -553,7 +565,7 @@ namespace Demo
if (gameObject.activeInHierarchy == false)
return;
using (Profiler.BeginZone($"{GetType().Name}.ScriptUpdate"))
using (Profiler.BeginZone($"{m_GetTypeName}.ScriptUpdate"))
{
if (tickType == TickType.Reset)
{
@@ -599,8 +611,7 @@ namespace Demo
#else
MonoBehaviour,
#endif
IHierarchyItemClickEventListener,
IUpdateable
IHierarchyItemClickEventListener
{
protected virtual IEnumerator DoSomethingDuringApplyScript()
{
@@ -661,9 +672,42 @@ namespace Demo
IsEnableUpdate = false;
}
}
// 可选的调度器注册
if (_useUpdateScheduler && (IsSelfEnableUpdate || IsEnableUpdate))
{
RegisterToScheduler();
}
IsScriptApply = true;
}
/// <summary>
/// 注册到UpdateScheduler
/// </summary>
private void RegisterToScheduler()
{
// 获取Update模式
_updateMode = GetUpdateMode();
// 获取时间范围
if (_updateMode == UpdateMode.TimeBound)
{
(_activeStartTime, _activeEndTime) = GetActiveTimeRange();
}
// 注册到调度器
try
{
GetRoot()?.Scheduler?.Register(this, _updateMode, _activeStartTime, _activeEndTime);
Debug.Log($"[ScriptableObject] 已注册到调度器: {GetUpdateName()}, Mode={_updateMode}");
}
catch (System.Exception ex)
{
Debug.LogError($"[ScriptableObject] 注册到调度器失败: {ex.Message}", this);
}
}
public virtual IEnumerator UnloadScript()
{
if (EnsureEnableScript())
@@ -717,6 +761,63 @@ namespace Demo
}
}
/// <summary>
/// IUpdateable接口实现
/// </summary>
public partial class ScriptableObject : IUpdateable
{
/// <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;
}
}
private string c_GetTypeName;
private string m_GetTypeName
{
get
{
if (string.IsNullOrEmpty(c_GetTypeName))
{
c_GetTypeName = GetType().Name;
}
return c_GetTypeName;
}
}
/// <summary>
/// IUpdateable.GetUpdateName实现
/// </summary>
public string GetUpdateName()
{
return $"{ScriptName}<{m_GetTypeName}>";
}
/// <summary>
/// IUpdateable.IsUpdateReady实现
/// </summary>
public bool IsUpdateReady => IsScriptApply;
}
#endregion
public interface IAssetBundleLoader : IScriptableObject
@@ -912,47 +1013,4 @@ 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;
}
}