蜜桃精品成人影片_99热在线精品免费_日韩亚洲中字无码一区二区三区_亚洲中文字幕久久精品无码一区

您現(xiàn)在所在的位置:首頁 >關(guān)于奇酷 > 行業(yè)動態(tài) > 鄭州unity3d培訓(xùn) UGUI長按監(jiān)測的兩種方法

鄭州unity3d培訓(xùn) UGUI長按監(jiān)測的兩種方法

來源:奇酷教育 發(fā)表于:

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

        鄭州unity3d培訓(xùn) UGUI長按監(jiān)測的兩種方法,奇酷(www.545318.com)老師總結(jié)兩種辦法如下:
        簡單的demo,隨便建幾個UI,把腳本拖到任意物體,按1秒鐘后有響應(yīng)事件。以下腳本可避免ScrollView失效,以及重疊UI穿透選擇。
鄭州unity3d培訓(xùn),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;
        // 是否已經(jīng)被選擇
        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;
                }
        }
}
 
方法二:使用射線,適用于鼠標(biāo)一下選擇多個UI的復(fù)雜場景
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
public class RyanPressTest : MonoBehaviour {
        Vector3 lastMousePose;
    GraphicRaycaster m_Raycaster;
    PointerEventData m_PointerEventData;
        float curT = 0;
        // 是否已經(jīng)被選擇
        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){
                                // 當(dāng)有多個重疊UI,results會返回所有被射線穿透的UI數(shù)組,一般我們只需要最上面的那個UI
                    Debug.Log(results[0].gameObject + " was pressed.");
                                isPressed = true;
                        }
        }
                if(Input.GetMouseButtonUp(0)){
                        isPressed = false;
                        curT = 0;
                }
        }
}
 
其實(shí)EventTrigger也能監(jiān)測長按,但用了它ScrollView就失效了,還是用上面兩種方法比較通。
建水县| 阜平县| 永靖县| 元朗区| 南丹县| 友谊县| 赞皇县| 渭南市| 托克托县| 丹棱县| 桃园县| 定陶县| 河间市| 股票| 霍山县| 三河市| 鸡西市| 三穗县| 宁海县| 互助| 屏东市| 峨眉山市| 老河口市| 建平县| 璧山县| 德令哈市| 高唐县| 凯里市| 信宜市| 龙泉市| 璧山县| 玉门市| 舒兰市| 资源县| 辛集市| 大荔县| 桐庐县| 漠河县| 襄城县| 张家港市| 安图县|