fix: 优化了MainCamera脚本的性能

This commit is contained in:
jackfiled 2023-02-12 17:01:10 +08:00
parent 2290812228
commit ce5d68721d

View File

@ -9,7 +9,6 @@ public class CameraBehaviour : MonoBehaviour
private const int MouseZoomMax = 20; //相机距离最大值 private const int MouseZoomMax = 20; //相机距离最大值
private const float XSpeed = 250.0f; private const float XSpeed = 250.0f;
private const float YSpeed = 120.0f; private const float YSpeed = 120.0f;
private const int YMinLimit = -360; private const int YMinLimit = -360;
private const int YMaxLimit = 360; private const int YMaxLimit = 360;
@ -23,11 +22,11 @@ public class CameraBehaviour : MonoBehaviour
private Vector3 _initPosition; //平移时用于存储平移的起点位置 private Vector3 _initPosition; //平移时用于存储平移的起点位置
private Vector3 _cameraX; //相机的x轴方向向量 private Vector3 _cameraX; //相机的x轴方向向量
private Vector3 _cameraY; //相机的y轴方向向量 private Vector3 _cameraY; //相机的y轴方向向量
private Vector3 _cameraZ; //相机的z轴方向向量
private Vector3 _initScreenPosition; //中键刚按下时鼠标的屏幕坐标(第三个值其实没什么用) private Vector3 _initScreenPosition; //中键刚按下时鼠标的屏幕坐标(第三个值其实没什么用)
private Vector3 _cursorScreenPosition; //当前鼠标的屏幕坐标(第三个值其实没什么用) private Vector3 _cursorScreenPosition; //当前鼠标的屏幕坐标(第三个值其实没什么用)
private Camera _mainCamera;
private void Start() private void Start()
{ {
//这里就是设置一下初始的相机视角以及一些其他变量这里的x和y。。。是和下面getAxis的mouse x与mouse y对应 //这里就是设置一下初始的相机视角以及一些其他变量这里的x和y。。。是和下面getAxis的mouse x与mouse y对应
@ -36,13 +35,16 @@ private void Start()
_eulerY = angles.x; _eulerY = angles.x;
_targetPosition = target.position; _targetPosition = target.position;
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); _cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
// 引入中间变量 提高代码效率 // 引入中间变量 提高代码效率
var transform1 = transform; var transform1 = transform;
transform1.rotation = _cameraRotation; //设置相机姿态 //设置相机姿态
/*var position = transform1.rotation = _cameraRotation;
_cameraRotation * new Vector3(0.0F, 0.0F, -_distance) + //设置相机位置
_targetPosition; //四元数表示一个旋转,四元数乘以向量相当于把向量旋转对应角度,然后加上目标物体的位置就是相机位置了*/ transform1.position = _cameraRotation * new Vector3(0, 0, -_distance) + _targetPosition;
transform1.position = _cameraRotation * new Vector3(0, 0, -_distance) + _targetPosition; //设置相机位置
// 获得主摄像机
_mainCamera = Camera.main;
} }
private void Update() private void Update()
@ -65,7 +67,7 @@ private void Update()
} }
else if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能 else if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能
{ {
if (_distance >= MouseZoomMin && _distance <= MouseZoomMax) if (_distance is >= MouseZoomMin and <= MouseZoomMax)
{ {
_distance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity; _distance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
} }
@ -91,12 +93,11 @@ private void Update()
var transform1 = transform; var transform1 = transform;
_cameraX = transform1.right; _cameraX = transform1.right;
_cameraY = transform1.up; _cameraY = transform1.up;
_cameraZ = transform1.forward;
_initScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _targetOnScreenPosition.z); _initScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _targetOnScreenPosition.z);
//targetOnScreenPosition.z为目标物体到相机平面的法线距离 //targetOnScreenPosition.z为目标物体到相机平面的法线距离
_targetOnScreenPosition = Camera.main!.WorldToScreenPoint(_targetPosition); _targetOnScreenPosition = _mainCamera.WorldToScreenPoint(_targetPosition);
_initPosition = _targetPosition; _initPosition = _targetPosition;
} }
@ -117,7 +118,6 @@ private void Update()
if (Input.GetMouseButtonUp(2)) if (Input.GetMouseButtonUp(2))
{ {
Debug.Log("upOnce");
//平移结束把cameraTargetPosition的位置更新一下不然会影响缩放与旋转功能 //平移结束把cameraTargetPosition的位置更新一下不然会影响缩放与旋转功能
_targetPosition = target.position; _targetPosition = target.position;
} }