美文网首页知识的搬运者
判断鼠标触摸点是否在UI范围内(NGUI版)

判断鼠标触摸点是否在UI范围内(NGUI版)

作者: 会奔跑的蘑菇 | 来源:发表于2020-04-09 16:42 被阅读0次

    using UnityEngine;

    using System.Collections;

    public class MouseTool

    {

        /// <summary>

        /// 鼠标是否在UI范围内

        /// </summary>

        /// <param name="trans"></param>

        /// <returns></returns>

        public static bool IsTouchInUi(Transform trans)

        {

            bool isInRect = false;

            float _mapWidth = trans.GetComponent<UIWidget>().width;//获取ui的实际宽度

            float _mapHight = trans.GetComponent<UIWidget>().height;//长度

            Vector3 newPos = trans.localPosition;

    //注意,别用错方法了,只能是InverseTransformPoint

    Vector3 mousePos = trans.parent.InverseTransformPoint(UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition));

            if (mousePos.x < (newPos.x + _mapWidth / 2) && mousePos.x > (newPos.x - _mapWidth / 2) &&

                mousePos.y < (newPos.y + _mapHight / 2) && mousePos.y > (newPos.y - _mapHight / 2))

            {

                isInRect = true;

            }

            return isInRect;

        }

    }

    相关文章

      网友评论

        本文标题:判断鼠标触摸点是否在UI范围内(NGUI版)

        本文链接:https://www.haomeiwen.com/subject/bggcmhtx.html