Unity/C#
유니티 커스텀 인터페이스
모카쨩
2021. 6. 24. 18:23
드래그 체크
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CustomInteface : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public GameObject ui;
// Start is called before the first frame update
void Start()
{
}
public void OnBeginDrag(PointerEventData data)
{
}
public void OnDrag(PointerEventData data)
{
}
public void OnEndDrag(PointerEventData data)
{
var zPos = transform.position.z;
var pos = Camera.main.ScreenToWorldPoint(data.position);
pos.z = zPos;
var pressPos = Camera.main.ScreenToWorldPoint(data.pressPosition);
pressPos.z = zPos;
ui.transform.position = pos;
}
// Update is called once per frame
void Update()
{
}
}
아이템 드래그
if (Vector2.Distance(eventData.pressPosition,eventData.position)>300)
{
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var worldPoint))
{
transform.position = worldPoint;
}
}
else
{
transform.position = eventData.pressPosition;
}