AI补足注释(已经快看不懂了)
实现i18n 优化部分模块的逻辑以优化性能 修复物品展示框打开时按下ESC唤出暂停菜单但没有暂停的bug Signed-off-by: Eicy <im@crash.work>
This commit is contained in:
@@ -2,27 +2,35 @@
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 控制摄像机的类
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public float mouseSensitivity = 100.0f;
|
||||
public Transform playerBody;
|
||||
[Header("Camera Settings")]
|
||||
public float mouseSensitivity = 100.0f; // 鼠标灵敏度
|
||||
public Transform playerBody; // 玩家身体的 Transform 组件
|
||||
|
||||
private float _xRotation;
|
||||
private float _xRotation; // 摄像机的上下旋转角度
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// 锁定鼠标光标到屏幕中心
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 获取鼠标的水平和垂直移动值
|
||||
var mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
|
||||
var mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
// 更新垂直旋转值并限制在 -90 到 90 度之间
|
||||
_xRotation -= mouseY;
|
||||
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f);
|
||||
|
||||
// 应用旋转到摄像机
|
||||
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
|
||||
|
||||
// 旋转玩家身体
|
||||
playerBody.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,21 +4,27 @@ using UnityEngine;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 处理摄像机与可交互物体的交互
|
||||
public class CameraInterAct : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int maxInterActDistance = 10;
|
||||
[SerializeField] private GameObject target;
|
||||
|
||||
[Header("Interaction Settings")]
|
||||
[SerializeField] private int maxInterActDistance = 10; // 最大交互距离
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Physics.Raycast(transform.position, transform.forward, out var raycastHit, maxInterActDistance);
|
||||
if (raycastHit.collider && Input.GetKeyDown(KeySettingManager.Instance.GetKey("InterAct")))
|
||||
// 执行射线检测
|
||||
if (!Physics.Raycast(transform.position, transform.forward, out var raycastHit,
|
||||
maxInterActDistance)) return;
|
||||
// 检测到碰撞体并按下交互键
|
||||
if (Input.GetKeyDown(KeySettingManager.Instance.GetKey("InterAct")))
|
||||
{
|
||||
EventManager.Instance.OnCameraInterAct(raycastHit.collider.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// 在场景视图中绘制射线,便于调试
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(transform.position, transform.position + transform.forward * maxInterActDistance);
|
||||
}
|
||||
|
||||
@@ -4,38 +4,52 @@ using UnityEngine.Rendering.PostProcessing;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 处理相机的后期处理效果
|
||||
public class CameraPostProcessing : MonoBehaviour
|
||||
{
|
||||
private UnityEngine.Camera _camera;
|
||||
private PostProcessVolume _processVolume;
|
||||
private UnityEngine.Camera _camera; // 参考相机组件
|
||||
private PostProcessVolume _processVolume; // 后期处理体积
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 获取相机和后期处理组件
|
||||
_camera = GetComponent<UnityEngine.Camera>();
|
||||
_processVolume = GetComponent<PostProcessVolume>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
EventManager.Instance.PlayerRunning += PostProcess;
|
||||
EventManager.Instance.PlayerRunStop += ProcessingStop;
|
||||
// 订阅事件
|
||||
EventManager.Instance.PlayerRunning += PostProcess; // 玩家开始跑步时激活后期处理效果
|
||||
EventManager.Instance.PlayerRunStop += ProcessingStop; // 玩家停止跑步时禁用后期处理效果
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// 取消订阅事件
|
||||
EventManager.Instance.PlayerRunning -= PostProcess;
|
||||
EventManager.Instance.PlayerRunStop -= ProcessingStop;
|
||||
}
|
||||
|
||||
// 启动后期处理效果
|
||||
private void PostProcess()
|
||||
{
|
||||
// 插值设置相机视野
|
||||
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 90f, Time.deltaTime * 10f);
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value = Mathf.Lerp(
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value, 1f, Time.deltaTime * 10f);
|
||||
|
||||
// 插值设置色差强度
|
||||
var chromaticAberration = _processVolume.profile.GetSetting<ChromaticAberration>();
|
||||
chromaticAberration.intensity.value = Mathf.Lerp(chromaticAberration.intensity.value, 1f, Time.deltaTime * 10f);
|
||||
}
|
||||
|
||||
// 停止后期处理效果
|
||||
private void ProcessingStop()
|
||||
{
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value = Mathf.Lerp(
|
||||
_processVolume.profile.GetSetting<ChromaticAberration>().intensity.value, 0f, Time.deltaTime * 10f);
|
||||
// 插值设置色差强度归零
|
||||
var chromaticAberration = _processVolume.profile.GetSetting<ChromaticAberration>();
|
||||
chromaticAberration.intensity.value = Mathf.Lerp(chromaticAberration.intensity.value, 0f, Time.deltaTime * 10f);
|
||||
|
||||
// 插值设置相机视野回到默认值
|
||||
_camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 60f, Time.deltaTime * 10f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,78 +3,99 @@ using UnityEngine;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 处理相机的震动效果
|
||||
public class CameraShake : MonoBehaviour
|
||||
{
|
||||
public Transform cameraTransform;
|
||||
public float amplitude = 0.05f;
|
||||
public float frequency = 10.0f;
|
||||
[Header("Camera Settings")]
|
||||
[Tooltip("相机抖动的Transform。")]
|
||||
public Transform cameraTransform; // 相机的变换组件
|
||||
|
||||
private Vector3 _originalPos;
|
||||
private bool _isRunning;
|
||||
private bool _isWalking;
|
||||
[Header("抖动参数")]
|
||||
[Tooltip("抖动效果的振幅。")]
|
||||
public float amplitude = 0.05f; // 震动幅度
|
||||
[Tooltip("抖动效果的频率。")]
|
||||
public float frequency = 10.0f; // 震动频率
|
||||
|
||||
private Vector3 _originalPos; // 相机的初始位置
|
||||
private bool _isRunning; // 玩家是否在奔跑
|
||||
private bool _isWalking; // 玩家是否在行走
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// 存储相机的初始位置
|
||||
_originalPos = cameraTransform.localPosition;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
EventManager.Instance.PlayerRunning += OnPlayerRunning;
|
||||
EventManager.Instance.PlayerRunStop += StopRunning;
|
||||
EventManager.Instance.PlayerWalking += OnPlayerWalking;
|
||||
EventManager.Instance.PlayerWalkStop += StopWalking;
|
||||
// 订阅事件
|
||||
EventManager.Instance.PlayerRunning += OnPlayerRunning; // 玩家开始奔跑事件
|
||||
EventManager.Instance.PlayerRunStop += StopRunning; // 玩家停止奔跑事件
|
||||
EventManager.Instance.PlayerWalking += OnPlayerWalking; // 玩家开始行走事件
|
||||
EventManager.Instance.PlayerWalkStop += StopWalking; // 玩家停止行走事件
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// 取消订阅事件
|
||||
EventManager.Instance.PlayerRunning -= OnPlayerRunning;
|
||||
EventManager.Instance.PlayerRunStop -= StopRunning;
|
||||
EventManager.Instance.PlayerWalking -= OnPlayerWalking;
|
||||
EventManager.Instance.PlayerWalkStop -= StopWalking;
|
||||
}
|
||||
|
||||
// 玩家开始奔跑时调用
|
||||
private void OnPlayerRunning()
|
||||
{
|
||||
_isRunning = true;
|
||||
_isRunning = true; // 设置奔跑状态
|
||||
}
|
||||
|
||||
// 玩家开始行走时调用
|
||||
private void OnPlayerWalking()
|
||||
{
|
||||
_isWalking = true;
|
||||
_isWalking = true; // 设置行走状态
|
||||
}
|
||||
|
||||
// 停止奔跑时调用
|
||||
private void StopRunning()
|
||||
{
|
||||
_isRunning = false;
|
||||
_isRunning = false; // 重置奔跑状态
|
||||
}
|
||||
|
||||
// 停止行走时调用
|
||||
private void StopWalking()
|
||||
{
|
||||
_isWalking = false;
|
||||
_isWalking = false; // 重置行走状态
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 处理相机震动效果
|
||||
if (_isRunning)
|
||||
{
|
||||
var xShake = Mathf.Sin(Time.time * frequency) * amplitude;
|
||||
var yShake = Mathf.Cos(Time.time * frequency * 2) * amplitude * 0.5f;
|
||||
|
||||
cameraTransform.localPosition = _originalPos + new Vector3(xShake, yShake, 0);
|
||||
// 计算奔跑状态下的震动
|
||||
ApplyShake(frequency, amplitude);
|
||||
}
|
||||
else if (_isWalking)
|
||||
{
|
||||
// 计算行走状态下的震动
|
||||
ApplyShake(frequency / 2, amplitude);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_isWalking)
|
||||
{
|
||||
var xShake = Mathf.Sin(Time.time * frequency / 2) * amplitude;
|
||||
var yShake = Mathf.Cos(Time.time * frequency / 2 * 2) * amplitude * 0.5f;
|
||||
|
||||
cameraTransform.localPosition = _originalPos + new Vector3(xShake, yShake, 0);
|
||||
}
|
||||
else
|
||||
cameraTransform.localPosition = _originalPos;
|
||||
// 恢复相机到初始位置
|
||||
cameraTransform.localPosition = _originalPos;
|
||||
}
|
||||
}
|
||||
|
||||
// 应用相机震动效果
|
||||
private void ApplyShake(float shakeFrequency, float shakeAmplitude)
|
||||
{
|
||||
var xShake = Mathf.Sin(Time.time * shakeFrequency) * shakeAmplitude; // X轴震动
|
||||
var yShake = Mathf.Cos(Time.time * shakeFrequency * 2) * shakeAmplitude * 0.5f; // Y轴震动
|
||||
|
||||
// 更新相机位置
|
||||
cameraTransform.localPosition = _originalPos + new Vector3(xShake, yShake, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,54 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 处理屏幕的纵横比,以确保游戏在不同分辨率下的正确显示
|
||||
public class ScreenAspect : MonoBehaviour
|
||||
{
|
||||
//目标比例,默认16:9
|
||||
public float TargetAspect = 16f / 9f;
|
||||
private UnityEngine.Camera _mainCamera;
|
||||
[FormerlySerializedAs("TargetAspect")]
|
||||
[Header("Target Aspect Ratio")]
|
||||
[Tooltip("目标纵横比,默认设置为16:9")]
|
||||
public float targetAspect = 16f / 9f; // 默认目标比例
|
||||
|
||||
private UnityEngine.Camera _mainCamera; // 主相机引用
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 获取主相机
|
||||
_mainCamera = UnityEngine.Camera.main;
|
||||
|
||||
// 计算当前窗口的纵横比
|
||||
var windowAspect = Screen.width / (float)Screen.height;
|
||||
|
||||
var scaleHeight = windowAspect / TargetAspect;
|
||||
// 计算缩放高度
|
||||
var scaleHeight = windowAspect / targetAspect;
|
||||
|
||||
// 根据缩放比例调整相机的视口
|
||||
if (scaleHeight < 1f)
|
||||
{
|
||||
var rect = _mainCamera.rect;
|
||||
|
||||
rect.width = 1f;
|
||||
rect.height = scaleHeight;
|
||||
rect.x = 0;
|
||||
rect.y = (1f - scaleHeight) / 2f;
|
||||
|
||||
_mainCamera.rect = rect;
|
||||
// 纵向拉伸的情况
|
||||
SetCameraRect(1f, scaleHeight, 0, (1f - scaleHeight) / 2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 横向拉伸的情况
|
||||
var scaleWidth = 1f / scaleHeight;
|
||||
|
||||
var rect = _mainCamera.rect;
|
||||
|
||||
rect.width = scaleWidth;
|
||||
rect.height = 1f;
|
||||
rect.x = (1f - scaleWidth) / 2f;
|
||||
rect.y = 0;
|
||||
|
||||
_mainCamera.rect = rect;
|
||||
SetCameraRect(scaleWidth, 1f, (1f - scaleWidth) / 2f, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置相机视口的矩形
|
||||
private void SetCameraRect(float width, float height, float x, float y)
|
||||
{
|
||||
var rect = _mainCamera.rect; // 获取相机的当前视口矩形
|
||||
|
||||
rect.width = width; // 设置视口宽度
|
||||
rect.height = height; // 设置视口高度
|
||||
rect.x = x; // 设置视口X位置
|
||||
rect.y = y; // 设置视口Y位置
|
||||
|
||||
_mainCamera.rect = rect; // 应用新的视口矩形
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user