旋转脚本放在要旋转的UI上,此UI是子物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class positionrotate : MonoBehaviour
{
private Vector2 oriVector2;
private int indexx = 0;
public bool clockwise = true;
public float Radius = 10f;
public float speed = 0.1f;
// Start is called before the first frame update
void OnEnable()
{
transform.localPosition = Vector2.zero;
indexx = 0;
oriVector2 = transform.position;
}
private void Update()
{
//rotatevector2 = oriVector2;
if (clockwise)
{
indexx++;
}
else
{
indexx--;
}
transform.position = GetCurPosByUndex(indexx) + oriVector2;
}
void OnDisable()
{
transform.localPosition = Vector2.zero;
}
public Vector2 GetCurPosByUndex(int index)
{
float totalAngle = Mathf.Deg2Rad * (index * speed);
if (totalAngle > 2 * Mathf.PI || totalAngle < -2 * Mathf.PI) indexx = 0;
Vector2 Pos = new Vector2(Radius * Mathf.Cos(totalAngle), Mathf.Sin(totalAngle) * Radius);
return Pos;
}
}
拖拽脚本放在要拖拽的物体上,此物体是父物体
using UnityEngine;
using UnityEngine.EventSystems;
public class DragPuzzle : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
RectTransform rt;
Vector2 startVector2;
public string TriggerName;
public positionrotate positionrotate;
public int index;
// 位置偏移量
Vector3 offset = Vector3.zero;
private void Awake()
{
rt = GetComponent<RectTransform>();
startVector2 = rt.GetComponent<RectTransform>().anchoredPosition;
}
void Start()
{
positionrotate = transform.GetChild(0).GetComponent<positionrotate>();
}
private void OnEnable()
{
rt.GetComponent<RectTransform>().anchoredPosition = startVector2;
if (positionrotate != null)
positionrotate.enabled = true;
}
public void OnBeginDrag(UnityEngine.EventSystems.PointerEventData eventData)
{
this.transform.SetAsLastSibling();
if (positionrotate != null)
positionrotate.enabled = false;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.enterEventCamera, out Vector3 globalMousePos))
{
// 计算偏移量
offset = rt.position - globalMousePos;
}
}
public void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
{
// 将屏幕空间上的点转换为位于给定RectTransform平面上的世界空间中的位置
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out Vector3 globalMousePos))
{
// 加上偏移量保证相对位置不变
rt.position = globalMousePos + offset;
}
}
public void OnPointerUp(UnityEngine.EventSystems.PointerEventData eventData)
{
}
public void OnEndDrag(UnityEngine.EventSystems.PointerEventData eventData)
{
rt.GetComponent<RectTransform>().anchoredPosition = startVector2;
if (positionrotate != null)
positionrotate.enabled = true;
}
}
1万+

被折叠的 条评论
为什么被折叠?



