Files
zeling_v2/Assets/_Game/Scripts/Camera/CameraAxisLockExtension.cs
2026-05-17 07:56:12 +08:00

44 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using Unity.Cinemachine;
namespace BaseGames.Camera
{
/// <summary>
/// Cinemachine 扩展:在 Body 阶段之后硬锁定相机某一轴向。
///
/// 用途:
/// - <see cref="LockX"/> = true → 垂直竖井 / 电梯相机仅上下移动X 固定于限位区域中心。
/// - <see cref="LockY"/> = true → 水平走廊相机仅左右移动Y 固定于限位区域中心。
///
/// 由 <see cref="CameraStateController"/> 在切换区域时自动写入 <see cref="LockedX"/> /
/// <see cref="LockedY"/>(从 ConfinerCollider.bounds.center 取值)并切换锁定开关。
/// </summary>
[AddComponentMenu("Cinemachine/Extensions/Camera Axis Lock")]
[DisallowMultipleComponent]
public class CameraAxisLockExtension : CinemachineExtension
{
/// <summary>锁定 X 轴(垂直竖井)。</summary>
[HideInInspector] public bool LockX = false;
/// <summary>锁定 Y 轴(水平走廊)。</summary>
[HideInInspector] public bool LockY = false;
/// <summary>X 轴锁定到的世界坐标(由 CameraStateController 写入)。</summary>
[HideInInspector] public float LockedX = 0f;
/// <summary>Y 轴锁定到的世界坐标(由 CameraStateController 写入)。</summary>
[HideInInspector] public float LockedY = 0f;
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage,
ref CameraState state,
float deltaTime)
{
if (stage != CinemachineCore.Stage.Body) return;
var pos = state.RawPosition;
if (LockX) pos.x = LockedX;
if (LockY) pos.y = LockedY;
state.RawPosition = pos;
}
}
}