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

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

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

第一種

可以得到UI,再根據(jù)名字判斷是不是自己自己要點(diǎn)擊的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 為要判斷的那個(gè)UI的RectTransform

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

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

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

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

void Update()
    {      
        //判斷是否點(diǎn)擊UI
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
            //移動(dòng)端
            if (Application.platform == RuntimePlatform.Android ||
                        Application.platform == RuntimePlatform.IPhonePlayer)
            {
                int fingerId = Input.GetTouch(0).fingerId;
                if (EventSystem.current.IsPointerOverGameObject(fingerId))
                {
                    Debug.Log("點(diǎn)擊到UI");                    
                }
            }
            //其它平臺
            else
            {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("點(diǎn)擊到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()
    {
        //移動(dòng)端
        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("點(diǎn)擊到UI");
                }
            }            
        }
        //其它平臺
        else
        {
            if(Input.GetMouseButtonDown(0))
            {
                if (IsPointerOverGameObject(Input.mousePosition))
                {
                    Debug.Log("點(diǎn)擊到UI");
                }
            }            
        }
    }
 
    /// <summary>
    /// 檢測是否點(diǎn)擊UI
    /// </summary>
    /// <param name="mousePosition"></param>
    /// <returns></returns>
    private bool IsPointerOverGameObject(Vector2 mousePosition)
    {       
        //創(chuàng)建一個(gè)點(diǎn)擊事件
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = mousePosition;
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        //向點(diǎn)擊位置發(fā)射一條射線,檢測是否點(diǎn)擊UI
        EventSystem.current.RaycastAll(eventData, raycastResults);
        if (raycastResults.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

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

相關(guān)文章

最新評論

怀化市| 辽源市| 武宣县| 兰考县| 汕尾市| 叶城县| 林芝县| 大埔区| 达尔| 和静县| 和田市| 北碚区| 湄潭县| 龙里县| 香格里拉县| 南郑县| 醴陵市| 屏南县| 津南区| 江北区| 龙泉市| 武乡县| 黄平县| 全椒县| 屏东市| 甘孜| 郸城县| 通州市| 泸西县| 杭州市| 册亨县| 和顺县| 高州市| 调兵山市| 汾西县| 内黄县| 香格里拉县| 冷水江市| 庆元县| 雅江县| 延安市|