1、打开unity,新建一个工程,具体如下图
2、在场景中添加一个 Cube,效果如下图
3、在工程中,添加一个脚本FollowMouseScale,双击打开脚本进行编辑,具体如下图
4、FollowMouseScale 脚本具体代码和代码说明如下图FollowMouseScale
5、FollowMouseScale 脚本具体内容如下:using UnityEngine;public class FollowMous髫潋啜缅eScale : MonoBehaviour { public float scaleSpeed = 5.0f; private float minScale = 1.0f; private float maxScale = 150.0f; private float currentScale; // Use this for initialization void Start () { //根据当前摄像机是正交还是透视进行对应赋值 if (Camera.main.orthographic == true) { currentScale = Camera.main.orthographicSize; } else { currentScale = Camera.main.fieldOfView; } } // Update is called once per frame void Update () { //获取鼠标滚轮的值,向前大于0,向后小于0,并设置放大缩小范围值 currentScale += Input.GetAxis("Mouse ScrollWheel") * scaleSpeed; currentScale = Mathf.Clamp(currentScale, minScale, maxScale); //根据当前摄像机是正交还是透视进行对应赋值,放大缩小 if (Camera.main.orthographic == true) { Camera.main.orthographicSize = currentScale; } else { Camera.main.fieldOfView = currentScale; } }}
6、脚本编译正确,回到Unity,在场景中添加一个 GameObject,把脚本挂载上去,具体如下图
7、运行场景,前后滚动滚轮,摄像头视野相应地放大缩小,具体如下图