一、演示效果
二、知识点讲解
2.1 布局
void CreateItems()
{
var rewardLists = LuckyManager.Instance.CalculateRewardId(rewardDatas, Random.Range(4, 5));
reward_data_list = reward_data_list ?? new List<RewardData>();
reward_data_list.Clear();
for (int i = 0; i < itemCount; i++)
{
Transform item = null;
if (i + 1 <= itemRoot.childCount)
{
item = itemRoot.GetChild(i);
}
else
{
item = Instantiate(itemRoot.GetChild(0), itemRoot);
}
int rewardId = -1;
foreach (var index in rewardLists)
{
if (i == index)
{
rewardId = index; break;
}
}
var reward_img = item.Find("Icon").GetComponent<Image>();
var reward_value = item.Find("Value").GetComponent<TextMeshProUGUI>();
if (rewardId < 0)
{
reward_img.gameObject.SetActive(false);
reward_value.gameObject.SetActive(false);
continue;
}
var reward_data = LuckyManager.Instance.GetDataById(rewardDatas, rewardId, out int wheel_index);
reward_data_list.Add(reward_data);
reward_img.sprite = itemImgs[reward_data.type - 1];
reward_img.SetNativeSize();
reward_value.text = string.Format("{0}", reward_data.amount);
reward_img.gameObject.SetActive(true);
reward_value.gameObject.SetActive(true);
}
}
2.2 操作逻辑
protected internal override void OnInit(UIView view)
{
base.OnInit(view);
btnClose.onClick.AddListener(OnCloseEvent);
}
protected internal override void OnOpen(object userData)
{
base.OnOpen(userData);
eraseMask.Init(EraseComplete);
rewardDatas = LuckyManager.Instance.GetDefaultScrathcCardData(itemCount);
IsRolling = false;
CreateItems();
}
三、完整代码链接
https://github.com/lixianjun0903/luckydraw-master.git