前言:每日记录自己学习unity的心得和体会,小弟才疏学浅,如有错误的地方,欢迎大佬们指正,感谢~
代码添加 EventTrigger 方法
检测鼠标进入UI显示ui的名字非常好用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ChuFaWaite : MonoBehaviour {
private Image imagee;
private EventTrigger trigger;
// Use this for initialization
void Start()
{
imagee = this.GetComponent<Image>();
if (!GetComponent<EventTrigger>())
{
trigger = gameObject.AddComponent<EventTrigger>();
}
else
{
trigger = gameObject.GetComponent<EventTrigger>();
}
UnityAction<BaseEventData> click = new UnityAction<BaseEventData>(MyClick);
EventTrigger.Entry myclick = new EventTrigger.Entry();
myclick.eventID = EventTriggerType.PointerEnter;
myclick.callback.AddListener(click);
trigger.triggers.Add(myclick);
UnityAction<BaseEventData> _click = new UnityAction<BaseEventData>(MyClick_);
EventTrigger.Entry myclick_ = new EventTrigger.Entry();
myclick_.eventID = EventTriggerType.PointerExit;
myclick_.callback.AddListener(_click);
trigger.triggers.Add(myclick_);
}
//进入
public void MyClick(BaseEventData data)
{
imagee.color = Color.white;
}
//离开
public void MyClick_(BaseEventData data)
{
imagee.color = Color.red;
}
}
3D物体检测
加入碰撞盒,可用下面代码检测鼠标进入物体,显示物体名字
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obj_OnTrigger : MonoBehaviour {
//当鼠标进入碰撞器的时候调用
private void OnMouseEnter()
{
Debug.Log("进入");
}
//当鼠标离开碰撞器的时候调用
private void OnMouseExit()
{
Debug.Log("离开");
}
}