unity教程之制作连连看⼩教程
哈哈,周末了总算有时间完善⼀下做的连连看了,顺便写个⼩教程分享给⼤家,哇哈哈~
⽂章出处
开始正题:
连连看的规则⼤家应该都知道,选中的两个图⽚相同,并且不能多于两个拐点能连在⼀块,这两个图⽚就可以消掉;
连通的类型:
1 直线型;
2 ⼀个拐点;
3 两个拐点;
下⾯开始介绍这三种连通类型
直线型:
直线型分两种,⼀种是横向,⼀种是竖向;
⾸先是横向连接
A,B两点的x坐标相同,图⽚类型相同,从A点开始到B点检测,如果AB两点之间没有其他图⽚就销毁AB两个图⽚,竖向的和横向的类似
⼀个拐点:
AB两点的x坐标和y坐标都不相同的时候开始检测⼀个拐点是否可以连接,通过AB两点计算出CD两点,然后分别检测AC,BC,AD,BD是否可以通过直线型连接到⼀起,显然AB两点可以通过A>C,C>B连接到⼀起,
两个拐点:
AB两点,从A开始,横向和竖向把所有和A能直线连接的点出来,⽤这些点和B点做⼀个拐点的检测,显然A点下边的那个点可以通过绿⾊的那个点可以通过⼀个拐点的⽅式和B点连接起来,
哈哈,是不是很简单啊,上边的就是连连看的核⼼内容
接下来详细介绍⼀下各个脚本的作⽤,(哇哈哈,以注释的形式给⼤家介绍吧)
GameManager.cs 游戏的核⼼代码,产⽣图⽚,判断是否可以销毁等
01. [color=#008ef1][font=宋体]using UnityEngine;[/font][/color]
02. using System.Collections;
03. using System.Collections.Generic;
04.
05. public class GameManager : MonoBehaviour
06. {
07. public DrawLine drawLine;//画线
08. public GameObject tilePrefab;//tile的预制
09. public List<Tile> tiles;//开始实例化的时候存放tile
10. public List<Tile> _tiles;//存放随机摆放的tile
11. public List<Tile> tilesEdge;//为了边界处可以有拐点,把棋盘四周的tile放在这⾥,这⾥的tile看不到
12. public int x, y;//的⼤⼩,两个数必须是偶数
13. private Tile tileA;
14. private Tile tileB;
15. private bool destroy;
16. private Vector3 mousePos;
17. private enum stepType//控制游戏的状态
18. {
19. one,
20. two,
21. three
22. }
23. private stepType _stepType;
24.
25. void Start ()
26. {
27. ansform.position = ;
28. Spawn ();
29. _stepType = ;
30. }
31.
32. private void Spawn ()//实例化tile
33. {
34. float num = (x * y - (2 * x + 2 * y - 4)) * 0.5f;
35. for (int i = 0; i <num; i ++) {
36. int idTex = Random.Range (20, 45);
37. GameObject obj = Instantiate (tilePrefab) as GameObject;
38. GameObject obj2 = Instantiate (tilePrefab) as GameObject;
39. Tile tile = obj.GetComponent<Tile> ();
40. Tile tile2 = obj2.GetComponent<Tile> ();
41. tile.Init (idTex);
42. tile2.Init (idTex);
43. tiles.Add (tile);
44. tiles.Add (tile2);
45. }
46. for (int i = 0; i<((2*x+2*y) -4); i++) {//实例化边缘的tile
47. GameObject obj = Instantiate (tilePrefab) as GameObject;
48. obj.name = "edage";
49. Tile tile = obj.GetComponent<Tile> ();
50. tilesEdge.Add (tile);
51. }
52. CreatTile ();
53. for (int i = 0; i < _tiles.Count; i++) {
54. _tiles [i].transform.name = i.ToString ();
55. _tiles [i].id = i;
56. }
57. ansform.position = new Vector3 (-(x / 2.0f - 0.5f), -(y / 2.0f - 0.5f), 0);
58. }
59.
60. private void CreatTile ()//随机摆放tile,如果是边缘的就放在边缘位置
61. {
62. int idTex = 0;
63. float _y = 0.0f;
64. for (int i = 0; i < y; i ++) {
65. float _x = 0.0f;
66. for (int j = 0; j < x; j ++) {
67. if (i == 0 || j == 0 || i == y - 1 || j == x - 1) {
68. tilesEdge [0].transform.position = new Vector3 (_x, _y, 0);
69. tilesEdge [0].pos = new Vector2 (_x, _y);
70. tilesEdge [0].ation = new Quaternion (0, 0, 180, 0);
71. tilesEdge [0].transform.parent = ansform;
72. _tiles.Add (tilesEdge [0]);
73. tilesEdge [0].transform.localScale = ;
74. tilesEdge [0].type = false;
75. tilesEdge.RemoveAt (0);
76. } else {
77. int id = Mathf.FloorToInt (Random.Range (0, tiles.Count));
78. tiles [id].transform.position = new Vector3 (_x, _y, 0);
79. tiles [id].pos = new Vector2 (_x, _y);
80. tiles [id].ation = new Quaternion (0, 0, 180, 0);
81. tiles [id].transform.parent = ansform;
82. _tiles.Add (tiles [id]);
83. tiles.RemoveAt (id);
84. }
85. _x += 1;
86. }
87. _y += 1;
88. }
89. }
90.
91. private void SelectTile ()//开始选择图⽚,通过射线⽅式选中,如果tileA和tileB不相同,则tileA等于tileB开始下⼀个检测
92. {
93. Ray ray = Camera.mainCamera.ScreenPointToRay (mousePos);
94. RaycastHit hit;
95. int mask = 1 << 8;
96. if (Physics.Raycast (ray, out hit, mask)) {
97. if (tileA == null) {
98. tileA = ansform.GetComponent<Tile> ();
99. tileA.SetTileTexture (1);
100. // print ("tileA = ansform.GetComponent<Tile> ();" + ansform.name); 101. } else {unity 教程
102. tileB = ansform.GetComponent<Tile> ();
103. tileB.SetTileTexture (1);
104. // print ("tileB = ansform.GetComponent<Tile> ();" + ansform.name); 105. Compare (tileA, tileB);
106. if (tileA == null ;; tileB == null) {
107.
108. // print ("a and b is null");
109. }
110. }
111. // ansform.GetComponent
112. }
113. }
114.
115. private void Compare (Tile tile1, Tile tile2)//⽐较两个点是否可以连接到⼀起
116. {
117. // same card
118. _stepType = ;
119. drawLine.waypoints.Add (ansform); //第⼀个选择的tile是画线的起始位置,
120. ansform.position = ansform.position;
121. destroy = false;
122. print ("compare");
123. if (tile1.pos.x == tile2.pos.x ;; tile1.pos.y == tile2.pos.y) {如果选中的是同⼀个图⽚返回124. tileA.SetTileTexture (0);
125. // tileB.SetTileTexture (0);
126. tileA = tileB;
127. tileB = null;
128. // tileA.SetTileTexture (1);
129. return;
130. } else if (tile1.pos.x == tile2.pos.x ;; tile1.pos.y != tile2.pos.y) {//如果两点的x相等,竖向检测131. print ("check y");
132. destroy = CheckY (tile1, tile2);
133. if (destroy)
134. drawLine.waypoints.Add (ansform);
135. } else if (tile1.pos.x != tile2.pos.x ;; tile1.pos.y == tile2.pos.y) {//如果两点的y相等,横向检测136. print ("check x");
137. destroy = CheckX (tile1, tile2);
138. if (destroy)
139. drawLine.waypoints.Add (ansform);
140. }
141. if (!destroy) {//不符合直线连接⽅式的开始进⾏⼀个拐点的检测
142. _stepType = stepType.two;
143. destroy = CheckTwoStep (tile1, tile2);
144. // print ("destroy = CheckTwoStep (tile1, tile1);:" + destroy);
145. print ("check two step");
146. if (!destroy) {//不符合直线和⼀个拐点检测的开始进⾏两个拐点的检测
147. _stepType = stepType.three;
148. destroy = CheckThreeStep (tile1, tile2);
149. print ("check three:" + destroy);
150. print ("tile1.idTex:" + tile1.idTex + "tile1.idTex:" + tile1.idTex);
151. }
152. }
153. if (destroy) {//如果符合销毁条件销毁图⽚,并开始画线
154. ansform.localScale = ;
155. ansform.localScale = ;
156. pe = false;
157. pe = false;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论