Unity中实现Scene模式下的鼠标操作效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CameraAction : MonoBehaviour {
 
    
    public float mouseScrollSpeed = 40, //滚滚轮放大缩小速度
        mouseDragSpeed1 = 10, //单单按右键旋转的速度
        mouseDragSpeed2 = 1.26f;//按中键移动的速度
    public Vector3 mouseLastPosition = new Vector3(0, 0, 0), //“上一次”光标的位置
        mousePositionDelta = new Vector3(0, 0, 0);//光标位置变化量,等于现在光标位置减上一次光标的位置
    Vector3 rotateDelta = new Vector3(0, 0, 0);//单单按鼠标右键旋转相机的速度
    public Texture2D m_texture2D1, m_texture2D2, m_texture2D3, m_texture2D4;//光标图案
 
    private void FixedUpdate()
    {
        MouseEvents();
    }
    void MouseEvents()
    {
        //按住鼠标右键拖动
        if (Input.GetMouseButton(1))
        {
            mousePositionDelta = Input.mousePosition - mouseLastPosition;
            mouseLastPosition = Input.mousePosition;
            if (mousePositionDelta.magnitude != 0)
            {
                rotateDelta = new Vector3(-mousePositionDelta.y * Time.deltaTime * mouseDragSpeed1,
                mousePositionDelta.x * Time.deltaTime * mouseDragSpeed1, 0);
                //按Alt+鼠标右键
                if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
                {
                    this.transform.Translate(new Vector3(0, 0, Time.deltaTime * mouseScrollSpeed * (rotateDelta.x + rotateDelta.y)), Space.Self);
                    Cursor.SetCursor(m_texture2D4, new Vector2(5, 5), CursorMode.ForceSoftware);
                }
                //单单按鼠标右键
                else
                {
                    transform.eulerAngles += rotateDelta;
                    Cursor.SetCursor(m_texture2D1, new Vector2(5, 5), CursorMode.ForceSoftware);
                }
            }
        }
        //按住鼠标中键拖动
        else if (Input.GetMouseButton(2))
        {
            mousePositionDelta = Input.mousePosition - mouseLastPosition;
            mouseLastPosition = Input.mousePosition;
            if (mousePositionDelta.magnitude != 0)
            {
                rotateDelta = new Vector3(-mousePositionDelta.x * Time.deltaTime * mouseDragSpeed2,
                  -mousePositionDelta.y * Time.deltaTime * mouseDragSpeed2, 0);
                transform.Translate(rotateDelta, Space.Self);
 
                Cursor.SetCursor(m_texture2D3, new Vector2(5, 5), CursorMode.ForceSoftware);
            }
        }
        //如果没有按鼠标除了滚滚轮的键,则实时更新光标“上一次”的位置mouseLastPosition
        else
        {
            mouseLastPosition = Input.mousePosition;//重新赋初值
            Cursor.SetCursor(m_texture2D2, new Vector2(5, 5), CursorMode.ForceSoftware);//更改鼠标图标
        }
        //滚轮——放大缩小
        if (Input.mouseScrollDelta.y != 0)//滚轮滚了多少
            transform.Translate(new Vector3(0, 0, Time.deltaTime * mouseScrollSpeed * Input.mouseScrollDelta.y), Space.Self);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值