68 lines
1.4 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
using UnityEngine;
using UnityEngine.EventSystems;
public class DragRotator : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform root;
public new Camera camera;
private Vector3 _lastPoint;
public RenderTexture renderTexture
{
get;
private set;
}
// Start is called before the first frame update
private void Awake()
{
//RenderTextureDescriptor desc = default;
//desc.width = 1024;
//desc.height = 1024;
renderTexture = RenderTexture.GetTemporary(1024, 1024, 16, RenderTextureFormat.ARGB32);
camera.targetTexture = renderTexture;
}
private void OnDestroy()
{
RenderTexture.ReleaseTemporary(renderTexture);
}
// Update is called once per frame
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_lastPoint = Input.mousePosition;
}
else if (Input.GetMouseButton(0))
{
var mousePoint = Input.mousePosition;
var deltaX = _lastPoint.x - mousePoint.x;
var deltaY = _lastPoint.y - mousePoint.y;
_lastPoint = mousePoint;
var pointY = Screen.height / 1920f * 1000f;
if (mousePoint.y > pointY)
root.Rotate(Vector3.up * (deltaX * 10f * Time.deltaTime), Space.Self);
}
}
public void OnDrag(PointerEventData eventData)
{
root.Rotate(Vector3.up * (-eventData.delta.x), Space.Self);
}
public void OnBeginDrag(PointerEventData eventData)
{
}
public void OnEndDrag(PointerEventData eventData)
{
}
}