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,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;//轉盤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);}總結
- 上一篇: Oracle 组织机构代码校验函数
- 下一篇: 群晖ds服务器型号,【群晖 Synolo