最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity 如何判斷鼠標(biāo)是否在哪個UI上(兩種方法)

 更新時間:2021年04月10日 15:14:02   作者:玉速林瘋  
這篇文章主要介紹了unity 判斷鼠標(biāo)是否在哪個UI上的兩種實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

第一種

可以得到UI,再根據(jù)名字判斷是不是自己自己要點擊的UI

其中參數(shù)canvas拖入此UI的canvas

 /// <summary>
        /// 獲取鼠標(biāo)停留處UI
        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public GameObject GetOverUI(GameObject canvas)
        {
            PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
            pointerEventData.position = Input.mousePosition;
            GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
            List<RaycastResult> results = new List<RaycastResult>();
            gr.Raycast(pointerEventData, results);
            if (results.Count != 0)
            {
                return results[0].gameObject;
            }
            return null;
        }

第二種就簡單了

rect 為要判斷的那個UI的RectTransform

bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)

補(bǔ)充:Unity中判斷鼠標(biāo)或者手指是否點擊在UI上(UGUI)

在Unity場景中,有時UI和游戲角色都需要響應(yīng)觸摸事件,如果同時響應(yīng)可能就會出現(xiàn)點擊UI的時候影響到了游戲角色。所以我們需要對所點擊到的東西做判斷,這里使用UGUI系統(tǒng)自帶的方法和射線檢測的方式,判斷是否點擊到UI上:

第一種方法,直接在Update中判斷:

void Update()
    {      
        //判斷是否點擊UI
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
            //移動端
            if (Application.platform == RuntimePlatform.Android ||
                        Application.platform == RuntimePlatform.IPhonePlayer)
            {
                int fingerId = Input.GetTouch(0).fingerId;
                if (EventSystem.current.IsPointerOverGameObject(fingerId))
                {
                    Debug.Log("點擊到UI");                    
                }
            }
            //其它平臺
            else
            {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("點擊到UI");                    
                }
            }
        }

第二種方式:射線檢測

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; 
public class NewBehaviourScript : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //移動端
        if (Application.platform == RuntimePlatform.Android ||
                    Application.platform == RuntimePlatform.IPhonePlayer)
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (IsPointerOverGameObject(Input.GetTouch(0).position))
                {
                    Debug.Log("點擊到UI");
                }
            }            
        }
        //其它平臺
        else
        {
            if(Input.GetMouseButtonDown(0))
            {
                if (IsPointerOverGameObject(Input.mousePosition))
                {
                    Debug.Log("點擊到UI");
                }
            }            
        }
    }
 
    /// <summary>
    /// 檢測是否點擊UI
    /// </summary>
    /// <param name="mousePosition"></param>
    /// <returns></returns>
    private bool IsPointerOverGameObject(Vector2 mousePosition)
    {       
        //創(chuàng)建一個點擊事件
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = mousePosition;
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        //向點擊位置發(fā)射一條射線,檢測是否點擊UI
        EventSystem.current.RaycastAll(eventData, raycastResults);
        if (raycastResults.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • WPF ComboBox獲取當(dāng)前選擇值的實例詳解

    WPF ComboBox獲取當(dāng)前選擇值的實例詳解

    這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • DevExpress實現(xiàn)GridControl單元格編輯驗證的方法

    DevExpress實現(xiàn)GridControl單元格編輯驗證的方法

    這篇文章主要介紹了DevExpress實現(xiàn)GridControl單元格編輯驗證的方法,很實用的功能,需要的朋友可以參考下
    2014-08-08
  • c#基于Redis實現(xiàn)輕量級消息組件的步驟

    c#基于Redis實現(xiàn)輕量級消息組件的步驟

    這篇文章主要介紹了c#基于Redis實現(xiàn)輕量級消息組件的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#進(jìn)行開發(fā),感興趣的朋友可以了解下
    2021-05-05
  • C#實現(xiàn)TreeView節(jié)點拖拽的方法

    C#實現(xiàn)TreeView節(jié)點拖拽的方法

    這篇文章主要介紹了C#實現(xiàn)TreeView節(jié)點拖拽的方法,涉及C#針對TreeView節(jié)點的動態(tài)添加及移除技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#實現(xiàn)給圖片添加日期信息的示例詳解

    C#實現(xiàn)給圖片添加日期信息的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)給圖片添加日期信息,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • c#中token的使用方法實例

    c#中token的使用方法實例

    本文主要介紹了c#中token的使用方法實例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Unity3d實現(xiàn)Flappy Bird游戲

    Unity3d實現(xiàn)Flappy Bird游戲

    這篇文章主要為大家詳細(xì)介紹了Unity3d實現(xiàn)Flappy Bird游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 驗證碼的三個常見漏洞和修復(fù)方法

    驗證碼的三個常見漏洞和修復(fù)方法

    這篇文章主要介紹了驗證碼的三個常見漏洞和修復(fù)方法,本文講解了把驗證碼存儲在Cookie中、沒有進(jìn)行非空判斷、沒有及時銷毀驗證碼三個常見問題和解決方法,需要的朋友可以參考下
    2015-03-03
  • 使用winapi安裝Windows服務(wù)示例程序

    使用winapi安裝Windows服務(wù)示例程序

    這篇文章主要介紹了使用winapi安裝Windows服務(wù)示例,大家參考使用吧
    2014-01-01
  • C#中Mutex對象用法分析

    C#中Mutex對象用法分析

    這篇文章主要介紹了C#中Mutex對象用法,結(jié)合實例形式分析了Mutex對象的功能與線程操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06

最新評論

财经| 广河县| 疏勒县| 石台县| 安宁市| 五峰| 汽车| 陇西县| 凤山市| 瑞丽市| 澄城县| 安国市| 罗甸县| 兴文县| 丹阳市| 家居| 射洪县| 内黄县| 新野县| 广灵县| 子洲县| 宜宾县| 大田县| 鄂托克旗| 闻喜县| 巫溪县| 黎城县| 揭东县| 襄垣县| 武陟县| 麻阳| 横峰县| 锡林浩特市| 双城市| 宣汉县| 新和县| 松阳县| 靖远县| 武宁县| 新干县| 大悟县|