Unity抽奖转盘

UI抽奖转盘

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(转盘的item转动指针不动 如需指针转动自行修改原理类似)

圆形布局code CircularLayoutGroup : LayoutGroup

	public override void CalculateLayoutInputVertical()
    {
        if (rectTransform.childCount > 0)
            Calculate();
    }
    public int StartAngle = 0;
    public int Radius = 360;
    public bool IsAutoRotateSelfToCenter = false;
    public void Calculate()
    {
        float perAngle = 360f / rectTransform.childCount;
        for (int i = 0; i < rectTransform.childCount; i++)
        {
            float angle = (i * perAngle + StartAngle) * Mathf.Deg2Rad;
            //半径 * 旋转角度的 sin cos的值 相对于圆心的 x,y
            float x = Radius * Mathf.Sin(angle);
            float y = Radius * Mathf.Cos(angle);
            var pos = new Vector2(x, y);
            pos.x += padding.left;
            pos.x -= padding.right;
            pos.y += padding.top;
            pos.y -= padding.bottom;
            rectTransform.GetChild(i).localPosition = pos;
            if (IsAutoRotateSelfToCenter)
            {
                Vector2 dir1 = -pos;
                Vector2 dir2 = Vector2.down; // 
                float signedAngle = Vector2.SignedAngle(dir1, dir2);
                rectTransform.GetChild(i).localRotation = Quaternion.Euler(0, 0, -signedAngle);
            }
            else
            {
                rectTransform.GetChild(i).localRotation = Quaternion.identity;
            }

        }
    }

逻辑code 用到了Dotween商店自行下载

	using DG.Tweening;
	
    public Transform _dial;//转盘
    private int _cyclesNum = 3;//旋转的整圈数
    private float _duration = 3;//旋转的持续时间
    List<Transform> items = new List<Transform>();
    public InputField _resultId;
    public Button _startButton;
    public bool _isAntidicDirection;//逆时针旋转

    private void Start()
    {
        for (int i = 0; i < _dial.childCount; i++)
        {
            items.Add(_dial.GetChild(i));
        }
        _startButton.onClick.AddListener(() => startDial());
    }
    public void startDial()
    {
        _startButton.interactable = false;
        _resultId.text = string.Empty;
        //伪随机
        int rotNum = Random.Range(1, items.Count);
        int angle = _cyclesNum * (_isAntidicDirection ? 360 : -360) + (rotNum - 1) * 360 / items.Count;
        //本次旋转的度数
        _dial.DORotate(new Vector3(0, 0, angle), _duration, RotateMode.FastBeyond360).OnUpdate(() =>
        {
            foreach (var item in items)
                //矫正子物体旋转防止父物体带动子物体旋转
                item.localEulerAngles = (-_dial.transform.eulerAngles);
        }).OnComplete(() =>
        {
            _resultId.text = rotNum.ToString();
            _startButton.interactable = true;
        }).SetEase(Ease.InOutCubic);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值