NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/Camera/CameraStand.cs

76 lines
2.1 KiB
C#
Raw Normal View History

2024-01-17 19:18:23 +08:00
using System.Collections;
using System.Collections.Generic;
2025-04-28 15:05:09 +08:00
using Framework.Utils;
2024-01-17 19:18:23 +08:00
using Gameplay;
using PhxhSDK;
using Sirenix.OdinInspector;
using UnityEngine;
2024-01-18 12:52:30 +08:00
#if UNITY_EDITOR
using UnityEditor;
#endif
2024-01-17 19:18:23 +08:00
2024-01-18 12:29:54 +08:00
public enum ECameraOperationType
{
[LabelText("固定机位")]
Fixed,
2024-01-18 12:52:30 +08:00
2024-01-18 12:29:54 +08:00
[LabelText("自由机位")]
Free,
}
2024-01-17 19:18:23 +08:00
/// <summary>
/// 记录一个相机的机位
/// </summary>
public class CameraStand : MonoBehaviour
{
2024-01-18 12:52:30 +08:00
private const string CAMERA_UNDO_NAME = "cameraStandTest";
2024-01-17 19:18:23 +08:00
[LabelText("是否正交")]
[SerializeField]
private bool _isOrthographic;
2024-01-17 20:00:46 +08:00
[ShowIf("_isOrthographic")]
2024-01-17 19:18:23 +08:00
[LabelText("正交size")]
[SerializeField]
private float _size;
2024-01-17 20:00:46 +08:00
[HideIf("_isOrthographic")]
2024-01-17 19:18:23 +08:00
[LabelText("FOV")]
[SerializeField]
private float _fov;
2024-01-18 12:29:54 +08:00
[LabelText("相机操作类型")]
[SerializeField]
private ECameraOperationType _cameraOperationType = ECameraOperationType.Fixed;
2024-01-18 12:52:30 +08:00
2025-04-28 15:05:09 +08:00
private Camera DefaultCamera => FrameWorkUtils.GetSceneRootComponent<Camera>(gameObject.scene) ??
2024-01-18 12:52:30 +08:00
Camera.main ?? CameraManager.Instance.MainCamera;
2024-01-17 19:18:23 +08:00
[Button("测试机位")]
private void TestStand()
{
Apply();
}
public void Apply(Camera targetCamera = null)
{
targetCamera = targetCamera ? targetCamera : DefaultCamera;
2024-01-18 12:52:30 +08:00
#if UNITY_EDITOR
Undo.RecordObject(targetCamera.transform, CAMERA_UNDO_NAME);
Undo.RecordObject(targetCamera, CAMERA_UNDO_NAME);
#endif
2024-01-17 19:18:23 +08:00
var trans = transform;
targetCamera.transform.SetPositionAndRotation(trans.position, trans.rotation);
targetCamera.orthographic = _isOrthographic;
targetCamera.orthographicSize = _size > 0 ? _size : targetCamera.orthographicSize;
targetCamera.fieldOfView = _fov > 0 ? _fov : targetCamera.fieldOfView;
2024-01-18 12:29:54 +08:00
var cameraController = targetCamera.GetComponent<CameraController>();
if (cameraController != null)
{
2024-01-18 12:52:30 +08:00
#if UNITY_EDITOR
Undo.RecordObject(cameraController, CAMERA_UNDO_NAME);
#endif
2024-01-18 12:29:54 +08:00
cameraController.enabled = _cameraOperationType != ECameraOperationType.Fixed;
}
2024-01-17 19:18:23 +08:00
}
}