七月婷婷在线视频综合-奇米77-奇米狠狠干-奇米久草-国内精品久久影视免费-国内精品免费视频自在线

您現在所在的位置:首頁 >關于奇酷 > 行業動態 > 鄭州unity3d培訓 UGUI長按監測的兩種方法

鄭州unity3d培訓 UGUI長按監測的兩種方法

來源:奇酷教育 發表于:

鄭州unity3d培訓 UGUI長按監測的兩種方法,奇酷(www qikuedu com)老師總結兩種辦法如下: 簡單的demo,隨便建幾個UI,把

        鄭州unity3d培訓 UGUI長按監測的兩種方法,奇酷(www.gp5698.cn)老師總結兩種辦法如下:
        簡單的demo,隨便建幾個UI,把腳本拖到任意物體,按1秒鐘后有響應事件。以下腳本可避免ScrollView失效,以及重疊UI穿透選擇。
鄭州unity3d培訓,Z???VR1VJ" src="http://uploadfile.qikuedu.com/2019/0319/20190319110022305.jpg" style="width: 480px; height: 270px;" />
方法一:使用EventSystems,適合場景中簡單的UI操作
using UnityEngine;
using UnityEngine.EventSystems;
 
public class RyanPressTest : MonoBehaviour {
        Vector3 lastMousePose;
    EventSystem m_EventSystem;
        float curT = 0;
        // 是否已經被選擇
        bool isPressed = false;
        void Start(){
        m_EventSystem = FindObjectOfType();
        }
        void Update () {
                if(Input.GetMouseButtonDown(0)){
                        lastMousePose = Input.mousePosition;
                }
        if (Input.GetMouseButton(0) && !isPressed && lastMousePose == Input.mousePosition)
        {
                        curT += Time.deltaTime;
                        // 長按1秒
                        if(curT >= 1f){
                    Debug.Log(m_EventSystem.currentSelectedGameObject + " was pressed.");
                                isPressed = true;
                        }
        }
                if(Input.GetMouseButtonUp(0)){
                        isPressed = false;
                        curT = 0;
                }
        }
}
 
方法二:使用射線,適用于鼠標一下選擇多個UI的復雜場景
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class RyanPressTest : MonoBehaviour {
        Vector3 lastMousePose;
    GraphicRaycaster m_Raycaster;
    PointerEventData m_PointerEventData;
        float curT = 0;
        // 是否已經被選擇
        bool isPressed = false;
        void Start(){
        m_Raycaster = FindObjectOfType ();
        }
        void Update () {
                if(Input.GetMouseButtonDown(0)){
                        lastMousePose = Input.mousePosition;
                }
        if (Input.GetMouseButton(0) && !isPressed && lastMousePose == Input.mousePosition)
        {
                        curT += Time.deltaTime;
            m_PointerEventData = new PointerEventData(null);
                        m_PointerEventData.position = lastMousePose;
 
            List results = new List();
            m_Raycaster.Raycast(m_PointerEventData, results);
                        // 長按1秒
                        if(results.Count > 0 && curT >= 1f){
                                // 當有多個重疊UI,results會返回所有被射線穿透的UI數組,一般我們只需要最上面的那個UI
                    Debug.Log(results[0].gameObject + " was pressed.");
                                isPressed = true;
                        }
        }
                if(Input.GetMouseButtonUp(0)){
                        isPressed = false;
                        curT = 0;
                }
        }
}
 
其實EventTrigger也能監測長按,但用了它ScrollView就失效了,還是用上面兩種方法比較通。
主站蜘蛛池模板: 二区在线观看 | 久热这里只精品热在线观看 | 又色又爽又黄的网站 | 人人爽人人拍 | 成人毛片100部免费看 | 在线国产欧美 | 18级成人毛片免费观看 | 最近2019中文免费字幕 | 日本一区二区三区中文字幕 | 视频黄色免费 | 91日韩欧美| 999影院成 人在线影院 | 成人午夜视频在线观看 | 黄色一级毛片在线观看 | 国产一极毛片 | 女女同性一区二区三区四区 | 欧美特黄三级在线观看 | 一级做a爱片特黄在线观看免费看 | 在线日韩欧美 | 人人爽人人爱 | 在线观看亚洲精品专区 | 狠狠成人 | ww7788色淫网站女女免费 | 波多野结衣一区免费作品 | 久久综合九色综合97_ 久久久 | 日本与大黑人xxxx | 精品国产欧美 | 国产精品videossex白浆 | 天天做天天爱夜夜爽毛片毛片 | 国产亚洲高清不卡在线观看 | 91精品国产美女福到在线不卡 | 99视频久久| 亚洲国产一级毛片 | 日韩成人精品视频 | 久久综合视频网站 | 激情综合网五月 | 五月天婷婷免费观看视频在线 | 99精品在线| 日日摸碰夜夜爽 | 成人欧美日韩高清不卡 | 成人爽爽激情在线观看 |