Unity3D 让一张UI图片绕一个点画圆转圈|同时可拖拽

旋转脚本放在要旋转的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;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值