一、演示效果
二、知识点
2.1 布局
private void CreateItems(){for (int i = 0; i < rewardDatas.Length; i++){var reward_data = rewardDatas[i];GameObject fruitOjb;if (i < itemRoot.childCount){fruitOjb = itemRoot.GetChild(i).gameObject;}else{fruitOjb = Instantiate(itemRoot.GetChild(0).gameObject, itemRoot);}fruitOjb.SetActive(true);var fruitImg = fruitOjb.transform.Find("Icon").GetComponent<Image>();fruitImg.sprite = itemImgs[reward_data.type - 1];fruitImg.SetNativeSize();fruitImg.transform.localScale = Vector3.one * itemSize;var amountTxt = fruitOjb.GetComponentInChildren<TextMeshProUGUI>();amountTxt.text = string.Format("{0}", reward_data.amount);fruitOjb.transform.localScale = Vector3.one * itemSize;cursorLists.Add(fruitOjb.transform.Find("Cursor").gameObject);selectLists.Add(fruitOjb.transform.Find("Select").gameObject);}}
2.2 转动逻辑
private IEnumerator StartSpin(Action<List<RewardData>> onSpinCompleted){IsRolling = true;ChangeSelectEanble(0, true);ChangeCursorEanble(0, true);GetSelectIndex();int[] rewardIdLists = LuckyManager.Instance.CalculateRewardId(rewardDatas, GetRewardCount());int spinCount = rewardIdLists.Length;List<RewardData> rewardDataList = new List<RewardData>();if (rollStyle3){yield return StaraThreeSpine();}for (int i = 0; i < rewardIdLists.Length; i++){var rewardDt = LuckyManager.Instance.GetDataById(rewardDatas, rewardIdLists[i], out int rewardIndex);if (rewardDt == null){Debug.LogErrorFormat("水果机表中不存在id:{0}", rewardIdLists[i]);continue;}int totalLen = itemCount * 3 + rewardIndex;if (rollStyle3 || rollStyle2)totalLen = itemCount * 3 - i + rewardIndex;int curIndex = startIndexs;float spinTime = totalLen * 0.06f;var moveAct = DOTween.To(() => curIndex, (x) => curIndex = x, totalLen, spinTime);moveAct.SetEase(Ease.Linear);moveAct.onUpdate = () =>{int fixNum = curIndex % itemCount;startIndexs = fixNum;ChangeCursorEanble(fixNum);};moveAct.OnComplete(() =>{rewardDataList.Add(rewardDt);ChangeSelectEanble(rewardIndex);if (--spinCount <= 0){IsRolling = false;onSpinCompleted?.Invoke(rewardDataList);}});if (rollStyle3 || rollStyle2)yield return new WaitForSeconds(spinTime);}}
四 、代码链接
https://github.com/lixianjun0903/luckydraw-master.git