人人射人人插 I 91成人综合 I 熟女视频一区二区在线观看 I 91 在线观看 I 欧美成人乱码一二三四区免费 I av在线网页 I 日韩aⅴ视频 I 女人高潮抽搐潮喷视频开腿 I 大陆日韩欧美 I 国产麻豆精品福利在线观看 I 国产在线精品一区二区在线观看 I 青草网在线观看 I 国产精品夜夜夜 I 色婷婷六月 I 亚洲第一天堂在线观看 I 国产午夜激无码av毛片不卡 I 精品乱码一卡二卡四卡 I 永久免费看片在线 I av影片网站 I xfplay噜噜av I 91精品国产91久久久久福利 I 免费能看的av I 国产成人美女视频 I 久久国产精品99精国产 I 久久久久国产成人免费精品免费 I 国产精品va I 久久国产精品成人影院 I 国产女人爽到高潮久久久4444 I 狠狠亚洲 I 野外做受三级视频 I 久久婷婷国产综合精品 I 色婷婷综合视频在线观看 I 成人瑟瑟视频 I 国产视频久久网 I 亚洲无人区一区

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

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

來源:奇酷教育 發表于:

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

        鄭州unity3d培訓 UGUI長按監測的兩種方法,奇酷(m.xrpc.com.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就失效了,還是用上面兩種方法比較通。