Unity实现仿编辑器Scene视图自由相机(平移/旋转/缩放/平滑移动)

简介

做Unity项目开发、Demo演示、场景漫游功能时,原生默认相机操控非常僵硬,没有编辑器Scene视图丝滑的移动、旋转、滚轮缩放、中键平移效果。

今天给大家分享一款高度还原Unity编辑器Scene视图的自由相机脚本,完美复刻官方编辑器相机手感,支持:右键视角旋转、WASDQE全方位移动、Shift加速、鼠标中键平移、滚轮缩放、平滑阻尼加速减速,零BUG、开箱即用,可直接用于游戏演示、场景漫游、编辑器预览功能。

**适用场景:**游戏场景漫游、Demo演示相机、自定义编辑器预览、3D沙盘操控、离线展示相机


一、功能亮点

  • 极致还原官方Scene相机手感,带平滑加速、阻尼减速效果,无卡顿、无瞬移

  • ✅ 鼠标右键拖拽旋转视角,限制俯仰角度,避免视角翻转

  • ✅ WASD前后左右移动、QE上下垂直移动,全方位自由漫游

  • ✅ Shift按键倍速加速移动,快速浏览大场景

  • ✅ 鼠标中键拖拽平移,精准拖动场景视角

  • ✅ 鼠标滚轮远近缩放,自适应视角拉近拉远

  • ✅ 全部参数公开可调节,速度、灵敏度、阻尼自定义

  • ✅ 挂载即用,无需任何配置,兼容所有3D项目


二、完整源码(可直接复制使用)

新建脚本 UnitySceneCamera.cs,将以下代码完整粘贴:

cs 复制代码
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class UnitySceneCamera : MonoBehaviour
{
    [Header("移动速度")]
    public float moveSpeed = 8f;
    public float shiftMultiplier = 2.5f;

    [Header("旋转灵敏度")]
    public float lookSensitivity = 3f;

    [Header("滚轮缩放")]
    public float zoomSpeed = 10f;

    [Header("中键平移")]
    public float panSpeed = 0.1f;

    [Header("平滑阻尼(模仿Unity Scene)")]
    public float moveAcceleration = 8f;
    public float moveDamping = 10f;

    private float _pitch;
    private float _yaw;
    private Vector3 _moveVelocity;
    private Vector3 _targetMoveDir;

    void Start()
    {
        // 初始化相机初始旋转角度
        Vector3 euler = transform.eulerAngles;
        _pitch = euler.x;
        _yaw = euler.y;
    }

    void Update()
    {
        // 右键按住:旋转视角 + WASD移动
        if (Input.GetMouseButton(1))
        {
            DoRotation();
            UpdateMoveInput();
        }
        else
        {
            // 松开右键,清空移动目标,触发阻尼减速
            _targetMoveDir = Vector3.zero;
        }

        // 执行平滑移动逻辑
        DoMovementSmooth();

        // 鼠标中键平移
        if (Input.GetMouseButton(2))
        {
            DoPanning();
        }

        // 滚轮缩放
        DoZoom();
    }

    /// <summary>
    /// 视角旋转逻辑(右键拖拽)
    /// </summary>
    void DoRotation()
    {
        float mouseX = Input.GetAxis("Mouse X") * lookSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * lookSensitivity;

        // 水平旋转、垂直俯仰
        _yaw += mouseX;
        _pitch -= mouseY;
        
        // 限制俯仰角度,防止视角翻转卡死
        _pitch = Mathf.Clamp(_pitch, -89f, 89f);

        // 赋值相机旋转
        transform.rotation = Quaternion.Euler(_pitch, _yaw, 0f);
    }

    /// <summary>
    /// 更新WASD/QE移动输入
    /// </summary>
    void UpdateMoveInput()
    {
        // Shift加速倍率
        float speed = moveSpeed;
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            speed *= shiftMultiplier;

        Vector3 dir = Vector3.zero;

        // 前后左右移动(基于相机自身朝向)
        if (Input.GetKey(KeyCode.W)) dir += transform.forward;
        if (Input.GetKey(KeyCode.S)) dir -= transform.forward;
        if (Input.GetKey(KeyCode.A)) dir -= transform.right;
        if (Input.GetKey(KeyCode.D)) dir += transform.right;

        // 上下垂直移动(世界空间上下)
        if (Input.GetKey(KeyCode.E)) dir += Vector3.up;
        if (Input.GetKey(KeyCode.Q)) dir -= Vector3.up;

        // 归一化防止斜向移动速度过快
        if (dir.magnitude > 0.001f)
            dir.Normalize();

        _targetMoveDir = dir * speed;
    }

    /// <summary>
    /// 平滑加速+阻尼减速移动核心
    /// </summary>
    void DoMovementSmooth()
    {
        // 渐进加速:向目标移动速度插值
        _moveVelocity = Vector3.Lerp(_moveVelocity, _targetMoveDir, moveAcceleration * Time.deltaTime);
        
        // 松开按键后渐进阻尼减速,模拟惯性手感
        if (_targetMoveDir.magnitude < 0.001f)
            _moveVelocity = Vector3.Lerp(_moveVelocity, Vector3.zero, moveDamping * Time.deltaTime);

        // 执行移动(世界空间)
        transform.Translate(_moveVelocity * Time.deltaTime, Space.World);
    }

    /// <summary>
    /// 滚轮缩放相机远近
    /// </summary>
    void DoZoom()
    {
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (Mathf.Abs(scroll) > 0.001f)
        {
            transform.Translate(0f, 0f, scroll * zoomSpeed, Space.Self);
        }
    }

    /// <summary>
    /// 鼠标中键平移视角
    /// </summary>
    void DoPanning()
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        // 反向平移,贴合官方Scene视图手感
        Vector3 pan = (-transform.right * mouseX + -transform.up * mouseY) * panSpeed;
        transform.Translate(pan, Space.World);
    }
}

