Unity翻牌对对碰

[复制链接]
查看11699 | 回复1 | 2023-1-25 17:16:44 | 显示全部楼层 |阅读模式
本项目工程是用Unity2018.3.0f2,亲测可用,工程在附件链接里。
资源均来源于网络,仅供学习和参考使用,版权归原作者所有,勿作商业用途,请在下载后24小时之内自觉删除。
本站发布的内容若无意中侵犯到您的权益,请联系我们,本站将在看到后立即删除。
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;

  4. public class GameMain : MonoBehaviour {

  5.     public Button btnLevel1;
  6.     public Button btnLevel2;
  7.     public Button btnLevel3;

  8.     public Transform panelStart;
  9.     public Transform panelCard;
  10.     public Transform panelOver;

  11.     // Use this for initialization
  12.     void Start () {
  13.         btnLevel1.onClick.AddListener(()=> {
  14.             panelStart.gameObject.SetActive(false);
  15.             panelCard.gameObject.SetActive(true);
  16.             LoadLevelCard(3, 2);
  17.         });
  18.         btnLevel2.onClick.AddListener(() => {
  19.             panelStart.gameObject.SetActive(false);
  20.             panelCard.gameObject.SetActive(true);
  21.             LoadLevelCard(4, 2);
  22.         });
  23.         btnLevel3.onClick.AddListener(() => {
  24.             panelStart.gameObject.SetActive(false);
  25.             panelCard.gameObject.SetActive(true);
  26.             LoadLevelCard(5, 2);
  27.         });
  28.         Button btnToStart = panelOver.Find("Button_to_start").GetComponent<Button>();
  29.         btnToStart.onClick.RemoveAllListeners();
  30.         btnToStart.onClick.AddListener(ToGameStartPage);
  31.     }

  32.     void LoadLevelCard(int width , int height)
  33.     {
  34.         //1加载卡牌图片
  35.         Sprite [] sps = Resources.LoadAll<Sprite>("Sprite");
  36.          
  37.         //2计算需要加载卡牌的数量
  38.         int totalCount = width * height/2;
  39.         //3计算加载卡牌的索引
  40.         List<Sprite> spsList = new List<Sprite>();
  41.         for( int i = 0; i < sps.Length; i++)
  42.         {
  43.             spsList.Add(sps[i]);
  44.         }

  45.         List<Sprite> needShowCardList = new List<Sprite>();
  46.         while (totalCount > 0)
  47.         {
  48.             int randomIndex = Random.Range(0,spsList.Count);
  49.             needShowCardList.Add(spsList[randomIndex]);
  50.             needShowCardList.Add(spsList[randomIndex]);
  51.             spsList.RemoveAt(randomIndex);
  52.             totalCount--;
  53.         }
  54.         //自加打散
  55.         for (int i = 0; i < needShowCardList.Count; i++)
  56.         {
  57.             int num = Random.Range(0, needShowCardList.Count);
  58.             if (num != i)
  59.             {
  60.                 Sprite temp = needShowCardList[i];
  61.                 needShowCardList[i] = needShowCardList[num];
  62.                 needShowCardList[num] = temp;
  63.             }
  64.         }
  65.         //4显示卡牌到UI上
  66.         Transform contentRoot = panelCard.Find("Panel");
  67.         int maxCount = Mathf.Max( contentRoot.childCount,needShowCardList.Count);
  68.         GameObject itemPrefab = contentRoot.GetChild(0).gameObject;
  69.         for (int i = 0; i < maxCount; i++)
  70.         {
  71.             GameObject itemObject = null;
  72.             if (i < contentRoot.childCount)
  73.             {
  74.                 itemObject = contentRoot.GetChild(i).gameObject;
  75.             }
  76.             else
  77.             {
  78.                 //clone
  79.                 itemObject = GameObject.Instantiate<GameObject>(itemPrefab);
  80.                 itemObject.transform.SetParent(contentRoot, false);
  81.             }
  82.             itemObject.transform.Find("Image_front").GetComponent<Image>().sprite = needShowCardList[i];
  83.             CardFlipAnimationCtrl cardAniCtrl = itemObject.GetComponent<CardFlipAnimationCtrl>();
  84.             cardAniCtrl.SetDefaultState();
  85.         }

  86.         GridLayoutGroup glg = contentRoot.GetComponent<GridLayoutGroup>();

  87.         float panelWidth = width * glg.cellSize.x+glg.padding.left+
  88.             glg.padding.right+(width-1)*glg.spacing.x;
  89.         float panelHeight = height * glg.cellSize.y+glg.padding.top+
  90.             glg.padding.bottom+(width-1)*glg.spacing.y;
  91.         contentRoot.GetComponent<RectTransform>().sizeDelta = new Vector2(panelWidth, panelHeight);
  92.     }


  93.     public void CheckIsGameOver()
  94.     {
  95.         CardFlipAnimationCtrl[] allCards =  GameObject.FindObjectsOfType<CardFlipAnimationCtrl>();
  96.         if( allCards != null && allCards.Length > 0)
  97.         {
  98.             List<CardFlipAnimationCtrl> cardInFront = new List<CardFlipAnimationCtrl>();

  99.             for( int i = 0; i < allCards.Length; i++)
  100.             {
  101.                 CardFlipAnimationCtrl cardTem = allCards[i];
  102.                 if( cardTem.isInFront && !cardTem.isOver)
  103.                 {
  104.                     cardInFront.Add(cardTem);
  105.                 }

  106.                 if(cardInFront.Count >= 2)
  107.                 {
  108.                     string cardImageName1 = cardInFront[0].GetCardImageName();
  109.                     string cardImageName2 = cardInFront[1].GetCardImageName();
  110.                     if( cardImageName1 == cardImageName2)
  111.                     {
  112.                         cardInFront[0].MachSucess();
  113.                         cardInFront[1].MachSucess();
  114.                     }
  115.                     else
  116.                     {
  117.                         cardInFront[0].MachFail();
  118.                         cardInFront[1].MachFail();
  119.                     }
  120.                     allCards = GameObject.FindObjectsOfType<CardFlipAnimationCtrl>();
  121.                     bool isAllOver = true;
  122.                     for( int o = 0; o < allCards.Length; o++)
  123.                     {
  124.                         isAllOver &= allCards[o].isOver;
  125.                     }
  126.                     if( isAllOver)
  127.                     {
  128.                         ToGameOverPanel();
  129.                     }  
  130.                     break;
  131.                 }
  132.             }
  133.         }
  134.     }

  135.     // Update is called once per frame
  136.     public void ToGameOverPanel ()
  137.     {
  138.         panelStart.gameObject.SetActive(false);
  139.         panelCard.gameObject.SetActive(false);
  140.         panelOver.gameObject.SetActive(true);
  141.     }

  142.     public void ToGameStartPage()
  143.     {
  144.         panelStart.gameObject.SetActive(true);
  145.         panelCard.gameObject.SetActive(false);
  146.         panelOver.gameObject.SetActive(false);
  147.     }
  148. }
