MyPingTu.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. public class MyPingTu : MonoBehaviour
  6. {
  7. public GameObject end;
  8. ItemImg[] itemImg;
  9. Sprite[] UIShowImage;
  10. int length = 0;
  11. int sqrt = 0;
  12. void Start()
  13. {
  14. var RSLoadAllImages = Resources.LoadAll("Texture1");
  15. length = transform.childCount;
  16. sqrt = (int)Mathf.Sqrt(length);
  17. Debug.Log("sqrt==>" + sqrt);
  18. Debug.Log("length==>" + length);
  19. //childLength-1——其中某一张为空白,即后下角为空白
  20. UIShowImage = new Sprite[length];
  21. for (int i = 0; i < length; i++)
  22. {
  23. UIShowImage[i] = (Sprite)RSLoadAllImages[i + 1];
  24. }
  25. //初始化图片的id,col,row
  26. InitItemImg(length);
  27. //打乱图片
  28. Shuffle(UIShowImage);
  29. InitShow();
  30. }
  31. /// <summary>
  32. /// 数组打乱
  33. /// </summary>
  34. T[] Shuffle<T>(T[] Array)
  35. {
  36. for (int i = Array.Length-3; i < Array.Length-1; i++)
  37. {
  38. T temp = Array[i];
  39. int randomIndex = i+1;//UnityEngine.Random.Range(Array.Length-2, Array.Length-1);
  40. Array[i] = Array[randomIndex];
  41. Array[randomIndex] = temp;
  42. }
  43. /*
  44. for (int i = 0; i < Array.Length - 1; i++)
  45. {
  46. T temp = Array[i];
  47. int randomIndex = UnityEngine.Random.Range(0, Array.Length-1);
  48. Array[i] = Array[randomIndex];
  49. Array[randomIndex] = temp;
  50. }*/
  51. return Array;
  52. }
  53. /// <summary>
  54. /// 初始化图片id,col,row
  55. /// </summary>
  56. void InitItemImg(int count)
  57. {
  58. itemImg = new ItemImg[length];
  59. for (int i = 0; i < count; i++)
  60. {
  61. itemImg[i] = new ItemImg();
  62. itemImg[i]._id = i;
  63. itemImg[i]._col = i / sqrt;
  64. itemImg[i]._row = i % sqrt;
  65. }
  66. }
  67. void InitShow()
  68. {
  69. //初始化显示图片
  70. for (int i = 0; i < UIShowImage.Length; ++i)
  71. {
  72. var t = transform.GetChild(i);
  73. t.GetComponent<Image>().sprite = UIShowImage[i];
  74. //指定Sprite
  75. itemImg[i]._sprite = t.GetComponent<Image>().sprite;
  76. //指定Obj
  77. itemImg[i]._Obj = t.gameObject;
  78. var item = itemImg[i];
  79. itemImg[i]._Obj.GetComponent<Button>().onClick.AddListener(delegate () {
  80. this.OnClickEvent(item);
  81. });
  82. }
  83. itemImg[length - 1]._Obj.GetComponent<Image>().sprite = null;
  84. itemImg[length - 1]._sprite = null;
  85. }
  86. void OnClickEvent(ItemImg item)
  87. {
  88. if (canMovie(item))
  89. {
  90. var t = NoneImage();
  91. t._sprite = item._sprite;
  92. t._Obj.GetComponent<Image>().sprite = item._Obj.GetComponent<Image>().sprite;
  93. item._sprite = null;
  94. item._Obj.GetComponent<Image>().sprite = null;
  95. }
  96. if (Success())
  97. {
  98. end.SetActive(true);
  99. Debug.Log("胜利");
  100. }
  101. }
  102. /// <summary>
  103. /// 判断是否可以移动
  104. /// </summary>
  105. bool canMovie(ItemImg item)
  106. {
  107. if (item._sprite == null)
  108. return false;
  109. var t = NoneImage();
  110. if ((t._col == item._col - 1 && t._row == item._row) || (t._col == item._col + 1 && t._row == item._row) || (t._col == item._col && t._row == item._row + 1) || (t._col == item._col && t._row == item._row - 1))
  111. {
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. /// <summary>
  120. /// 获取空白图片
  121. /// </summary>
  122. ItemImg NoneImage()
  123. {
  124. for (int i = 0; i < length; i++)
  125. {
  126. if (itemImg[i]._sprite == null)
  127. {
  128. return itemImg[i];
  129. }
  130. }
  131. return null;
  132. }
  133. /// <summary>
  134. /// 判断胜利
  135. /// </summary>
  136. bool Success()
  137. {
  138. for (int i = 0; i < length - 1; i++)
  139. {
  140. if (itemImg[i]._Obj.GetComponent<Image>().sprite != null)
  141. {
  142. if (itemImg[i]._Obj.GetComponent<Image>().sprite.name != "Image_02_" + i.ToString())
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. }
  149. public class ItemImg
  150. {
  151. public int _col;
  152. public int _row;
  153. public int _id;
  154. public GameObject _Obj;
  155. public Sprite _sprite;
  156. }