71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEngine.EventSystems;
|
|||
|
|
|||
|
public class UIDragToScene3D : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|||
|
{
|
|||
|
public GameObject buildingPrefab; // <20><>Ӧ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>Ԥ<EFBFBD>Ƽ<EFBFBD>
|
|||
|
private GameObject previewObject; // <20>϶<EFBFBD>ʱ<EFBFBD><CAB1>ʾ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>ʵ<EFA3A8>ʵ<EFBFBD> 3D ģ<>ͣ<EFBFBD>
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><>ʼ<EFBFBD>϶<EFBFBD>ʱ
|
|||
|
/// </summary>
|
|||
|
/// <param name="eventData"></param>
|
|||
|
public void OnBeginDrag(PointerEventData eventData)
|
|||
|
{
|
|||
|
Debug.Log("Drag Started");
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> 3D <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>϶<EFBFBD><CFB6>е<EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (buildingPrefab != null)
|
|||
|
{
|
|||
|
previewObject = Instantiate(buildingPrefab);
|
|||
|
previewObject.GetComponent<Collider>().enabled = false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD>壬<EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>϶<EFBFBD><CFB6><EFBFBD>
|
|||
|
public void OnDrag(PointerEventData eventData)
|
|||
|
{
|
|||
|
if (previewObject != null)
|
|||
|
{
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Vector3 mouseWorldPosition = GetMouseWorldPosition();
|
|||
|
previewObject.transform.position = mouseWorldPosition; // <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
/// </summary>
|
|||
|
/// <param name="eventData"></param>
|
|||
|
public void OnEndDrag(PointerEventData eventData)
|
|||
|
{
|
|||
|
Debug.Log("Drag Ended");
|
|||
|
|
|||
|
if (previewObject != null)
|
|||
|
{
|
|||
|
// <20><><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪʵ<CEAA>ʵĽ<CAB5><C4BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
previewObject.GetComponent<Collider>().enabled = true; // <20>ָ<EFBFBD><D6B8><EFBFBD>ײ<EFBFBD><D7B2>
|
|||
|
previewObject = null; // <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
private Vector3 GetMouseWorldPosition()
|
|||
|
{
|
|||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|||
|
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity))
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>棬<EFBFBD><E6A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĵ<D0B5>
|
|||
|
return hit.point;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>棬<EFBFBD><E6A3AC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ĭ<EFBFBD>ϵ<EFBFBD>
|
|||
|
return ray.GetPoint(10); // Ĭ<>Ͼ<EFBFBD><CFBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 10 <20><><EFBFBD><EFBFBD>λ
|
|||
|
}
|
|||
|
}
|
|||
|
}
|