using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Assets.Scripts._1024
{
class EVENTdome : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IDragHandler
{
//Unity 事件处理的几种方式
//1 通过编辑器绑定方式
//2:addlistener
//3:实现接口
//4:自定义框架
//1,2 绑定的同一个事件 ,比较隐秘,不建议用1
//光标类
//IPointerEnterHandler pointerEnterHandler;
//IPointerDownHandler pointerDownHandler;
//IPointerExitHandler pointerExitHandler;
//IPointerMoveHandler pointerMoveHandler;
//IPointerUpHandler pointerUpHandler;
//拖拽类
//IBeginDragHandler beginDragHandler;
//IDragHandler dragHandler;
//IEndDragHandler endDragHandler;
//IDropHandler dropHandler;
//点选类(可点选 的空间)
//IUpdateSelectedHandler updateSelectedHandler;
//ISelectHandler selectHandler;
//IDeselectHandler deselectHandler;
//输入类
//IScrollHandler scrollHandler;
//IMoveHandler moveHandler;
// ISubmitHandler submitHandler;
//ICancelHandler cancelHandler;
public void OnBeginDrag(PointerEventData eventData)
{
throw new NotImplementedException();
}
// 拖拽点和轴心线向量
Vector3 diss;
//T
public void OnDrag(PointerEventData eventData)
{
Camera camera = eventData.pressEventCamera;
Vector3 vector3 = eventData.position;
// 仅仅适用于canvas 是 overlay的模式
// this.transform.position = eventData.position;
// camery 模式需要转换
Vector3 worldPosition;
RectTransform rectTransform = this.transform.GetComponent<RectTransform>();
RectTransform transform = this.transform.parent as RectTransform;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform, eventData.position, camera, out worldPosition))
{
// 在这里可以使用worldPosition,例如更新UI元素的位置
transform.position = worldPosition;
}
this.transform.position = worldPosition + diss;
// this.transform.position = worldPosition;
}
public void OnPointerDown(PointerEventData eventData)
{
//记录按下到中心点的偏移量坐标
Vector2 vector2 = eventData.position; //鼠标屏幕坐标
Camera came = eventData.pressEventCamera;
Vector3 wporpositon;//鼠标世界坐标
RectTransform rectTransform = this.transform.parent as RectTransform;
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, vector2, came, out wporpositon);
diss = this.transform.position - wporpositon;
//if (eventData.clickCount == 2)
//{
// print(" PointerEventData double clicked ");
//}
}
}
}
下面是实现判断拖拽的方向
public class gamacontrols : MonoBehaviour, IPointerDownHandler, IDragHandler
{
#region 拖动参数
//Vector3 disvec;
Vector2 StartScreenPosition;
MoveDirections dir = MoveDirections.wait;
public bool IsDown = false;
#endregion
public void OnPointerDown(PointerEventData eventData)
{
//Vector2 vector2 = eventData.position;
RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, vector2, came, out wporpositon);
//RectTransform rectTransform = this.transform.parent as RectTransform;
//Vector3 vecotrcurrent;
//RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, vector2, eventData.pressEventCamera, out vecotrcurrent);
//disvec = this.transform.position - vecotrcurrent;
StartScreenPosition = eventData.position;
IsDown = true;
}
public void OnDrag(PointerEventData eventData)
{
if (IsDown)
{
Vector2 Offset = eventData.position - StartScreenPosition;
float x = MathF.Abs(Offset.x);
float y = MathF.Abs(Offset.y);
//执行左右移动
if (x > y && x >= 50)
{
dir = Offset.x > 0 ? MoveDirections.right : MoveDirections.left;
}
//执行上下移动
if (x < y && y >= 50)
{
dir = Offset.y > 0 ? MoveDirections.up : MoveDirections.dowm;
}
if (dir != MoveDirections.wait)
{
Core.Move(dir);
dir = MoveDirections.wait;
IsDown = false;
}
}
}
}