unity判断是否点击了UI界面

1.判断是否点击 UGUI界面:

复制代码
using UnityEngine;  
using System.Collections;  
using UnityEngine.UI;  
using UnityEngine.EventSystems;  
public class UiClickTest : MonoBehaviour {  
  
    // Use this for initialization  
    void Start () {  
      
    }  
      
    // Update is called once per frame  
    void Update () {  
  
        if (Input.GetMouseButtonDown(0)) {  
            if (EventSystem.current.IsPointerOverGameObject()) {  
                Debug.Log("点击到UGUI的UI界面,会返回true");  
            } else {  
                Debug.Log("如果没点击到UGUI上的任何东西,就会返回false");  
            }  
        }  
    }  
}  
复制代码

2.判断是否点击了NGUI界面:

复制代码
void OnClick ()  
{  
    // NGUI 3.8之前 点击空白地方返回为null  
    // NGUI 3.8之后 点击空白地方返回为UIRoot  
    if (UICamera.hoveredObject.name != "UIRoot")  
    {  
        Debug.Log ("我现在点击的不是空白");  
    }  
}  

void Update()  
   {  
       // 返回值是bool变量,如果鼠标在UI上返回true(记得加碰撞器)  
    Debug.Log(UICamera.Raycast(Input.mousePosition));  
  
   }  
复制代码

 

posted @   游戏鼻祖  阅读(14222)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示