复制代码
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;

  6. /***
  7. *
  8. * 卡牌功能类
  9. *
  10. * **/
  11. public class CardFlipAnimationCtrl : MonoBehaviour,IPointerClickHandler {

  12.     public Transform cardFront;
  13.     public Transform cardBack;

  14.     public float flipDuaration = 0.2f;

  15.     public bool isInFront = false;
  16.     public bool isOver = false;
  17.    
  18.     void Awake () {
  19.         cardFront = transform.Find("Image_front");
  20.         cardBack = transform.Find("Image_back");
  21.         }


  22.     public void OnPointerClick(PointerEventData eventData)
  23.     {
  24.         if(!isInFront)
  25.         {
  26.             StartCoroutine(FlipCardToFront());
  27.         }
  28.         else
  29.         {
  30.             StartCoroutine(FlipCardToBack());
  31.         }
  32.               
  33.     }

  34.     IEnumerator FlipCardToFront()
  35.     {
  36.         //1.翻转反面到90度
  37.         cardFront.gameObject.SetActive(false);
  38.         cardBack.gameObject.SetActive(true);
  39.         cardFront.rotation = Quaternion.identity;
  40.         while( cardBack.rotation.eulerAngles.y>90)
  41.         {
  42.             cardBack.rotation *= Quaternion.Euler(0, Time.deltaTime*90f*(1f/flipDuaration), 0);
  43.             if (cardBack.rotation.eulerAngles.y > 90)
  44.             {
  45.                 cardBack.rotation = Quaternion.Euler( 0, 90 , 0 );
  46.                 break;
  47.             }
  48.             yield return new WaitForFixedUpdate();
  49.         }

  50.         //2.翻转正面到0度
  51.         cardFront.gameObject.SetActive(true);
  52.         cardBack.gameObject.SetActive(false);
  53.         cardFront.rotation = Quaternion.Euler(0,90,0);
  54.         while (cardFront.rotation.eulerAngles.y > 0)
  55.         {
  56.             cardFront.rotation *= Quaternion.Euler(0, -Time.deltaTime * 90f * (1f / flipDuaration), 0);
  57.             if (cardFront.rotation.eulerAngles.y > 90)
  58.             {
  59.                 cardFront.rotation = Quaternion.Euler(0, 0, 0);
  60.                 break;
  61.             }
  62.             yield return new WaitForFixedUpdate();
  63.         }
  64.         isInFront = true;

  65.         Camera.main.gameObject.GetComponent<GameMain>().CheckIsGameOver();
  66.     }

  67.     IEnumerator FlipCardToBack()
  68.     {
  69.         //1.翻转正面到90度
  70.         cardFront.gameObject.SetActive(true);
  71.         cardBack.gameObject.SetActive(false);
  72.         cardFront.rotation = Quaternion.identity;
  73.         while (cardFront.rotation.eulerAngles.y < 90)
  74.         {
  75.             cardFront.rotation *= Quaternion.Euler(0, Time.deltaTime * 90f * (1f / flipDuaration), 0);
  76.             if (cardFront.rotation.eulerAngles.y > 90)
  77.             {
  78.                 cardFront.rotation = Quaternion.Euler(0, 90, 0);
  79.                 break;
  80.             }
  81.             yield return new WaitForFixedUpdate();
  82.         }

  83.         //2.翻转正面到0度
  84.         cardFront.gameObject.SetActive(false);
  85.         cardBack.gameObject.SetActive(true);
  86.         cardBack.rotation = Quaternion.Euler(0, 90, 0);
  87.         while (cardBack.rotation.eulerAngles.y > 0)
  88.         {
  89.             cardBack.rotation *= Quaternion.Euler(0, -Time.deltaTime * 90f * (1f / flipDuaration), 0);
  90.             if (cardBack.rotation.eulerAngles.y > 90)
  91.             {
  92.                 cardBack.rotation = Quaternion.Euler(0, 0, 0);
  93.                 break;
  94.             }
  95.             yield return new WaitForFixedUpdate();
  96.         }
  97.         isInFront = false;
  98.     }

  99.     internal void SetDefaultState()
  100.     {
  101.         cardFront.gameObject.SetActive(false);
  102.         cardBack.gameObject.SetActive(true);
  103.         isOver = false;
  104.         isInFront = false;
  105.         cardFront.rotation = Quaternion.identity;
  106.         cardBack.rotation = Quaternion.identity;
  107.     }

  108.     internal string GetCardImageName()
  109.     {
  110.         return cardFront.GetComponent<Image>().sprite.name;
  111.     }

  112.     internal void MachSucess()
  113.     {
  114.         isOver = true;
  115.         cardFront.gameObject.SetActive(false);
  116.         cardBack.gameObject.SetActive(false);
  117.     }

  118.     internal void MachFail()
  119.     {
  120.         StartCoroutine(FlipCardToBack());
  121.     }
  122. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

964178 | 11 小时前 | 显示全部楼层

The Essential Support Provided by the Counsel Familiar in Disability Law

What Reason Employing Your Government Aid Disability Lawyer Proves to Be Necessary.

Hiring A Public Assistance Incapacity Lawyer Can Be Compulsory.





The SSA Administers numerous programs from layoff benefits to social aid, and also deals with impairment benefits for individuals not able to perform due to long-term sickness or wound. As with every single intricate institution, there are laws and regulations which must be followed by the aforementioned body in arrangement to attain advantages. Thinking about bringing on an expert Social Security Act legal professional Is vital.



Employing the skilled Social Security Disability counsel might guide you manage the challenges of SSA SSA, improving your possibility of acceptance for advantages. They might assure that your proposal is lodged accurately and includes corroborating medical evidence; furthermore, they recognize how to compile this evidence promptly so it is not turned in late; furthermore, they could avoid offering irrelevant data that will lead to hindrances and dismissals by the Services for Secure Aging.



Your specialized professional in disabilities will also ready you for a hearing with SSA if your request is turned down, by describing what to envisage at it and clarifying any of your queries about what happens there. They may guide with presenting additional supporting materials and querying the arbiter who heard your lawsuit to examine their judgment and query witnesses or employment specialists called by SSA to testify on your behalf, which can boost the possibility that an early appeal for disability succeeds if originally denied. Attorneys specializing in social security disability could regain any past due perks attributable to changes in the starting date, which symbolizes when your ailment began. Effective disability advocates may support with recovering any past due perks that would have been lost attributable to changes in the starting date or when your condition began.



Applying for an application for Social Security Disability benefits involves a careful consideration of the specific conditions that make you eligible


The Critical Role of the Counselor Experienced in Disability Matters 811f398
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

11

主题

11

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10393