• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

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

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1103/157.html

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


两种方法可以实现单指滑动屏幕旋转模型~请听我们常州游戏开发培训工作室幻天网络细细道来~哈哈

 
方法一:



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);
                }
            }
        }
 
    }
}
 
以上。


 



上篇:上一篇:Unity3d游戏开发设计模式之子类沙盒模式
下篇:下一篇:Unity判断手势的滑动方向,单点触摸和多点触摸,