Unity初步 基本拼图实现
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 public class GameControl : MonoBehaviour {
7
8 //资源加载路径
9 public string spritePath = "Sprites/Teriri";
10
11 //拼图碎片
12 public Sprite[] sprites = null;
13
14 //拼图控制(以按钮的形式)
15 public Button[] buttons = null;
16
17 //携带网格方块自动布局组件的背景
18 public Transform bgTransform = null;
19
20 //碎片预制体
21 public GameObject puzzlePrefab = null;
22
23 //用于记录空图片的位置
24 public RectTransform nullImage = null;
25
26 //用于获取布局方块的大小,便于调节碎片
27 private GridLayoutGroup bgLayout = null;
28
29 //用于记录正确的图片名字相加顺序
30 private string correctSpritesNameOrder = null;
31
32 //用于确认按钮化的图片碎片是否正确
33 private string spritesNameOrder = null;
34
35 private Sprite remainSprite = null;//用于保存被置空的图片,最后游戏结束时现实出来
36
37
38
39 //此函数用于初始化
40 private void Start() {
41
42 string theLastSpriteName = null;
43
44 //资源加载
45 this.sprites = Resources.LoadAll<Sprite>(this.spritePath);
46 this.bgLayout = bgTransform.GetComponent<GridLayoutGroup>();
47
48 List<Sprite> tempSprites = new List<Sprite>();
49
50 //记录正确的精灵名字顺序
51 for (int i = 0; i < this.sprites.Length; i++) {
52 tempSprites.Add(this.sprites[i]);
53 this.correctSpritesNameOrder += this.sprites[i].name;
54 //记录下最后一张碎片的名字
55 if (i == this.sprites.Length - 1) {
56 theLastSpriteName = this.sprites[i].name;
57 }
58 }
59
60 print("正确的图片名字构成顺序:" + this.correctSpritesNameOrder.ToString());
61
62 int spritesLength = this.sprites.Length;
63 //Debug.Log(spritesLength);
64 for (int i = spritesLength - 1; i >= 0; i--) {
65 //实例化预制体
66 GameObject btnInstance = Instantiate(this.puzzlePrefab) as GameObject;
67 //获取按钮组件
68 Button btn = btnInstance.GetComponent<Button>();
69
70 GameControl self = this;//要保存一下当前的this对象,以免出现this指向错误
71 btn.onClick.AddListener(delegate () {
72 self.BtnOnclick(btnInstance.GetComponent<RectTransform>());
73 });
74
75 //随机取图片出来放置,达到打乱的效果
76 int randomNumber = Random.Range(0, tempSprites.Count);
77 btnInstance.name = tempSprites[randomNumber].name;
78
79 Image btnInstanceImage = btnInstance.GetComponent<Image>();
80 if (btnInstance.name == theLastSpriteName) {
81 //置空,否则打乱
82 this.remainSprite = tempSprites[randomNumber];
83 btnInstanceImage.sprite = null;
84 this.nullImage = btnInstance.GetComponent<RectTransform>();
85 } else {
86 //btnInstance.GetComponent<Image>().sprite = tempSprites[randomNumber];
87 btnInstanceImage.sprite = tempSprites[randomNumber];
88 }
89
90 //Debug.Log(btnInstanceImage);
91
92 tempSprites.Remove(tempSprites[randomNumber]);
93 btnInstance.transform.SetParent(bgTransform);
94 btnInstance.GetComponent<RectTransform>().localScale = Vector3.one;
95 //Debug.Log(randomNumber + ":" + btnInstance.name);
96 }
97 this.buttons = this.transform.Find("BackGround").GetComponentsInChildren<Button>();
98
99 ////随机放置一个空图片位置
100 //int tempNumber = Random.Range(0, this.buttons.Length);
101 //this.remainSprite = buttons[tempNumber].GetComponent<Image>().sprite;//保存被置空的图片
102 //this.buttons[tempNumber].GetComponent<Image>().sprite = null;
103 //this.nullImage = this.buttons[tempNumber].GetComponent<RectTransform>();
104
105 //设置最右下角的图片为消失的图片
106 }
107
108 //拼图按钮点击事件
109 private void BtnOnclick(RectTransform btnRect) {
110 if (Vector2.Distance(btnRect.anchoredPosition, this.nullImage.anchoredPosition)
111 <= (this.bgLayout.cellSize.x + this.bgLayout.spacing.x)) {
112 //print("产生替换");
113 //Sprite btnRectSprite = btnRect.GetComponent<Image>().sprite;//获取需要替换的引用
114 ////这个空位应该是啥也没有
115 //Sprite nullImageSprite = this.nullImage.GetComponent<Image>().sprite;
116 Sprite cacheSprite = btnRect.GetComponent<Image>().sprite;//交换前缓存一下图片
117 string cacheString = btnRect.gameObject.name;
118
119 btnRect.gameObject.name = this.nullImage.gameObject.name;
120 this.nullImage.gameObject.name = cacheString;
121
122 btnRect.GetComponent<Image>().sprite = this.nullImage.GetComponent<Image>().sprite;
123 this.nullImage.GetComponent<Image>().sprite = cacheSprite;
124 //this.nullImage.GetComponent<Image>().sprite = cacheSprite;
125 this.nullImage = btnRect;
126
127 CheckAll();
128 }
129 }
130
131 //遍历所有图片名字顺序,判断是否完成拼图,结束游戏
132 private void CheckAll() {
133 this.spritesNameOrder = "";
134 this.buttons = this.bgTransform.GetComponentsInChildren<Button>();
135 for (int i = 0; i < this.buttons.Length; i++) {
136 this.spritesNameOrder += this.buttons[i].gameObject.name;
137 }
138
139 if (this.spritesNameOrder == this.correctSpritesNameOrder) {
140 this.nullImage.GetComponent<Image>().sprite = this.remainSprite;//现实被置空的图片
141 Debug.Log("德丽莎天下第一可爱!");
142 this.enabled = false;//停止游戏刷新
143 }
144 }
145 }
结束: