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(); } /// /// 数组打乱 /// T[] Shuffle(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; } /// /// 初始化图片id,col,row /// 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(bool isinit=false) { //初始化显示图片 for (int i = 0; i < UIShowImage.Length; ++i) { var t = transform.GetChild(i); t.GetComponent().sprite = UIShowImage[i]; //指定Sprite itemImg[i]._sprite = t.GetComponent().sprite; //指定Obj itemImg[i]._Obj = t.gameObject; var item = itemImg[i]; if(!isinit) { itemImg[i]._Obj.GetComponent