드래그 드롭 기능 추가
This commit is contained in:
24
Assets/Scripts/UI/UIButtonToggle.cs
Normal file
24
Assets/Scripts/UI/UIButtonToggle.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UIButtonToggle : MonoBehaviour
|
||||
{
|
||||
public bool isPressed = false;
|
||||
public UnityEvent onPressed;
|
||||
public UnityEvent onReleased;
|
||||
void Awake()
|
||||
{
|
||||
Button button = GetComponent<Button>();
|
||||
if (button != null)
|
||||
{
|
||||
button.onClick.AddListener(
|
||||
() =>
|
||||
{
|
||||
isPressed = !isPressed;
|
||||
if (isPressed) onPressed?.Invoke();
|
||||
else onReleased?.Invoke();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/UIButtonToggle.cs.meta
Normal file
2
Assets/Scripts/UI/UIButtonToggle.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e97dd21b324d80a468a071d9171b5f77
|
||||
@@ -4,17 +4,15 @@ using UnityEngine.EventSystems;
|
||||
|
||||
public class UIDragSpawner : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public GameObject prefabToSpawn;
|
||||
public Canvas canvas;
|
||||
private RectTransform rectTransform;
|
||||
private CanvasGroup canvasGroup;
|
||||
public RectTransform rectTransform;
|
||||
public GameObject prefabToSpawn;
|
||||
public LayerMask mask;
|
||||
private GameObject dragPreview;
|
||||
private CanvasGroup canvasGroup;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
//canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
|
||||
canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
@@ -23,16 +21,19 @@ public class UIDragSpawner : MonoBehaviour, IBeginDragHandler, IDragHandler, IEn
|
||||
canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
canvasGroup.alpha = 0.6f;
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
|
||||
dragPreview = Instantiate(gameObject, canvas.transform);
|
||||
dragPreview.GetComponent<CanvasGroup>().blocksRaycasts = false;
|
||||
|
||||
dragPreview = Instantiate(prefabToSpawn);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (dragPreview)
|
||||
{
|
||||
dragPreview.GetComponent<RectTransform>().position = eventData.position;
|
||||
Ray ray = Camera.main.ScreenPointToRay(eventData.position);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100.0f, mask))
|
||||
{
|
||||
dragPreview.transform.position = hit.point;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +42,14 @@ public class UIDragSpawner : MonoBehaviour, IBeginDragHandler, IDragHandler, IEn
|
||||
canvasGroup.alpha = 1f;
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
|
||||
if (dragPreview)
|
||||
if (IsPointerOverScrollRect(rectTransform))
|
||||
{
|
||||
Destroy(dragPreview);
|
||||
|
||||
Ray ray = Camera.main.ScreenPointToRay(eventData.position);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
Instantiate(prefabToSpawn, hit.point, Quaternion.identity);
|
||||
}
|
||||
dragPreview = null;
|
||||
}
|
||||
}
|
||||
public bool IsPointerOverScrollRect(RectTransform rt)
|
||||
{
|
||||
return RectTransformUtility.RectangleContainsScreenPoint(rt, Input.mousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user