123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using System;
- public class MyPingTu : MonoBehaviour
- {
- public GameObject end;
- ItemImg[] itemImg;
- Sprite[] UIShowImage;
- int length = 0;
- int sqrt = 0;
- void Start()
- {
- var RSLoadAllImages = Resources.LoadAll("Texture1");
- length = transform.childCount;
- sqrt = (int)Mathf.Sqrt(length);
- Debug.Log("sqrt==>" + sqrt);
- Debug.Log("length==>" + length);
- //childLength-1——其中某一张为空白,即后下角为空白
- UIShowImage = new Sprite[length];
- for (int i = 0; i < length; i++)
- {
- UIShowImage[i] = (Sprite)RSLoadAllImages[i + 1];
- }
- //初始化图片的id,col,row
- InitItemImg(length);
- //打乱图片
- Shuffle(UIShowImage);
- InitShow();
- }
- /// <summary>
- /// 数组打乱
- /// </summary>
- T[] Shuffle<T>(T[] Array)
- {
- for (int i = Array.Length-3; i < Array.Length-1; i++)
- {
- T temp = Array[i];
- int randomIndex = i+1;//UnityEngine.Random.Range(Array.Length-2, Array.Length-1);
- Array[i] = Array[randomIndex];
- Array[randomIndex] = temp;
- }
- /*
- for (int i = 0; i < Array.Length - 1; i++)
- {
- T temp = Array[i];
- int randomIndex = UnityEngine.Random.Range(0, Array.Length-1);
- Array[i] = Array[randomIndex];
- Array[randomIndex] = temp;
- }*/
- return Array;
- }
- /// <summary>
- /// 初始化图片id,col,row
- /// </summary>
- void InitItemImg(int count)
- {
- itemImg = new ItemImg[length];
- for (int i = 0; i < count; i++)
- {
- itemImg[i] = new ItemImg();
- itemImg[i]._id = i;
- itemImg[i]._col = i / sqrt;
- itemImg[i]._row = i % sqrt;
- }
- }
- void InitShow()
- {
- //初始化显示图片
- for (int i = 0; i < UIShowImage.Length; ++i)
- {
- var t = transform.GetChild(i);
- t.GetComponent<Image>().sprite = UIShowImage[i];
- //指定Sprite
- itemImg[i]._sprite = t.GetComponent<Image>().sprite;
- //指定Obj
- itemImg[i]._Obj = t.gameObject;
- var item = itemImg[i];
- itemImg[i]._Obj.GetComponent<Button>().onClick.AddListener(delegate () {
- this.OnClickEvent(item);
- });
- }
- itemImg[length - 1]._Obj.GetComponent<Image>().sprite = null;
- itemImg[length - 1]._sprite = null;
- }
- void OnClickEvent(ItemImg item)
- {
- if (canMovie(item))
- {
- var t = NoneImage();
- t._sprite = item._sprite;
- t._Obj.GetComponent<Image>().sprite = item._Obj.GetComponent<Image>().sprite;
- item._sprite = null;
- item._Obj.GetComponent<Image>().sprite = null;
- }
- if (Success())
- {
- end.SetActive(true);
- Debug.Log("胜利");
- }
- }
- /// <summary>
- /// 判断是否可以移动
- /// </summary>
- bool canMovie(ItemImg item)
- {
- if (item._sprite == null)
- return false;
- var t = NoneImage();
- 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))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 获取空白图片
- /// </summary>
- ItemImg NoneImage()
- {
- for (int i = 0; i < length; i++)
- {
- if (itemImg[i]._sprite == null)
- {
- return itemImg[i];
- }
- }
- return null;
- }
- /// <summary>
- /// 判断胜利
- /// </summary>
- bool Success()
- {
- for (int i = 0; i < length - 1; i++)
- {
- if (itemImg[i]._Obj.GetComponent<Image>().sprite != null)
- {
- if (itemImg[i]._Obj.GetComponent<Image>().sprite.name != "Image_02_" + i.ToString())
- return false;
- }
- }
- return true;
- }
- }
- public class ItemImg
- {
- public int _col;
- public int _row;
- public int _id;
- public GameObject _Obj;
- public Sprite _sprite;
- }
|