MyPingTu.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(bool isinit=false)
  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. if(!isinit)
  80. {
  81. itemImg[i]._Obj.GetComponent<Button>().onClick.AddListener(delegate () {
  82. this.OnClickEvent(item);
  83. });
  84. }
  85. }
  86. itemImg[length - 1]._Obj.GetComponent<Image>().sprite = null;
  87. itemImg[length - 1]._sprite = null;
  88. }
  89. void OnClickEvent(ItemImg item)
  90. {
  91. if (canMovie(item))
  92. {
  93. var t = NoneImage();
  94. t._sprite = item._sprite;
  95. t._Obj.GetComponent<Image>().sprite = item._Obj.GetComponent<Image>().sprite;
  96. item._sprite = null;
  97. item._Obj.GetComponent<Image>().sprite = null;
  98. }
  99. if (Success())
  100. {
  101. end.SetActive(true);
  102. Debug.Log("胜利");
  103. }
  104. }
  105. /// <summary>
  106. /// 判断是否可以移动
  107. /// </summary>
  108. bool canMovie(ItemImg item)
  109. {
  110. if (item._sprite == null)
  111. return false;
  112. var t = NoneImage();
  113. 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))
  114. {
  115. return true;
  116. }
  117. else
  118. {
  119. return false;
  120. }
  121. }
  122. /// <summary>
  123. /// 获取空白图片
  124. /// </summary>
  125. ItemImg NoneImage()
  126. {
  127. for (int i = 0; i < length; i++)
  128. {
  129. if (itemImg[i]._sprite == null)
  130. {
  131. return itemImg[i];
  132. }
  133. }
  134. return null;
  135. }
  136. /// <summary>
  137. /// 判断胜利
  138. /// </summary>
  139. bool Success()
  140. {
  141. for (int i = 0; i < length - 1; i++)
  142. {
  143. if (itemImg[i]._Obj.GetComponent<Image>().sprite != null)
  144. {
  145. if (itemImg[i]._Obj.GetComponent<Image>().sprite.name != "Image_02_" + i.ToString())
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. private void OnEnable()
  152. {
  153. }
  154. }
  155. public class ItemImg
  156. {
  157. public int _col;
  158. public int _row;
  159. public int _id;
  160. public GameObject _Obj;
  161. public Sprite _sprite;
  162. }