Unity中UI的一些经常用的事件EventTrigger
上一章说了一些Unity不规则图片点击。这一次就说一说常用的UI事件。
EventTrigger是一个可以实现很多功能的东东。
添加方法有两种,第一就是在Inspector中添加EventTrigger
第二种就是写代码了。身为一个独游开发者写代码才是我的特长。好吧!我不喜欢废话,下面是干货。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Test: EventTrigger
{
public override void OnBeginDrag(PointerEventData data)
{
Debug.Log("处理开始拖动时要做什么事.");
}
public override void OnCancel(BaseEventData data)
{
Debug.Log("OnCancel called.");
}
public override void OnDeselect(BaseEventData data)
{
Debug.Log("OnDeselect called.");
}
public override void OnDrag(PointerEventData data)
{
Debug.Log("理拖动过程中要做什么事.");
}
public override void OnDrop(PointerEventData data)
{
Debug.Log("OnDrop called.");
}
public override void OnEndDrag(PointerEventData data)
{
Debug.Log("理拖动结束要做什么事.");
}
public override void OnInitializePotentialDrag(PointerEventData data)
{
Debug.Log("OnInitializePotentialDrag called.");
}
public override void OnMove(AxisEventData data)
{
Debug.Log("OnMove called.");
}
public override void OnPointerClick(PointerEventData data)
{
Debug.Log("OnPointerClick called.");
Debug.Log("在图片内取消滑鼠点击");
}
public override void OnPointerDown(PointerEventData data)
{
Debug.Log("OnPointerDown called.");
Debug.Log("滑鼠点击");
}
public override void OnPointerEnter(PointerEventData data)
{
Debug.Log("OnPointerEnter called.");
Debug.Log("滑鼠移入");
}
public override void OnPointerExit(PointerEventData data)
{
Debug.Log("OnPointerExit called.");
Debug.Log("滑鼠移出");
}
public override void OnPointerUp(PointerEventData data)
{
Debug.Log("OnPointerUp called.");
Debug.Log("取消滑鼠点击");
}
public override void OnScroll(PointerEventData data)
{
Debug.Log("OnScroll called.");
}
public override void OnSelect(BaseEventData data)
{
Debug.Log("OnSelect called.");
}
public override void OnSubmit(BaseEventData data)
{
Debug.Log("OnSubmit called.");
}
public override void OnUpdateSelected(BaseEventData data)
{
Debug.Log("OnUpdateSelected called.");
}
}
代码不是只有这一种写发,另一种就是这个
using UnityEngine;
using UnityEngine.EventSystems;
public class ChoiceCart : MonoBehaviour,IPointerEnterHandler,IPointerClickHandler {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnPointerEnter(PointerEventData eventData)
{
//滑鼠进入
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("在图片内取消滑鼠点击");
}
}
在这里的IPointerEnterHandler和IPointerClickHandler就是和上面的不同,要用哪个就要继承哪个。另外说一句不要忘了添加using UnityEngine.EventSystems;