Unity EventSystem事件系统

     StandaloneInputModule和TouchInputModule两个组件会检测一些输入操作,以事件的方式(message系统)通知目标对象,那么这两个组件支持的事件主要有以下:

  • IPointerEnterHandler - OnPointerEnter - 当指针进入对象时调用
  • IPointerExitHandler - OnPointerExit - 当指针退出对象时调用
  • IPointerDownHandler - OnPointerDown - 在对象上按下指针时调用
  • IPointerUpHandler - OnPointerUp - 释放指针时调用(在原始对象上调用)
  • IPointerClickHandler - OnPointerClick - 在同一对象上按下并释放指针时调用
  • IInitializePotentialDragHandler - OnInitializePotentialDrag - 在找到拖动目标时调用,可用于初始化值
  • IBeginDragHandler - OnBeginDrag - 在拖动即将开始时调用拖动对象
  • IDragHandler - OnDrag - 发生拖动时在拖动对象上调用
  • IEndDragHandler - OnEndDrag - 拖动完成时在拖动对象上调用
  • IDropHandler - OnDrop - 在拖动完成的对象上调用
  • IScrollHandler - OnScroll - 当鼠标滚轮滚动时调用
  • IUpdateSelectedHandler - OnUpdateSelected - 在每个tick上调用所选对象
  • ISelectHandler - OnSelect - 当对象成为选定对象时调用
  • IDeselectHandler - OnDeselect - 在选定对象上调用将被取消选中
  • IMoveHandler - OnMove - 在移动事件发生时调用(左,右,上,下等)
  • ISubmitHandler - OnSubmit - 按下提交按钮时调用
  • ICancelHandler - OnCancel - 按下取消按钮时调用

只要目标对象的mono脚本实现了以上接口,那么输入模块会将检测到的事件通过这些接口通知给目标对象。

参考:http://docs.unity3d.com/Manual/SupportedEvents.html

 

部分实现

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

public class ScenePictureEdit : UIBehaviour, IDragHandler, IEndDragHandler, IPointerDownHandler,IPointerUpHandler, IBeginDragHandler
{
    private RectTransform ViewRectTran;
    private float  IPointerDown, IPointerUp;
    private bool IPointerDownBool = false, OnDragBool = false;
    private Vector2 m_PinPos;
    public GameObject EventDataGo;

    void Start ()
    {

        ViewRectTran = GetComponent();

    }

    void Update()
    {
        if (Time.time - IPointerDown > 1 && IPointerDownBool)
        {
            EventDataGo.GetComponent().sizeDelta *=1.2f;
            EventDataGo.GetComponent().SetParent ( GameObject.Find("Canvas").GetComponent());
            OnDragBool = true;
            IPointerDownBool = false;
        }
    }

    public virtual void OnDrag(PointerEventData eventData)
    {
        IPointerDownBool = false;
        Vector2 vector;
        if (((eventData.button == PointerEventData.InputButton.Left) && this.IsActive()) && RectTransformUtility.ScreenPointToLocalPointInRectangle(this.ViewRectTran, eventData.position, eventData.pressEventCamera, out vector))
        {
            
            /*if (!OnDragBool)*/  ViewRectTran.localPosition +=new  Vector3((vector.x - m_PinPos.x),0,0);
            //else EventDataGo.GetComponent().localPosition += (Vector3)(vector - m_PinPos);
            m_PinPos = vector;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log(1);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        EventDataGo = eventData.pointerEnter.GetComponentInParent().gameObject;
        IPointerDownBool = true;
        IPointerDown = Time.time;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        IPointerUp = Time.time;
        if (IPointerUp- IPointerDown < 0.3)
        {
            if (GameObject.Find("Book_Bg").GetComponent())
            GameObject.Find("Book_Bg").GetComponent().ScenePictureBtn(eventData.pointerEnter.GetComponent());
        }
        IPointerDownBool = false;
        if (OnDragBool)
        {
            EventDataGo.GetComponent().SetParent(GetComponent());
            EventDataGo.GetComponent().sizeDelta /= 1.2f;
        }
        OnDragBool = false;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector2 vector;
        if (((eventData.button == PointerEventData.InputButton.Left) && this.IsActive()) && RectTransformUtility.ScreenPointToLocalPointInRectangle(this.ViewRectTran, eventData.position, eventData.pressEventCamera, out vector))
        {
            m_PinPos = vector;
        }
        
    }
}

 

你可能感兴趣的:(Unity)