100个 Unity小游戏系列六 -Unity 抽奖游戏专题四 翻卡游戏

embedded/2024/10/19 11:51:16/

一、演示效果

二、知识点讲解

2.1 布局

  void CreateItems(){reward_data_list = reward_data_list ?? new List<RewardData>();reward_data_list.Clear();for (int i = 0; i < ItemCount; i++){GameObject item;if (i + 1 <= itemParent.childCount){item = itemParent.GetChild(i).gameObject;}else{item = Instantiate(itemParent.GetChild(0).gameObject, itemParent);}item.transform.localPosition = initPosArr[i];item.transform.localScale = Vector3.one;var cardGraphics = item.transform.Find("Graphics").GetComponent<RectTransform>();RefreshCardData(cardGraphics, false, rewardDatas[i]);SetCardOpen(false, item.GetComponent<Button>(), true, rewardDatas[i]);}StartAnim(rewardDatas);}/// <summary>/// 明牌,洗牌动画/// </summary>private void StartAnim(RewardData[] rewardsArr){CardSelectable = false;if (rewardsArr.Length != itemParent.childCount){Debug.LogErrorFormat("rewards length is not equals cards count:{0}", itemParent.childCount);return;}isSwapCard = true;var seqAnim = DOTween.Sequence();seqAnim.AppendInterval(2f);seqAnim.AppendCallback(() =>{int overCount = itemParent.childCount;for (int i = 0; i < itemParent.childCount; i++){SetCardOpen(false, itemParent.GetChild(i).GetComponent<Button>(), false, null, 0.2f, () =>{//所有卡片翻回去之后开始洗牌if (--overCount <= 0){List<int> cardsList = new List<int>();for (int childIndex = 0; childIndex < itemParent.childCount; childIndex++){cardsList.Add(childIndex);}SwapCards(cardsList, 10, () =>{RefreshCardADState(false);CardSelectable = true;});}});}});}

2.2 转动逻辑

 private void SwapCards(List<int> cardsList, int swapCount, Action onComplete){if (swapCount <= 0){onComplete?.Invoke();return;}SwapCardAnim(cardsList, () =>{ListRandom(cardsList);SwapCards(cardsList, --swapCount, onComplete);});}public static void ListRandom<T>(List<T> sources){int index;T temp;for (int i = 0; i < sources.Count; i++){index = UnityEngine.Random.Range(0, sources.Count);if (index != i){temp = sources[i];sources[i] = sources[index];sources[index] = temp;}}}/// <summary>/// 洗牌动画/// </summary>/// <param name="onSwapOver"></param>private void SwapCardAnim(List<int> cardsindexList, Action onSwapOver){if (itemParent.childCount % 2 != 0){Debug.LogError("cardsRoot 卡片个数不能为奇数");return;}int moveMission = itemParent.childCount;TweenCallback onMoveOver = () =>{if (--moveMission <= 0){onSwapOver?.Invoke();}};for (int i = 0; i < cardsindexList.Count; i += 2){var indexA = cardsindexList[i];var indexB = cardsindexList[i + 1];var cardA = itemParent.GetChild(indexA);var cardB = itemParent.GetChild(indexB);float moveDuration = Vector2.Distance(initPosArr[indexA], initPosArr[indexB]) / 1500;moveDuration = Mathf.Clamp(moveDuration, 0, 0.18f);cardA.DOLocalMove(initPosArr[indexB], moveDuration).onComplete = onMoveOver;cardB.DOLocalMove(initPosArr[indexA], moveDuration).onComplete = onMoveOver;}}private void SetCardOpen(bool isUser, Button cardBt, bool isOpen, RewardData reward = null, float duration = 0.2f, Action onCardAnimOver = null){if (isOpen && reward == null){Debug.LogError("翻卡传入奖励数据为null");return;}if (isUser && !CardSelectable){return;}CardSelectable = false;int texIndex = 0;int colorIndex = 0;string tmStr = string.Empty;Vector3 halfRotate = new Vector3(0, 90, 0);if (isOpen){texIndex = reward.type;colorIndex = reward.type - 1;tmStr = reward.amount.ToString();halfRotate.y = 270;}cardBt.interactable = !isOpen;var card = cardBt.transform;var cardGraphics = card.Find("Graphics").GetComponent<RectTransform>();var seqAnim = DOTween.Sequence();seqAnim.Append(cardGraphics.DOLocalRotate(halfRotate, duration).SetEase(Ease.Linear));seqAnim.AppendCallback(() =>{RefreshCardData(cardGraphics, isOpen, reward);});seqAnim.Append(cardGraphics.DOLocalRotate(halfRotate + Vector3.up * 90, duration).SetEase(Ease.Linear));seqAnim.onComplete = () =>{if (!isUser){//CardSelectable = true;onCardAnimOver?.Invoke();}};if (isUser){card.SetSiblingIndex(itemParent.childCount);//float moveDuration = Vector2.Distance(card.transform.localPosition, Vector3.zero) / 400;//moveDuration = Mathf.Clamp(moveDuration, 0, 0.4f);//card.DOMove(Vector3.zero, moveDuration);//card.DOScale(1.4f, moveDuration).onComplete = () =>//{//    var moveSeq = DOTween.Sequence();//    moveSeq.AppendInterval(1.0f);//    moveSeq.AppendCallback(() =>//    {//        var smallCard = smallCardsRoot.GetChild(CurClaimIndex);//        moveDuration = Mathf.Clamp(Vector2.Distance(card.position, smallCard.position) / 10, 0, 0.4f);//        card.DOScale(smallCard.GetComponent<RectTransform>().sizeDelta.x / card.GetComponent<RectTransform>().sizeDelta.x, moveDuration);//        card.DOMove(smallCard.position, moveDuration).onComplete = () =>//        {//            CardSelectable = true;//            GF.Sound.PlaySound("poker_end.wav", false);//            //加奖励//            GF.UserData.ClaimReward(UserDataType.LuckpokerRedpacket, reward, GF.UserData.GameUIForm.gameMainView.levelSocreTxt.transform);//            onCardAnimOver?.Invoke();//        };//    });//};CardSelectable = true;onCardAnimOver?.Invoke();}}

