CheckCanvas.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CheckCanvas : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. }
  10. //上下旋转最大角度限制
  11. public int yMinLimit = -20;
  12. public int yMaxLimit = 80;
  13. //旋转速度
  14. public float xSpeed = 250.0f;//左右旋转速度
  15. public float ySpeed = 120.0f;//上下旋转速度
  16. //旋转角度
  17. private float x = 0.0f;
  18. private float y = 0.0f;
  19. //缩放比例限制
  20. public float MinScale = 0.2f;
  21. public float MaxScale = 3.0f;
  22. //缩放比例
  23. private float scale = 1.0f;
  24. void Update()
  25. {
  26. if (Input.GetMouseButton(0))
  27. {
  28. //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
  29. x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  30. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  31. y = ClampAngle(y, yMinLimit, yMaxLimit);
  32. //欧拉角转化为四元数
  33. Quaternion rotation = Quaternion.Euler(-y, -x, 0);
  34. transform.rotation = rotation;
  35. }
  36. if (Input.GetAxis("Mouse ScrollWheel") != 0)
  37. {
  38. scale += Input.GetAxis("Mouse ScrollWheel");
  39. scale = Mathf.Clamp(scale, MinScale, MaxScale);
  40. transform.localScale = new Vector3(scale, scale, scale);
  41. }
  42. }
  43. //角度范围值限定
  44. static float ClampAngle(float angle, float min, float max)
  45. {
  46. if (angle < -360)
  47. angle += 360;
  48. if (angle > 360)
  49. angle -= 360;
  50. return Mathf.Clamp(angle, min, max);
  51. }
  52. }