我是靠谱客的博主 贤惠大象,最近开发中收集的这篇文章主要介绍Unity_UGUI的事件系统,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

IPointerEnterHandler
指针进入
public void OnPointerEnter(PointerEventData eventData);

IPointerExitHandler
指针退出
public void OnPointerExit(PointerEventData eventData);

IPointerDownHandler
指针按下
public void OnPointerDown(PointerEventData eventData);

IPointerUpHandler
指针释放(可能按下时的指针位置跟释放时的指针位置不同,这里指的是按下时指针指着的物体)
public void OnPointerUp(PointerEventData eventData);

IPointerClickHandler
在同一物体上按下并释放
public void OnPointerClick(PointerEventData eventData);

IInitializePotentialDragHandler
拖拽时的初始化,跟IPointerDownHandler差不多,在按下时调用 
public void OnInitializePotentialDrag(PointerEventData eventData);

IBeginDragHandler
开始拖拽
public void OnBeginDrag(PointerEventData eventData);

IDragHandler
拖拽中
public void OnDrag(PointerEventData eventData);

IEndDragHandler
拖拽结束(被拖拽的物体调用)
public void OnEndDrag(PointerEventData eventData);

IDropHandler
拖拽结束(拖拽结束后的位置(即鼠标位置)如果有物体,则那个物体调用)
public void OnDrop(PointerEventData eventData);

IScrollHandler
滚轮滚动
public void OnScroll(PointerEventData eventData);

IUpdateSelectedHandler
被选中的物体每帧调用
public void OnUpdateSelected(BaseEventData eventData);

ISelectHandler
物体被选中时(EventSystem.current.SetSelectedGameObject(gameObject))
public void OnSelect(BaseEventData eventData);

IDeselectHandler
物体从选中到取消选中时
public void OnDeselect(BaseEventData eventData);

IMoveHandler
物体移动时(与InputManager里的Horizontal和Vertica按键相对应),前提条件是物体被选中
public void OnMove(AxisEventData eventData);

ISubmitHandler
提交按钮被按下时(与InputManager里的Submit按键相对应,PC上默认的是Enter键),前提条件是物体被选中
public void OnSubmit(BaseEventData eventData);

ICancelHandler
取消按钮被按下时(与InputManager里的Cancel按键相对应,PC上默认的是Esc键),前提条件是物体被选中
public void OnCancel(BaseEventData eventData);


//
总结:
1.共有17个接口方法
2.其中OnMove使用的参数是AxisEventData eventData
3.其中OnUpdateSelected、OnSelect 、OnDeselect 、OnSubmit 、OnCancel 使用的参数是BaseEventData eventData
4.其余使用的参数是PointerEventData eventData
//
实现事件的两种方式:
1.编辑器:添加Event Trigger组件

2.代码:继承EventTrigger,实现方法(EventTrigger继承了上面所说的接口)


using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TestUI3 : MonoBehaviour
{
    private GameObject button;
    private GameObject image;

	void Start () 
	{
        button = GameObject.Find("Button");
        image = GameObject.Find("Image");
        EventTriggerListener.GetListener(button).onPointerClick += Click;
        EventTriggerListener.GetListener(image).onDrag += Drag;
	}

    public void Click(GameObject go)
    {
        Debug.Log(go.name);
    }

    public void Drag(GameObject go)
    {
        Debug.Log(go.name);
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class EventTriggerListener : EventTrigger
{
    #region 变量
    //带参数是为了方便取得绑定了UI事件的对象  
    public delegate void PointerEventDelegate(GameObject go, PointerEventData eventData);
    public delegate void BaseEventDelegate(GameObject go, BaseEventData eventData);
    public delegate void AxisEventDelegate(GameObject go, AxisEventData eventData);

    public event PointerEventDelegate onPointerEnter;
    public event PointerEventDelegate onPointerExit;
    public event PointerEventDelegate onPointerDown;
    public event PointerEventDelegate onPointerUp;
    public event PointerEventDelegate onPointerClick;
    public event PointerEventDelegate onInitializePotentialDrag;
    public event PointerEventDelegate onBeginDrag;
    public event PointerEventDelegate onDrag;
    public event PointerEventDelegate onEndDrag;
    public event PointerEventDelegate onDrop;
    public event PointerEventDelegate onScroll;
    public event BaseEventDelegate onUpdateSelected;
    public event BaseEventDelegate onSelect;
    public event BaseEventDelegate onDeselect;
    public event AxisEventDelegate onMove;
    public event BaseEventDelegate onSubmit;
    public event BaseEventDelegate onCancel;
    #endregion

    public static EventTriggerListener GetListener(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    #region 方法
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onPointerEnter != null) onPointerEnter(gameObject, eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onPointerExit != null) onPointerExit(gameObject, eventData);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onPointerDown != null) onPointerDown(gameObject, eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onPointerUp != null) onPointerUp(gameObject, eventData);
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onPointerClick != null) onPointerClick(gameObject, eventData);
    }
    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject, eventData);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject, eventData);
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(gameObject, eventData);
    }
    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject, eventData);
    }
    public override void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null) onScroll(gameObject, eventData);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelected != null) onUpdateSelected(gameObject, eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject, eventData);
    }
    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject, eventData);
    }
    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(gameObject, eventData);
    }
    public override void OnSubmit(BaseEventData eventData)
    {
        if (onSubmit != null) onSubmit(gameObject, eventData);
    }
    public override void OnCancel(BaseEventData eventData)
    {
        if (onCancel != null) onCancel(gameObject, eventData);
    }
    #endregion
}


最后

以上就是贤惠大象为你收集整理的Unity_UGUI的事件系统的全部内容,希望文章能够帮你解决Unity_UGUI的事件系统所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部