七月婷婷在线视频综合-奇米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就失效了,還是用上面兩種方法比較通。
主站蜘蛛池模板: 中文字幕日韩精品在线 | 欧美乱大交xxxxxbbb | 一级女性全黄生活片免费看 | 99色网| 色秀影院 | 中文字幕人成乱码在线观看 | 免费国产小视频在线观看 | 国产成人综合在线观看 | 做a的视频免费 | 一级毛片免费全部播放 | 视频在线18羞羞 | 国产a∨一区二区三区香蕉小说 | 99久久精品国产亚洲 | 成人免费视频一区二区三区 | 中文字幕激情视频 | 999精品国产| 日批视频在线免费看 | 成人午夜精品视频在线观看 | 日本高清一区二区三区不卡免费 | 一级做a爰片毛片 | 成年视频国产免费观看 | 在线观看日韩一区 | 国产成人精品三区 | 天天草夜夜骑 | 在线观看亚洲天堂 | 一级特黄录像播放 | 久久亚 | 九九精品在线播放 | 亚洲精品午夜在线观看 | 日韩理论在线 | 黄色片视频网 | 国内精品久久久久久久久野战 | 色天天综合色天天天天看大 | 国产一二三区视频 | 国产欧美一区二区三区在线 | 黄色片视频在线免费观看 | 日本三级唾液浓厚在线观看 | 天天夜夜爽 | 国产成人精品午夜 | 五月婷婷在线播放 | 一级做a爰片性色毛片小说 一级做a爰片性色毛片武则天五则 |