本文共 1890 字,大约阅读时间需要 6 分钟。
01.添加事件
using UnityEngine;using UnityEngine.EventSystems;[RequireComponent(typeof(EventTrigger))]public class ImageDrag : MonoBehaviour { public void Click() { Debug.Log("Click"); }}
02.、
using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;public class Test5 : MonoBehaviour { private EventTrigger eventTrigger; void Awake() { eventTrigger = this.GetComponent(); } void Start() { eventTrigger.triggers = new List (); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.PointerClick; entry.callback = new EventTrigger.TriggerEvent(); entry.callback.AddListener((data) => { Click(); }); } private void Click() { Debug.Log("Click"); }}
using UnityEngine;using UnityEngine.EventSystems;public class DragTest4 : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler,IInitializePotentialDragHandler,IDropHandler{ public RectTransform canRectTransform; public void OnBeginDrag(PointerEventData eventData) { Debug.Log("beginDrag"); } //UI跟随鼠标 public void OnDrag(PointerEventData eventData) { Debug.Log("Drag"); var rec = this.GetComponent(); Vector3 pos = Vector3.zero; RectTransformUtility.ScreenPointToWorldPointInRectangle(rec, eventData.position, eventData.enterEventCamera, out pos); rec.position = pos; } public void OnEndDrag(PointerEventData eventData) { Debug.Log("EndDrag"); } public void OnInitializePotentialDrag(PointerEventData eventData) { Debug.Log("OnInitializePotential"); } public void OnDrop(PointerEventData eventData) { Debug.Log("Drop"); }}
转载地址:http://vurxo.baihongyu.com/