127 lines
4.0 KiB
C#
127 lines
4.0 KiB
C#
|
|
using JetBrains.Annotations;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using Unity.VisualScripting;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.EventSystems;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
using ZXKFramework;
|
|||
|
|
namespace YiLiao.JingMaiLiuZhiZhen
|
|||
|
|
{
|
|||
|
|
public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerEnterHandler,IPointerExitHandler
|
|||
|
|
{
|
|||
|
|
public enum Group
|
|||
|
|
{
|
|||
|
|
group1,
|
|||
|
|
group2,
|
|||
|
|
group3,
|
|||
|
|
group4
|
|||
|
|
}
|
|||
|
|
public string dragName;
|
|||
|
|
public Group group;
|
|||
|
|
[Header("<22>Ƿ<EFBFBD><C7B7><EFBFBD>ק")]
|
|||
|
|
public bool drag;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public GameObject falseImage;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public GameObject rightImage;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public GameObject hoverImage;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public GameObject dragImage;
|
|||
|
|
public Drag rightTarget;//<2F><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>
|
|||
|
|
[HideInInspector]
|
|||
|
|
public Drag yourTarget;//<2F><><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
|
DragManager dragManager;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public RectTransform rectTransform;
|
|||
|
|
private Vector3 startPos;
|
|||
|
|
private Vector2 offset;
|
|||
|
|
[HideInInspector]
|
|||
|
|
public bool right;
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
rectTransform = GetComponent<RectTransform>();
|
|||
|
|
startPos = rectTransform.localPosition;
|
|||
|
|
falseImage = transform.FindFirst("FalseImg");
|
|||
|
|
rightImage = transform.FindFirst("RightImg");
|
|||
|
|
hoverImage = transform.FindFirst("HoverImg");
|
|||
|
|
dragImage = transform.FindFirst("DragImg");
|
|||
|
|
}
|
|||
|
|
public void Init(DragManager dragManager)
|
|||
|
|
{
|
|||
|
|
this.dragManager = dragManager;
|
|||
|
|
ResetPos();
|
|||
|
|
right = false;
|
|||
|
|
falseImage.SetActive(false);
|
|||
|
|
rightImage.SetActive(false);
|
|||
|
|
hoverImage.SetActive(false);
|
|||
|
|
dragImage.SetActive(false);
|
|||
|
|
yourTarget = null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ʼ<EFBFBD><CABC>קʱ<D7A7><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (drag && !falseImage.activeSelf && !rightImage.activeSelf)
|
|||
|
|
{
|
|||
|
|
RectTransform rectTransform = transform as RectTransform;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UI Ԫ<>ص<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
|
|||
|
|
offset = rectTransform.anchoredPosition - eventData.position;
|
|||
|
|
dragManager.drag = this;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><>ק<EFBFBD><D7A7><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>
|
|||
|
|
public void OnDrag(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (drag && !falseImage.activeSelf && !rightImage.activeSelf)
|
|||
|
|
{
|
|||
|
|
dragImage.SetActive(true);
|
|||
|
|
hoverImage.SetActive(false);
|
|||
|
|
RectTransform rectTransform = transform as RectTransform;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ú<EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UI Ԫ<>ص<EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
|
rectTransform.anchoredPosition = eventData.position + offset;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void OnEndDrag(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
if (drag && !falseImage.activeSelf && !rightImage.activeSelf)
|
|||
|
|
{
|
|||
|
|
dragManager.DragEnd();
|
|||
|
|
dragImage.SetActive(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public void ResetPos()
|
|||
|
|
{
|
|||
|
|
rectTransform.localPosition = startPos;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0> EventSystem
|
|||
|
|
EventSystem eventSystem = EventSystem.current;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD> UI <20><>
|
|||
|
|
if (eventSystem.IsPointerOverGameObject())
|
|||
|
|
{
|
|||
|
|
if (!dragImage.activeSelf && yourTarget == null)//dragImage<67><65>hoverImageֻ<65><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʾ
|
|||
|
|
{
|
|||
|
|
hoverImage.SetActive(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public void OnPointerExit(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0> EventSystem
|
|||
|
|
EventSystem eventSystem = EventSystem.current;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD> UI <20><>
|
|||
|
|
if (eventSystem.IsPointerOverGameObject())
|
|||
|
|
{
|
|||
|
|
hoverImage.SetActive(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|