2021. 6. 24. 18:23
Unity/C#
드래그 체크
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;
}
'Unity > C#' 카테고리의 다른 글
유니티 좌표계산 모음 (0) | 2021.07.01 |
---|---|
유니티 기즈모 (Gizmo) 관련코드 (0) | 2021.06.15 |
유니티 안드로이드 빌드관련 스크립트 (0) | 2021.06.10 |