72 lines
3.4 KiB
C#
72 lines
3.4 KiB
C#
using UnityEngine;
|
||
using Unity.Cinemachine;
|
||
|
||
namespace BaseGames.Camera
|
||
{
|
||
/// <summary>
|
||
/// 单房间虚拟相机。激活时提升优先级,停用时降为 0。
|
||
/// 挂载在每个房间的 CinemachineCamera GameObject 上。
|
||
/// </summary>
|
||
[RequireComponent(typeof(CinemachineCamera))]
|
||
public class RoomCamera : MonoBehaviour
|
||
{
|
||
[Header("房间设置")]
|
||
[SerializeField] private RoomVisibleArea _visibleArea;
|
||
[SerializeField] private Vector2 _cameraOffset = Vector2.zero;
|
||
[SerializeField] private CameraBlendProfileSO _blendProfile;
|
||
[SerializeField] private int _activePriority = 15;
|
||
|
||
[Header("可视区域(透视相机)")]
|
||
[Tooltip("摄像机应显示的最大可视矩形(世界坐标)。\n" +
|
||
"在 Scene 视图中可直接拖拽四条边编辑,然后点击 Inspector 中的\n" +
|
||
"「从可视区域更新限位区域」按钮将其换算为 CinemachineConfiner2D 所需的限位多边形。")]
|
||
[SerializeField] private Rect _visibleBounds = new Rect(-12f, -6f, 24f, 12f);
|
||
|
||
[Tooltip("摄像机到场景平面(Z = 0)的垂直距离,用于透视视口尺寸计算。\n" +
|
||
"留 0 时自动取 transform.position.z 的绝对值(推荐)。")]
|
||
[SerializeField] private float _cameraDepth = 0f;
|
||
|
||
private CinemachineCamera _vcam;
|
||
|
||
private void Awake() => _vcam = GetComponent<CinemachineCamera>();
|
||
private void OnEnable() => _vcam.Priority = _activePriority;
|
||
private void OnDisable() => _vcam.Priority = 0;
|
||
|
||
public PolygonCollider2D ConfinerCollider => _visibleArea?.Collider;
|
||
public Vector2 CameraOffset => _cameraOffset;
|
||
public CameraBlendProfileSO BlendProfile => _blendProfile;
|
||
public Rect VisibleBounds => _visibleBounds;
|
||
|
||
/// <summary>
|
||
/// 摄像机到场景平面的有效深度。
|
||
/// _cameraDepth > 0 时使用配置值,否则自动读取 |transform.position.z|,再兜底 10。
|
||
/// </summary>
|
||
public float CameraDepth
|
||
{
|
||
get
|
||
{
|
||
if (_cameraDepth > 0f) return _cameraDepth;
|
||
float z = Mathf.Abs(transform.position.z);
|
||
return z > 0.01f ? z : 10f;
|
||
}
|
||
}
|
||
|
||
/// <summary>在 CameraStateController 管理的激活流程中调用。</summary>
|
||
public void Activate() => gameObject.SetActive(true);
|
||
public void Deactivate() => gameObject.SetActive(false);
|
||
|
||
// ── Gizmo ──────────────────────────────────────────────────────────────
|
||
private void OnDrawGizmosSelected()
|
||
{
|
||
// 黄色:可视区域(设计意图——玩家在此房间内的最大可见范围)
|
||
Vector3 center = new Vector3(_visibleBounds.center.x, _visibleBounds.center.y, 0f);
|
||
Vector3 size = new Vector3(_visibleBounds.width, _visibleBounds.height, 0.01f);
|
||
|
||
Gizmos.color = new Color(1f, 0.85f, 0.15f, 0.10f);
|
||
Gizmos.DrawCube(center, size);
|
||
Gizmos.color = new Color(1f, 0.85f, 0.15f, 0.90f);
|
||
Gizmos.DrawWireCube(center, size);
|
||
}
|
||
}
|
||
}
|