feat: 添加场景过渡类型支持,优化场景加载逻辑

This commit is contained in:
2026-05-19 15:18:13 +08:00
parent be96b326de
commit ee0f659c97
12 changed files with 259 additions and 20 deletions

View File

@@ -4,8 +4,15 @@ using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 房间传送点。玩家进入触发器或按交互键时,广播 <see cref="SceneLoadRequest"/>
/// 由 SceneLoader 监听并执行 Additive 场景加载/卸载。
/// 房间/场景过渡触发器。玩家进入触发器或按交互键时,广播 <see cref="SceneLoadRequest"/>
/// <para>
/// 通过 <see cref="_transitionType"/> 控制过渡演出:
/// <list type="bullet">
/// <item><b>Room</b>:极短淡出,无加载画面,适合相邻房间边界。</item>
/// <item><b>Scene</b>:完整淡出 + 加载画面,适合大区域切换。</item>
/// </list>
/// 如需门动画,请改用 <see cref="DoorTransition"/> 组件,它会在动画完成后内部触发本组件。
/// </para>
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class RoomTransition : MonoBehaviour, IInteractable
@@ -17,6 +24,11 @@ namespace BaseGames.World
[SerializeField] private string _targetSceneAddress; // Addressable key目标场景
[SerializeField] private string _targetTransitionId; // 目标房间出生点 ID
[Header("过渡类型")]
[Tooltip("Room极短淡出无加载画面相邻房间边界专用。\n" +
"Scene完整淡出 + 加载画面,大区域/地图间切换专用。")]
[SerializeField] private TransitionType _transitionType = TransitionType.Room;
[Header("触发方式")]
[SerializeField] private bool _autoTrigger = true; // true = 玩家进入触发器自动触发
@@ -32,7 +44,7 @@ namespace BaseGames.World
// ── IInteractable ─────────────────────────────────────────────────────
public bool CanInteract => !_autoTrigger;
public string InteractPrompt => "前往下一区域";
public string InteractPrompt => _transitionType == TransitionType.Scene ? "前往下一区域" : "进入";
public void Interact(Transform player) => RequestTransition();
@@ -47,7 +59,8 @@ namespace BaseGames.World
RequestTransition();
}
private void RequestTransition()
/// <summary>触发过渡。可由触发器、交互系统或 <see cref="DoorTransition"/> 调用。</summary>
public void RequestTransition()
{
if (_requiresKeyItem && !HasItem(_requiredItemId)) return;
@@ -55,7 +68,8 @@ namespace BaseGames.World
{
SceneName = _targetSceneAddress,
EntryTransitionId = _targetTransitionId,
ShowLoadingScreen = true,
TransitionType = _transitionType,
ShowLoadingScreen = _transitionType == TransitionType.Scene,
IsRespawn = false,
});
}
@@ -66,7 +80,7 @@ namespace BaseGames.World
if (string.IsNullOrEmpty(itemId)) return true;
if (_worldState == null)
{
Debug.LogWarning($"[RoomTransition] WorldStateRegistry 未配置, {itemId} 检查跳过");
Debug.LogWarning($"[RoomTransition] WorldStateRegistry 未配置,钥匙 {itemId} 检查跳过");
return false;
}
return _worldState.IsCollected(itemId);
@@ -74,10 +88,12 @@ namespace BaseGames.World
private void OnDrawGizmos()
{
Gizmos.color = new Color(0f, 1f, 0.5f, 0.6f);
var col = GetComponent<Collider2D>();
if (col != null)
Gizmos.DrawWireCube(transform.position, col.bounds.size);
if (col == null) return;
Gizmos.color = _transitionType == TransitionType.Scene
? new Color(1f, 0.6f, 0f, 0.6f) // 橙色:大区域切换
: new Color(0f, 1f, 0.5f, 0.6f); // 绿色:房间切换
Gizmos.DrawWireCube(transform.position, col.bounds.size);
}
}
}