Unity 安卓屏幕触碰 旋转、移动以及缩放功能

Unity 安卓屏幕触碰 旋转、移动以及缩放功能

Unity 安卓屏幕触碰 旋转、移动以及缩放

引用资料:
1.https://blog.csdn.net/m0_37184457/article/details/78720228
2.http://blog.sina.com.cn/s/blog_54d4af0e0102x20i.html

代码很简单没有难度,随便 康一康 就会了。
RotationOrMove 和 BackPosition需要自己搭载使用。
就酱,与君共勉。

Unity 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 安卓控制器
/// </summary>

public class ControllerBehavior_ZH : MonoBehaviour
{
    private Touch _OldTouch1;  //上次触摸点1(手指1)  
    private Touch _OldTouch2;  //上次触摸点2(手指2)  

    // 记录手指触屏的位置  
    Vector2 _M_Screenpos = new Vector2();

    //旋转还是移动布尔
    bool _bMoveOrRotation;

    //相机初始位置
    Vector3 _OldPosition;

    void Start()
    {
        //记录开始摄像机的Position
        _OldPosition = Camera.main.transform.position;
    }
    void Update()
    {
        //没有触摸  
        if (Input.touchCount <= 0)
        {
            return;
        }

        //单点触摸   
        if (1 == Input.touchCount)
        {
            //如果为 True 单指操作为旋转  如果为 Fals 单指操作为移动
            if (_bMoveOrRotation)
            {
                //水平上下旋转
                Touch _Touch = Input.GetTouch(0);
                Vector2 _DeltaPos = _Touch.deltaPosition;
                transform.Rotate(Vector3.down * _DeltaPos.x, Space.World);
                transform.Rotate(Vector3.right * _DeltaPos.y, Space.World);
            }
            else
            {
                //移动

                if (Input.touches[0].phase == TouchPhase.Began)
                {
                    // 记录手指触屏的位置  
                    _M_Screenpos = Input.touches[0].position;

                }
                // 手指移动  
                else if (Input.touches[0].phase == TouchPhase.Moved)
                {

                    // 移动摄像机  
                    Camera.main.transform.Translate(new Vector3(-Input.touches[0].deltaPosition.x * Time.deltaTime * 0.1f, -Input.touches[0].deltaPosition.y * Time.deltaTime * 0.1f, 0));
                }
            }
        }

        //多点触摸, 放大缩小  
        Touch _NewTouch1 = Input.GetTouch(0);
        Touch _NewTouch2 = Input.GetTouch(1);

        //第2点刚开始接触屏幕, 只记录,不做处理  
        if (_NewTouch2.phase == TouchPhase.Began)
        {
            _OldTouch2 = _NewTouch2;
            _OldTouch1 = _NewTouch1;
            return;
        }

        //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型  
        float _OldDistance = Vector2.Distance(_OldTouch1.position, _OldTouch2.position);
        float _NewDistance = Vector2.Distance(_NewTouch1.position, _NewTouch2.position);

        //两个距离之差,为正表示放大手势, 为负表示缩小手势  
        float _Offset = _NewDistance - _OldDistance;

        //放大因子, 一个像素按 0.01倍来算(100可调整)  
        float _ScaleFactor = _Offset / 100f;
        Vector3 _LocalScale = transform.localScale;
        Vector3 _Scale = new Vector3(_LocalScale.x + _ScaleFactor,
                               _LocalScale.y + _ScaleFactor,
                               _LocalScale.z + _ScaleFactor);

        //最小缩放到 0.3 倍  
        if (_Scale.x > 0.3f && _Scale.y > 0.3f && _Scale.z > 0.3f)
        {
            transform.localScale = _Scale;
        }

        //记住最新的触摸点,下次使用  
        _OldTouch1 = _NewTouch1;
        _OldTouch2 = _NewTouch2;
    }


    //通过按钮让物体回到最初的位置
    public void BackPosition()
    {
        //位置回归原点
        Camera.main.transform.position = _OldPosition;
        //旋转归零
        Camera.main.transform.eulerAngles = Vector3.zero;
    }

    //设置单指操作方式 旋转还是移动
    public void RotationOrMove()
    {
        _bMoveOrRotation = !_bMoveOrRotation;
    }
}

  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现在 Unity 中使用鼠标控制物体的旋转移动缩放,你可以使用以下示例代码: ```csharp using UnityEngine; public class MouseControl : MonoBehaviour { private Vector3 mouseOrigin; private bool isRotating; private bool isMoving; private bool isScaling; void Update() { // 鼠标右键按下时旋转物体 if (Input.GetMouseButtonDown(1)) { mouseOrigin = Input.mousePosition; isRotating = true; } // 鼠标右键松开时停止旋转 if (Input.GetMouseButtonUp(1)) { isRotating = false; } // 鼠标中键按下时移动物体 if (Input.GetMouseButtonDown(2)) { mouseOrigin = Input.mousePosition; isMoving = true; } // 鼠标中键松开时停止移动 if (Input.GetMouseButtonUp(2)) { isMoving = false; } // 鼠标滚轮滚动时缩放物体 float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll != 0f) { isScaling = true; } else { isScaling = false; } if (isRotating) { // 根据鼠标的移动旋转物体 Vector3 mouseOffset = (Input.mousePosition - mouseOrigin) * 0.1f; transform.Rotate(mouseOffset.y, -mouseOffset.x, 0, Space.World); mouseOrigin = Input.mousePosition; } if (isMoving) { // 根据鼠标的移动移动物体 Vector3 mouseOffset = (Input.mousePosition - mouseOrigin) * 0.01f; transform.Translate(mouseOffset.x, mouseOffset.y, 0, Space.World); mouseOrigin = Input.mousePosition; } if (isScaling) { // 根据鼠标滚轮的滚动来缩放物体 float scaleAmount = 1f + scroll; transform.localScale *= scaleAmount; } } } ``` 将以上代码添加到一个物体上,然后在场景中将需要控制的物体作为该物体的子物体。在运行游戏时,你可以使用鼠标右键来旋转物体,使用鼠标中键来移动物体,使用鼠标滚轮来缩放物体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maddie_Mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值