滑動屏幕旋轉模型功能實現

兩種方法可以實現單指滑動屏幕旋轉模型~請聽我細細道來~哈哈大笑


方法一:


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);
                }
            }
        }

    }
}
 

以上。


發佈了39 篇原創文章 · 獲贊 85 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章