查看: 522|回复: 0
打印 上一主题 下一主题

[其他] 判断鼠标或手指(移动平台)是否点击在UGUI上

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53456
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-31 23:29:32 |只看该作者 |倒序浏览

很多的时候我们要判断时候都点中UGUI

此时要加命名空间 using UnityEngine.EventSystem;

有的时候新建脚本并不能直接用 UGUI  此时也要加命名空间  using UnityEngine.UI;

接下来就是判断是否点击在UGUI上了 ,上代码


//Windows 上

if (Input.GetMouseButtonDown(0))
            {
                Debug.Log(EventSystem.current.gameObject.name);
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("当前触摸在UI上");
            
                }
                else
                {
                  
                    Debug.Log("当前没有触摸在UI上");
               
                }
            }


//安卓上

  if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                {
                    Debug.Log("Hit UI, Ignore Touch");
               
                }
                else
                {
               
                    Debug.Log("Handle Touch");
                }
            }


   bool IsPointerOverGameObject(int fingerId)
    {
        EventSystem eventSystem = EventSystem.current;
        return (eventSystem.IsPointerOverGameObject(fingerId)
            && eventSystem.currentSelectedGameObject != null);
    }

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-6 12:50 , Processed in 0.079887 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部