三 、完整代码逻辑

https://github.com/lixianjun0903/luckydraw-master.git


http://www.ppmy.cn/embedded/43650.html

相关文章

解锁数据的力量 全新 Navicat 17 焕新上市

Navicat 17 解锁数据的力量&#x1f680; 焕新上市 &#x1f525; Navicat Premium 17Navicat 17 for MySQLNavicat 17 for OracleNavicat 17 for SQL ServerNavicat 17 for PostgreSQLNavicat 17 for MariaDBNavicat 17 for RedisNavicat 17 for MongoDBNavicat 17 for SQLit…

20240528训练题目(2022 国际大学生程序设计竞赛亚洲区域赛 (南京站))

D题 题目描述 You’re the researcher of the International Chat Program Company (ICPC). Today, you discover the following chat history when reviewing some research data. SUA (2022/12/04 23:01:25) I’m out of ideas for competitive programming problems! Pl…

STM32-11-电容触摸按键

STM32-01-认识单片机 STM32-02-基础知识 STM32-03-HAL库 STM32-04-时钟树 STM32-05-SYSTEM文件夹 STM32-06-GPIO STM32-07-外部中断 STM32-08-串口 STM32-09-IWDG和WWDG STM32-10-定时器 STM32电容触摸按键 电容触摸按键原理&#xff1a; 无手指触摸&#xff1a;上电时&…

常见排序算法之插入排序

目录 一、直接插入排序 1.1 什么是插入排序 1.2 代码思路 1.3 C语言源码 二、希尔排序 2.0 插入排序的弊端 2.1 什么是希尔排序&#xff1f; 2.2 排序思路 2.3 C语言源码 一、直接插入排序 1.1 什么是插入排序 插入排序是一种简单直观的排序算法&#xff0c;它通过构…

图像上下文学习|多模态基础模型中的多镜头情境学习

【原文】众所周知&#xff0c;大型语言模型在小样本上下文学习&#xff08;ICL&#xff09;方面非常有效。多模态基础模型的最新进展实现了前所未有的长上下文窗口&#xff0c;为探索其执行 ICL 的能力提供了机会&#xff0c;并提供了更多演示示例。在这项工作中&#xff0c;我…

elasticdump和ESM

逐个执行如下命令&#xff1b; 1.拷贝analyzer如分词&#xff08;需要分词器&#xff0c;可能不成功&#xff0c;不影响复制&#xff09; ./elasticdump --inputhttp://[来源IP地址]:9200/[来源索引] --outputhttp://[目标IP地址]:9200/[目标索引] --typeanalyzer 2.拷贝映射…

unity 制作app实现底部导航栏和顶部状态栏

前段时间在用unity制作一个app&#xff0c;发现有个问题用unity制作的app&#xff0c;他默认是没有顶部状态栏的&#xff0c;也没有底部的导航栏&#xff0c;是一个全部覆盖的状态。但仔细观察可以发现&#xff0c;正常app&#xff0c;顶部状态栏是有的&#xff0c;而且是透明的…

HBase安装

安装HBase 提示&#xff1a;需要安装好hadoop和zookeeper 安装zookeeper可参考 一、确定HBase版本 去网站确认 https://hbase.apache.org/book.html#hadoop二、下载HBase安装包 去清华大学镜像站下载 https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/三、安装HBase …