當(dāng)前位置:
首頁(yè) >
Unity抽奖转盘
發(fā)布時(shí)間:2024/3/24
39
豆豆
UI抽獎(jiǎng)轉(zhuǎn)盤
(轉(zhuǎn)盤的item轉(zhuǎn)動(dòng)指針不動(dòng) 如需指針轉(zhuǎn)動(dòng)自行修改原理類似)
圓形布局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;//半徑 * 旋轉(zhuǎn)角度的 sin cos的值 相對(duì)于圓心的 x,yfloat 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;//轉(zhuǎn)盤private int _cyclesNum = 3;//旋轉(zhuǎn)的整圈數(shù)private float _duration = 3;//旋轉(zhuǎn)的持續(xù)時(shí)間List<Transform> items = new List<Transform>();public InputField _resultId;public Button _startButton;public bool _isAntidicDirection;//逆時(shí)針旋轉(zhuǎn)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;//偽隨機(jī)int rotNum = Random.Range(1, items.Count);int angle = _cyclesNum * (_isAntidicDirection ? 360 : -360) + (rotNum - 1) * 360 / items.Count;//本次旋轉(zhuǎn)的度數(shù)_dial.DORotate(new Vector3(0, 0, angle), _duration, RotateMode.FastBeyond360).OnUpdate(() =>{foreach (var item in items)//矯正子物體旋轉(zhuǎn)防止父物體帶動(dòng)子物體旋轉(zhuǎn)item.localEulerAngles = (-_dial.transform.eulerAngles);}).OnComplete(() =>{_resultId.text = rotNum.ToString();_startButton.interactable = true;}).SetEase(Ease.InOutCubic);}總結(jié)
- 上一篇: Oracle 组织机构代码校验函数
- 下一篇: 群晖ds服务器型号,【群晖 Synolo