Unity如何判断是否点击在gui上
Posted avi9111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity如何判断是否点击在gui上相关的知识,希望对你有一定的参考价值。
目录
准确来说,是判断是否点击在Ugui和ngui上,因为这2个ui,或其他所谓第三方Ui,都是基于三维的基类
所以能通过Unity鸡肋中的鸡肋,嗨,是基类,gameobject来判断,
后面会分享一个gui(基于IMGUI)的实例吧,因为我用的比较多,网上却介绍不多
ugui等如果没有manager,也是挺难用的,传统的gui或者说imgui就是layout和style一体的, UnityEditor也提供了一些gui 的utility,挺好的
但我们还是先说说ugui吧,imgui之后补充
ugui点击的基本判断
/// <summary>
/// Whether touch down or mouse button down over UI GameObject.
/// </summary>
public bool IsClickDownOverUI()
{
#if UNITY_EDITOR
if (Input.GetMouseButtonDown(0))
#else
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
#endif
{
#if UNITY_EDITOR
if (EventSystem.current.IsPointerOverGameObject())
#else
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#endif
{
return true;
}
}
return false;
}
/// <summary>
/// Whether touch up or mouse button up over UI GameObject.
/// </summary>
public static bool IsClickUpOverUI()
{
#if UNITY_EDITOR
if (Input.GetMouseButtonUp(0))
#else
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)
#endif
{
#if UNITY_EDITOR
if (EventSystem.current.IsPointerOverGameObject())
#else
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#endif
{
return true;
}
}
return false;
}
IMGUI的判断
以上是关于Unity如何判断是否点击在gui上的主要内容,如果未能解决你的问题,请参考以下文章
在Unity3d中如何碰撞触发GUI的显示,然后3秒之后自动消失?(求详细代码)