天天操天天拍-天天操天天射天天-天天操天天射天天操-天天操天天添-欧美乱码伦视频免费-欧美乱一级在线观看

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

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

來源:奇酷教育 發表于:

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

        鄭州unity3d培訓 UGUI長按監測的兩種方法,奇酷(www.15lily.com)老師總結兩種辦法如下:
        簡單的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就失效了,還是用上面兩種方法比較通。
主站蜘蛛池模板: 精品视频一区二区观看 | 国产综合精品一区二区 | 黄色四虎影院 | 久久一日本道色综合久久m 久久伊人精品综合观看99 | 国产九九视频在线观看 | 道日本一本草久 | 久久久精品国产免费观看同学 | 免费一级毛片不卡在线播放 | 国产精品永久免费视频 | 国产一级一极性活片 | 久久99国产精品成人欧美 | 大学生一级毛片 | 波多野结衣在线一区二区 | 国产三级毛片 | 两性午夜性刺激在线观看免费 | 国产精品久久亚洲不卡4k岛国 | 黄片毛片免费在线观看 | 免费a在线看 | 久久久久美女 | 国产视频高清在线观看 | 岛国片夜色影院055 道日本一本草久 | 国产精品福利一区二区 | 免费看片在线 | 韩国精品欧美一区二区三区 | 国产成人精品免费视频软件 | 国产成人免费网站在线观看 | 国产综合精品久久亚洲 | 精品精品国产高清a毛片 | 麻豆久久婷婷国产综合五月 | 国产在线麻豆一区二区 | 免费一级毛片不卡不收费 | 黄色网 在线播放 | 国产成人高清在线观看播放 | 国家一级毛片 | 久久精品久久精品 | 国产视频资源在线观看 | 精品三级内地国产在线观看 | 国产免费观看网站 | 久久影院国产 | 高清不卡一区二区 | 金发欧美一区在线观看 |