UGUI 之 判断鼠标或者手指是否点击在UI上

参考雨松大大的方法:

比如战斗场景,UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分。为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上。 以前NGUI的时候都是自己来发送射线判断,现在UGUI好了系统提供了更为简便的方法。

#if UNITY_ANDROID && !UNITY_EDITOR
#define ANDROID
#endif
 
 
#if UNITY_IPHONE && !UNITY_EDITOR
#define IPHONE
#endif
 
 
 
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour {
 
	// Use this for initialization
	void Start () {
	
	}
	
 
	void Update()
	{
		if (Input.GetMouseButtonDown(0)||(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Began))
		{
#if IPHONE || ANDROID
			if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
			if (EventSystem.current.IsPointerOverGameObject())
#endif
				Debug.Log("当前触摸在UI上");
			
			else 
				Debug.Log("当前没有触摸在UI上");
		}
	}
}

这段代码我在Android上已经测试通过。 哦 我用的是unity5.1

//对应unity5版本之前的版本

Unity UGUI鼠标穿透UI问题(Unity官方的解决方法)


大家可以参考

http://forum.unity3d.com/threads/4-6-how-to-detect-if-any-gui-element-has-been-clicked-on.263473/

补充:.android 用上面的方法无效。。 看来还是要写射线,不过我更相信这是unity的bug.这里有个解决的方案,大家可以试试。。

http://forum.unity3d.com/threads/ispointerovereventsystemobject-always-returns-false-on-mobile.265372/


这里我们直接在使用Input.GetMouseButtonDown(0)的地方加了一个检测函数,CheckGuiRaycastObjects,如下

bool CheckGuiRaycastObjects()
{
    PointerEventData eventData = new PointerEventData(Main.Instance.<strong>eventSystem</strong>);
    eventData.pressPosition = Input.mousePosition;
    eventData.position = Input.mousePosition;

    List<RaycastResult> list = new List<RaycastResult>();
    Main.Instance.<strong>graphicRaycaster</strong>.Raycast(eventData, list);
    //Debug.Log(list.Count);
    return list.Count > 0;
}

不过在使用时需要先获取两个加粗显示的变量,graphicRaycaster和eventSystem。

这两个变量分别对应的是Canvas中的GraphicRaycaster组件,和创建UI时自动生成的“EventSystem”中的EventSystem组件,用的是自己制定以下就可以。

然后在使用的时候可以这样


void Update () 
{
    if (CheckGuiRaycastObjects()) return;
    //Debug.Log(EventSystem.current.gameObject.name);
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            //do some thing
        }
    }
} 

还有一个需要注意的地方就是,在做UI的时候一般会用一个Panel做跟目录,这个panel也会被添加到GraphicRegistry中的公共列表中,如果是这样的话记得把list.Count>0改成list.Count>1,或者直接删除Panel上的继承自Graphic的组件。

这样在结合着EventSystem.current.IsPointerOverGameObject()来使用就比较好了。


写个例子:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.UI;


public class MainMenu : MonoBehaviour {

	// Use this for initialization
	void Start () {
		List<string> btnsName = new List<string>();
		btnsName.Add("BtnPlay");
		btnsName.Add("BtnShop");
		btnsName.Add("BtnLeaderboards");

		foreach(string btnName in btnsName)
		{
			GameObject btnObj = GameObject.Find(btnName);
			Button btn = btnObj.GetComponent<Button>();
			btn.onClick.AddListener(delegate() {
				this.OnClick(btnObj); 
			});
		} 
	}

	public void OnClick(GameObject sender)
	{
		switch (sender.name)
		{
		case "BtnPlay":
			Debug.Log("BtnPlay");
			break;
		case "BtnShop":
			Debug.Log("BtnShop");
			break;
		case "BtnLeaderboards":
			Debug.Log("BtnLeaderboards");
			break;
		default:
			Debug.Log("none");
			break;
		}
	}
	
	// Update is called once per frame
	void Update () {
	
        if(Input.GetMouseButtonDown(0))

{
if (CheckGuiRaycastObjects()) return;
GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);

}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值