三、参数详细说明(Inspector可视化调节)

脚本挂载后,在Inspector面板可直接调整所有参数,无需改代码:

1. 移动速度参数

  • moveSpeed:基础移动速度,默认8,适配绝大多数场景

  • shiftMultiplier:Shift加速倍率,默认2.5倍,大场景快速漫游必备

2. 旋转参数

  • lookSensitivity:鼠标旋转灵敏度,数值越大转动越快,默认3

3. 缩放参数

  • zoomSpeed:滚轮缩放速度,默认10,可根据场景大小微调

4. 平移参数

  • panSpeed:中键拖拽平移速度,默认0.1,手感细腻不飘

5. 平滑阻尼核心参数(决定手感)

  • moveAcceleration:移动加速度,数值越大起步越快,默认8

  • moveDamping:停止阻尼系数,数值越大停下越干脆,默认10,完美复刻官方惯性手感


四、操作按键说明(完全复刻Unity编辑器)

操作按键 功能说明
鼠标右键按住 拖拽旋转相机视角,同时激活WASDQE移动
W/S 相机向前、向后移动
A/D 相机向左、向右平移
Q/E 相机向上、向下垂直移动
LeftShift+移动 加速移动,适合大范围场景浏览
鼠标中键按住拖拽 整体平移场景视角
鼠标滚轮 相机拉近、拉远缩放

五、使用步骤(超简单)

  1. 打开Unity项目,在Project窗口右键 -> Create -> C# Script,命名为 UnitySceneCamera

  2. 双击打开脚本,删除默认代码,粘贴上方完整源码并保存

  3. 将脚本挂载到场景中的MainCamera主相机上

  4. 直接运行游戏,即可使用所有Scene视图同款操控功能

提示: 脚本添加了[RequireComponent(typeof(Camera))] 特性,挂载对象会自动绑定Camera组件,无需手动添加,避免报错。