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

Unity 如何獲取鼠標(biāo)停留位置下的物體

 更新時間:2021年04月12日 16:56:58   作者:末零  
這篇文章主要介紹了Unity 如何獲取鼠標(biāo)停留位置下的物體,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

根據(jù)UGUI的射線檢測機(jī)制獲取當(dāng)前鼠標(biāo)下的UI:

/// <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;
    }

其中,results為鼠標(biāo)下UI的列表。

不僅適用于UGUI,可以在攝像機(jī)上添加PhysicsRaycaster組件,傳參為攝像機(jī),這樣就可以獲取3D物體。

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

剛遇到一個問題,我的UI點擊包括3D物體點擊都是用的EventSystem,也就是上面的方法,這時用

UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()這個方法去判斷鼠標(biāo)是否在UI上,就會出現(xiàn)鼠標(biāo)在3D物體上也會拿到返回值,(沒有去研究傳參index的用法),直接選擇了上面獲取UI的獲取方法。

腳本:

/************************************************************
* 版本聲明:v1.0.0
* 類 名 稱:MouseOverController.cs
* 創(chuàng)建日期:2019/8/10 16:10:44
* 作者名稱:末零
* 功能描述:獲取鼠標(biāo)停留處的物體
************************************************************/ 
using System.Collections.Generic; 
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; 
namespace LastZero.Utility
{
    public class MouseOverController
    {
        /// <summary>
        /// 獲取鼠標(biāo)停留處UI
        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public static 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;
        } 
        /// <summary>
        /// 獲取鼠標(biāo)停留處UI
        /// </summary>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public static GameObject GetOverGameObject(GameObject camera)
        {
            if (camera.GetComponent<PhysicsRaycaster>() == null)
                camera.AddComponent<PhysicsRaycaster>(); 
            PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
            pointerEventData.position = Input.mousePosition;
            PhysicsRaycaster gr = camera.GetComponent<PhysicsRaycaster>();
            List<RaycastResult> results = new List<RaycastResult>();
            gr.Raycast(pointerEventData, results);
            if (results.Count != 0)
            {
                return results[0].gameObject;
            } 
            return null;
        }
    }
}

補充:unity中鼠標(biāo)經(jīng)過一個物體時出現(xiàn)提示

首先被檢測的物體要有collider

using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
//    public Transform cube;
    bool isShowTip;
//    // Use this for initialization
    void Start () {
        isShowTip=false;
    }    
    void OnMouseEnter () {
        isShowTip=true;
        //Debug.Log (cube.name);//可以得到物體的名字
    }
    void OnMouseExit () {
        isShowTip=false;
    }
    void OnGUI () {
        if (isShowTip){
            GUI.Label(new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,100,40),"afdasdfasdf"); 
         }  
    }
}

補充:Unity中UGUI中獲取鼠標(biāo)點擊位置以及UI物體的屏幕坐標(biāo)

鼠標(biāo)點擊位置:

直接訪問Input.mousePosition屬性,返回一個三維屏幕坐標(biāo),即鼠標(biāo)的坐標(biāo)。

UI物體的屏幕坐標(biāo):

RectTransformUtility.WordToScreenPoint(Camera.main, rectTransform.position),返回的是二維屏幕坐標(biāo)。

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

相關(guān)文章

最新評論

旬阳县| 格尔木市| 天柱县| 策勒县| 洪雅县| 旺苍县| 芜湖县| 龙陵县| 玛纳斯县| 阜城县| 宣城市| 双柏县| 涿州市| 阳原县| 英山县| 临江市| 长阳| 乐东| 龙岩市| 沈阳市| 绿春县| 灌云县| 伊吾县| 桦甸市| 改则县| 娱乐| 当涂县| 册亨县| 靖江市| 丰宁| 哈尔滨市| 马边| 平定县| 闵行区| 洛宁县| 天全县| 五华县| 澄江县| 深州市| 盐山县| 平潭县|