43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace BaseGames.Camera
|
|
{
|
|
/// <summary>
|
|
/// 相机区域切换触发器。玩家进入时通知 CameraStateController 切换到目标房间相机。
|
|
/// [ExecuteAlways] 确保编辑器中 Gizmo 立即更新。
|
|
/// </summary>
|
|
[ExecuteAlways]
|
|
[RequireComponent(typeof(BoxCollider2D))]
|
|
public class CameraTriggerZone : MonoBehaviour
|
|
{
|
|
[SerializeField] private RoomCamera _targetCamera;
|
|
[SerializeField] private string _playerTag = "Player";
|
|
|
|
private BoxCollider2D _collider;
|
|
|
|
private void Awake()
|
|
{
|
|
_collider = GetComponent<BoxCollider2D>();
|
|
_collider.isTrigger = true;
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
if (!other.CompareTag(_playerTag)) return;
|
|
if (_targetCamera != null)
|
|
CameraStateController.Instance?.SwitchRoom(_targetCamera);
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
if (_collider == null) _collider = GetComponent<BoxCollider2D>();
|
|
Gizmos.color = new Color(0.2f, 0.8f, 1f, 0.25f);
|
|
Gizmos.matrix = transform.localToWorldMatrix;
|
|
Gizmos.DrawCube(_collider.offset, _collider.size);
|
|
Gizmos.color = new Color(0.2f, 0.8f, 1f, 0.8f);
|
|
Gizmos.DrawWireCube(_collider.offset, _collider.size);
|
|
}
|
|
}
|
|
}
|