滑动屏幕旋转模型功能实现

两种方法可以实现单指滑动屏幕旋转模型~请听我细细道来~哈哈大笑


方法一:


1.0  首先添加一个模型。

1.1 给任何ui添加以下脚本。

    void OnGUI()
    {
            print("触摸中~");
            if (Event.current.type == EventType.MouseDown)
            {
                first = Event.current.mousePosition;
            }
            if (Event.current.type == EventType.MouseDrag)
            {
                second = Event.current.mousePosition;
                if (second.x < first.x)
                {
                    print("left");
                    WomanModel.transform.Rotate(Vector3.up * Time.deltaTime * 150);
                }
                if (second.x > first.x)
                {
                    print("right");
                    WomanModel.transform.Rotate(Vector3.up * Time.deltaTime * -150);
                }
            }
    }
即可实现往右滑动模型向右旋转,向左滑动模型向左旋转。


方法二:

2.1 创建一个透明Button,做的大一点,覆盖大半个屏幕,这个是针对某一块区域做得触屏检测。

如下图:



2.2 给button添加Event Triger组件。

如图:


2.3 给button绑定以下脚本,并绑定point enter和point exit事件。

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

public class test : MonoBehaviour {

    public GameObject WomanModel;
    bool isTouch = false;

    private Vector2 first = Vector2.zero;//鼠标按下的位置  
    private Vector2 second = Vector2.zero;//鼠标拖动的位置

    public void OnPointerDown()
    {
        isTouch = true;
        Debug.LogError("OnPointDown................");
    }
    public void OnPointerExit()
    {
        if (isTouch)
        {
            isTouch = false;
            Debug.LogError("OnPointerExit................");
        }
        
    }

    void OnMouseDown()
    {
        Debug.Log("OnMouseDown response");
        isTouch = true;
    }
    void OnMouseUp()
    {
        Debug.Log("OnMouseup response");
        isTouch = false;
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
           // Debug.Log("Input.GetMouseButtonDown response");
        }
    }


    void OnGUI()
    {
        if (Event.current != null && Event.current.type == EventType.mouseDown)
        {
            //Debug.Log("EventType.mouseDown response");
        }
        if (isTouch == true)
        {
            print("触摸中~");
            if (Event.current.type == EventType.MouseDown)
            {
                first = Event.current.mousePosition;
            }
            if (Event.current.type == EventType.MouseDrag)
            {
                second = Event.current.mousePosition;
                if (second.x < first.x)
                {
                    print("left");
                    WomanModel.transform.Rotate(Vector3.up * Time.deltaTime * 150);
                }
                if (second.x > first.x)
                {
                    print("right");
                    WomanModel.transform.Rotate(Vector3.up * Time.deltaTime * -150);
                }
            }
        }

    }
}
 

以上。


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值