mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(scene): 重构场景路由系统架构
- 移除原有的 ISceneLoader 接口定义 - 新增 SceneRouterBase 抽象基类实现场景路由基础功能 - 添加 ISceneRoot 接口定义场景根节点操作规范 - 创建 ISceneRouter 接口统一场景路由操作契约 - 实现场景替换和卸载的核心逻辑 - 添加场景切换前后钩子方法支持扩展功能 - [release ci]
This commit is contained in:
parent
41661c8a6f
commit
917a95e245
@ -11,29 +11,21 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using GFramework.Core.Abstractions.utility;
|
||||
|
||||
namespace GFramework.Core.Abstractions.scene;
|
||||
namespace GFramework.Game.Abstractions.scene;
|
||||
|
||||
/// <summary>
|
||||
/// 定义场景加载器的接口,用于管理场景的加载、替换和卸载操作。
|
||||
/// 定义场景根接口,用于管理场景的加载和卸载操作。
|
||||
/// </summary>
|
||||
public interface ISceneLoader<out T> : IUtility
|
||||
public interface ISceneRoot
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前加载的场景对象。
|
||||
/// </summary>
|
||||
/// <returns>当前场景对象,如果未加载任何场景则返回 null。</returns>
|
||||
T? Current { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 替换当前场景为指定键对应的场景。
|
||||
/// </summary>
|
||||
/// <param name="key">场景的唯一标识符或键值。</param>
|
||||
/// <param name="key">场景的唯一标识符,用于定位要加载的场景。</param>
|
||||
void Replace(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 卸载当前加载的场景。
|
||||
/// 卸载当前场景。
|
||||
/// </summary>
|
||||
void Unload();
|
||||
}
|
||||
43
GFramework.Game.Abstractions/scene/ISceneRouter.cs
Normal file
43
GFramework.Game.Abstractions/scene/ISceneRouter.cs
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2026 GeWuYou
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
namespace GFramework.Game.Abstractions.scene;
|
||||
|
||||
/// <summary>
|
||||
/// 定义场景路由接口,用于管理场景的切换、卸载以及根节点绑定。
|
||||
/// </summary>
|
||||
public interface ISceneRouter
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前场景的唯一标识符(键)。
|
||||
/// </summary>
|
||||
/// <value>当前场景的键,如果未加载任何场景则返回 null。</value>
|
||||
string? CurrentKey { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 替换当前场景为指定键对应的场景。
|
||||
/// </summary>
|
||||
/// <param name="sceneKey">目标场景的唯一标识符(键)。</param>
|
||||
void Replace(string sceneKey);
|
||||
|
||||
/// <summary>
|
||||
/// 卸载当前场景。
|
||||
/// </summary>
|
||||
void Unload();
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的场景根节点与当前路由进行绑定。
|
||||
/// </summary>
|
||||
/// <param name="root">需要绑定的场景根节点。</param>
|
||||
void BindRoot(ISceneRoot root);
|
||||
}
|
||||
92
GFramework.Game/scene/SceneRouterBase.cs
Normal file
92
GFramework.Game/scene/SceneRouterBase.cs
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright (c) 2026 GeWuYou
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Game.Abstractions.scene;
|
||||
|
||||
namespace GFramework.Game.scene;
|
||||
|
||||
/// <summary>
|
||||
/// 场景路由基类,提供场景切换和卸载的基础功能。
|
||||
/// 实现了 <see cref="ISceneRouter"/> 接口,用于管理场景的加载、替换和卸载操作。
|
||||
/// </summary>
|
||||
public abstract class SceneRouterBase
|
||||
: AbstractSystem, ISceneRouter
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前绑定的场景根节点。
|
||||
/// </summary>
|
||||
protected ISceneRoot? Root;
|
||||
|
||||
/// <summary>
|
||||
/// 当前激活场景的键值。
|
||||
/// </summary>
|
||||
public string? CurrentKey { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 绑定场景根节点。
|
||||
/// </summary>
|
||||
/// <param name="root">要绑定的场景根节点。</param>
|
||||
public void BindRoot(ISceneRoot root)
|
||||
{
|
||||
Root = root;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 替换当前场景为指定键值的新场景。
|
||||
/// 在替换前后会调用相应的虚方法以支持扩展逻辑。
|
||||
/// </summary>
|
||||
/// <param name="sceneKey">目标场景的键值。</param>
|
||||
public void Replace(string sceneKey)
|
||||
{
|
||||
// 调用替换前的钩子方法
|
||||
OnBeforeReplace(sceneKey);
|
||||
|
||||
// 执行场景替换操作
|
||||
Root!.Replace(sceneKey);
|
||||
|
||||
// 更新当前场景键值
|
||||
CurrentKey = sceneKey;
|
||||
|
||||
// 调用替换后的钩子方法
|
||||
OnAfterReplace(sceneKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 卸载当前场景,并将当前场景键值置为空。
|
||||
/// </summary>
|
||||
public void Unload()
|
||||
{
|
||||
// 执行场景卸载操作
|
||||
Root!.Unload();
|
||||
|
||||
// 清空当前场景键值
|
||||
CurrentKey = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景替换前的虚方法,可在子类中重写以实现自定义逻辑。
|
||||
/// </summary>
|
||||
/// <param name="key">即将被替换的场景键值。</param>
|
||||
protected virtual void OnBeforeReplace(string key)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景替换后的虚方法,可在子类中重写以实现自定义逻辑。
|
||||
/// </summary>
|
||||
/// <param name="key">已替换的场景键值。</param>
|
||||
protected virtual void OnAfterReplace(string key)
|
||||
{
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user