Files
Simulation/Assets/Scripts/UI/UIDragSpawner.cs

62 lines
1.6 KiB
C#
Raw Normal View History

2025-04-30 17:55:20 +09:00
using UnityEngine;
2025-06-05 17:54:18 +09:00
using UnityEngine.UI;
2025-04-30 17:55:20 +09:00
using UnityEngine.EventSystems;
public class UIDragSpawner : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
2025-05-09 10:53:08 +09:00
public RectTransform rectTransform;
public LayerMask mask;
2025-04-30 17:55:20 +09:00
private GameObject dragPreview;
2025-06-05 17:54:18 +09:00
public SimulationModel targetModel;
2025-09-08 12:03:11 +09:00
Transform objectRoot;
2025-06-05 17:54:18 +09:00
//private CanvasGroup canvasGroup;
2025-04-30 17:55:20 +09:00
void Awake()
{
2025-09-08 12:03:11 +09:00
objectRoot = ObjectRoot.I.transform;
2025-04-30 17:55:20 +09:00
}
2025-06-05 17:54:18 +09:00
public void SetInfo(SimulationModel spawnTarget, Sprite thumbnail)
2025-04-30 17:55:20 +09:00
{
2025-06-05 17:54:18 +09:00
this.GetComponent<Image>().sprite = thumbnail;
targetModel = spawnTarget;
}
2025-05-09 10:53:08 +09:00
2025-06-05 17:54:18 +09:00
public void OnBeginDrag(PointerEventData eventData)
{
2025-09-08 12:03:11 +09:00
dragPreview = Instantiate(targetModel).gameObject;
dragPreview.transform.SetParent(objectRoot);
2025-04-30 17:55:20 +09:00
}
public void OnDrag(PointerEventData eventData)
{
if (dragPreview)
{
2025-05-09 10:53:08 +09:00
Ray ray = Camera.main.ScreenPointToRay(eventData.position);
if (Physics.Raycast(ray, out RaycastHit hit, 100.0f, mask))
{
dragPreview.transform.position = hit.point;
}
2025-04-30 17:55:20 +09:00
}
}
public void OnEndDrag(PointerEventData eventData)
{
2025-05-09 10:53:08 +09:00
if (IsPointerOverScrollRect(rectTransform))
{
2025-04-30 17:55:20 +09:00
Destroy(dragPreview);
2025-05-09 10:53:08 +09:00
dragPreview = null;
2025-06-05 17:54:18 +09:00
return;
}
else
{
DataManager.I.AddModel(dragPreview.GetComponent<SimulationModel>());
}
2025-05-09 10:53:08 +09:00
}
public bool IsPointerOverScrollRect(RectTransform rt)
{
return RectTransformUtility.RectangleContainsScreenPoint(rt, Input.mousePosition);
2025-04-30 17:55:20 +09:00
}
}