Unity3D脚本:Unity制作连连看脚本

GameManager.cs 游戏的核心代码,产生图片,判断是否可以销毁等。

using System.Collections;
 
using System.Collections.Generic;
 
public class GameManager : MonoBehaviour
 
{
 
public DrawLine drawLine;//画线
 
public GameObject tilePrefab;//tile的预制
 
public List tiles;//开始实例化的时候存放tile
 
public List _tiles;//存放随机摆放的tile
 
public List tilesEdge;//为了边界处可以有拐点,把棋盘四周的tile放在这里,这里的tile看不到
 
public int x, y;//棋牌的大小,两个数必须是偶数
 
private Tile tileA;
 
private Tile tileB;
 
private bool destroy;
 
private Vector3 mousePos;
 
private enum stepType//控制游戏的状态
 
{
 
one,
 
two,
 
three
 
}
 
private stepType _stepType;
 
void Start ()
 
{
 
this.gameObject.transform.position = Vector3.zero;
 
Spawn ();
 
_stepType = stepType.one;
 
}private void Spawn ()//实例化tile
 
{
 
float num = (x * y - (2 * x + 2 * y - 4)) * 0.5f;
 
for (int i = 0; i <num; i ++) {
 
int idTex = Random.Range (20, 45);
 
GameObject obj = Instantiate (tilePrefab) as GameObject;
 
GameObject obj2 = Instantiate (tilePrefab) as GameObject;
 
Tile tile = obj.GetComponent ();
 
Tile tile2 = obj2.GetComponent ();
 
tile.Init (idTex);
 
tile2.Init (idTex);
 
tiles.Add (tile);
 
tiles.Add (tile2);
 
}
 
for (int i = 0; i<((2*x+2*y) -4); i++) {//实例化边缘的tile
 
GameObject obj = Instantiate (tilePrefab) as GameObject;
 
obj.name = "edage";
 
Tile tile = obj.GetComponent ();
 
tilesEdge.Add (tile);
 
}
 
CreatTile ();
 
for (int i = 0; i < _tiles.Count; i++) {
 
_tiles [i].transform.name = i.ToString ();
 
_tiles [i].id = i;
 
}
 
this.transform.position = new Vector3 (-(x / 2.0f - 0.5f), -(y / 2.0f - 0.5f), 0);
 
}private void CreatTile ()//随机摆放tile,如果是边缘的就放在边缘位置
 
{
 
int idTex = 0;
 
float _y = 0.0f;
 
for (int i = 0; i < y; i ++) {
 
float _x = 0.0f;
 
for (int j = 0; j < x; j ++) {
 
if (i == 0 || j == 0 || i == y - 1 || j == x - 1) {
 
tilesEdge [0].transform.position = new Vector3 (_x, _y, 0);
 
tilesEdge [0].pos = new Vector2 (_x, _y);
 
tilesEdge [0].transform.rotation = new Quaternion (0, 0, 180, 0);
 
tilesEdge [0].transform.parent = this.gameObject.transform;
 
_tiles.Add (tilesEdge [0]);
 
tilesEdge [0].transform.localScale = Vector3.zero;
 
tilesEdge [0].type = false;
 
tilesEdge.RemoveAt (0);
 
} else {
 
int id = Mathf.FloorToInt (Random.Range (0, tiles.Count));
 
tiles [id].transform.position = new Vector3 (_x, _y, 0);
 
tiles [id].pos = new Vector2 (_x, _y);
 
tiles [id].transform.rotation = new Quaternion (0, 0, 180, 0);
 
tiles [id].transform.parent = this.gameObject.transform;
 
_tiles.Add (tiles [id]);
 
tiles.RemoveAt (id);
 
}
 
_x += 1;
 
}
 
_y += 1;
 
}
 
}private void SelectTile ()//开始选择图片,通过射线方式选中,如果tileA和tileB不相同,则tileA等于tileB开始下一个检测
 
{
 
Ray ray = Camera.mainCamera.ScreenPointToRay (mousePos);
 
RaycastHit hit;
 
int mask = 1 << 8;
 
if (Physics.Raycast (ray, out hit, mask)) {
 
if (tileA == null) {
 
tileA = hit.transform.GetComponent ();
 
tileA.SetTileTexture (1);
 
// print ("tileA = hit.transform.GetComponent ();" + tileA.transform.name);
 
} else {
 
tileB = hit.transform.GetComponent ();
 
tileB.SetTileTexture (1);
 
// print ("tileB = hit.transform.GetComponent ();" + tileB.transform.name);
 
Compare (tileA, tileB);
 
if (tileA == null && tileB == null) {// print ("a and b is null");
 
}
 
}
 
// hit.transform.GetComponent
 
}
 
}
 
private void Compare (Tile tile1, Tile tile2)//比较两个点是否可以连接到一起
 
{
 
// same card
 
_stepType = stepType.one;
 
drawLine.waypoints.Add (tile1.transform); //第一个选择的tile是画线的起始位置,
 
drawLine.transform.position = tile1.transform.position;
 
destroy = false;
 
print ("compare");
 
if (tile1.pos.x == tile2.pos.x && tile1.pos.y == tile2.pos.y) {如果选中的是同一个图片返回
 
tileA.SetTileTexture (0);
 
//            tileB.SetTileTexture (0);
 
tileA = tileB;
 
tileB = null;
 
//            tileA.SetTileTexture (1);
 
return;
 
} else if (tile1.pos.x == tile2.pos.x && tile1.pos.y != tile2.pos.y) {//如果两点的x相等,竖向检测
 
print ("check y");
 
destroy = CheckY (tile1, tile2);
 
if (destroy)
 
drawLine.waypoints.Add (tile2.transform);
 
} else if (tile1.pos.x != tile2.pos.x && tile1.pos.y == tile2.pos.y) {//如果两点的y相等,横向检测
 
print ("check x");
 
destroy = CheckX (tile1, tile2);
 
if (destroy)
 
drawLine.waypoints.Add (tile2.transform);
 
}
 
if (!destroy) {//不符合直线连接方式的开始进行一个拐点的检测
 
_stepType = stepType.two;
 
destroy = CheckTwoStep (tile1, tile2);
 
//            print ("destroy = CheckTwoStep (tile1, tile1);:" + destroy);
 
print ("check two step");
 
if (!destroy) {//不符合直线和一个拐点检测的开始进行两个拐点的检测
 
_stepType = stepType.three;
 
destroy = CheckThreeStep (tile1, tile2);
 
print ("check three:" + destroy);
 
print ("tile1.idTex:" + tile1.idTex + "tile1.idTex:" + tile1.idTex);
 
}
 
}
 
if (destroy) {//如果符合销毁条件销毁图片,并开始画线
 
tile1.transform.localScale = Vector3.zero;
 
tile2.transform.localScale = Vector3.zero;
 
tile1.type = false;
 
tile2.type = false;
 
tileA = null;
 
tileB = null;
 
drawLine.MoveToWaypoint ();
 
} else {//不符合的话,清除画线中的路径
 
drawLine.ClearPath ();
 
tileA.SetTileTexture (0);
 
//            tileB.SetTileTexture (0);
 
tileA = tileB;
 
tileB = null;
 
return;
 
}
 
}
 
// one step横向检测
 
private bool CheckX (Tile a, Tile b)
 
{
 
bool compare = true;
 
int _min, _max;
 
if (a.idTex == b.idTex) {
 
CompareID (a, b, out _min, out _max);
 
_min += 1;
 
if (_min == _max)
 
return true;
 
for (int i = _min; i < _max; i++) {
 
if (_tiles [i].type == true) {
 
compare = false;
 
break;
 
}
 
}
 
return compare;
 
} else
 
return false;
 
}
 
//竖向检测
 
private bool CheckY (Tile a, Tile b)
 
{
 
bool compare = true;
 
int _min, _max;
 
//        int idA = (int)(a.x * x + a.y);
 
//        int idB = (int)(b.x * x + b.y);
 
//        print (_tiles [idA].id.ToString () + "idA:" + idA);
 
//        print (_tiles [idB].id.ToString () + "idB:" + idB);
 
//        print ("a.idtex:" + a.idTex + "b.idtex:" + b.idTex);
 
if (a.idTex == b.idTex) {
 
CompareID (a, b, out _min, out _max);
 
_min += x;
 
if (_min == _max)
 
return true;
 
for (int i = _min; i < _max; i+=x) {
 
//                print ("1step");
 
if (_tiles [i].type == true) {
 
compare = false;
 
break;
 
}
 
}
 
//            if (compare) {
 
//                print ("2step");
 
//                a.type = false;
 
//                b.type = false;
 
//            }
 
return compare;
 
} else
 
return false;
 
}
 
// two step一个拐点的检测
 
private bool CheckTwoStep (Tile a, Tile b)
 
{
 
if (a.pos.x == b.pos.x || a.pos.y == b.pos.y)
 
return false;
 
int id1 = (int)(a.pos.y * x + b.pos.x);
 
if (_tiles [id1].type == false) {
 
_tiles [id1].idTex = a.idTex;
 
if (CheckY (_tiles [id1], b)) {
 
if (CheckX (a, _tiles [id1])) {
 
if (_stepType == stepType.two) {
 
drawLine.waypoints.Add (_tiles [id1].transform);
 
drawLine.waypoints.Add (b.transform);
 
} else if (_stepType == stepType.three) {
 
drawLine.waypoints.Add (_tiles [id1].transform);
 
print ("=====================:" + 1);
 
}
 
return true;
 
}
 
//                else
 
//                    return false;
 
}
 
}
 
int id2 = (int)(b.pos.y * x + a.pos.x);
 
if (_tiles [id2].type == false) {
 
_tiles [id2].idTex = b.idTex;
 
if (CheckY (a, _tiles [id2])) {
 
if (CheckX (b, _tiles [id2])) {
 
if (_stepType == stepType.two) {
 
drawLine.waypoints.Add (_tiles [id2].transform);
 
drawLine.waypoints.Add (b.transform);
 
} else if (_stepType == stepType.three) {
 
drawLine.waypoints.Add (_tiles [id2].transform);
 
print ("=====================:" + 2);
 
}
 
return true;
 
}
 
//                else
 
//                    return false;
 
}
 
}
 
return false;
 
}
 
// three step两个拐点的检测
 
private bool CheckThreeStep (Tile a, Tile b)
 
{
 
print ("a:" + a.idTex + "b:" + b.idTex);
 
//        if (a.pos.x == b.pos.x || a.pos.y == b.pos.y) return false;
 
bool returnValue = false;
 
print ("returnValue:" + returnValue);
 
List _comparrPointsB;
 
ComparePoints (b, out _comparrPointsB);//返回b点可以横竖直线相连的点
 
for (int i =0; i<_comparrPointsB.Count; i++) {
 
returnValue = CheckTwoStep (a, _comparrPointsB [i]);
 
if (returnValue) {
 
drawLine.waypoints.Add (_comparrPointsB [i].transform);
 
drawLine.waypoints.Add (b.transform);
 
return returnValue;
 
}
 
}
 
if (!returnValue) {
 
List _comparrPointsA;
 
ComparePoints (a, out _comparrPointsA);
 
print (a.name);
 
print (b.name);
 
for (int i =0; i<_comparrPointsA.Count; i++) {
 
print ("--------------" + b.idTex);
 
returnValue = CheckTwoStep (b, _comparrPointsA [i]);
 
if (returnValue) {
 
drawLine.waypoints.Add (_comparrPointsA [i].transform);
 
drawLine.waypoints.Add (b.transform);
 
return returnValue;
 
}
 
}
 
}
 
return returnValue;
 
}
 
//两个拐点的时候返回可以与a横竖直线相连的点
 
private void ComparePoints (Tile a, out List comparePoints)
 
{
 
print ("a.idtex" + a.idTex);
 
comparePoints = new List ();
 
comparePoints.Clear ();
 
//        for (int i = 0; i < y; i ++) { //            if (i != a.y) { //                int id = (int)(i * x + a.pos.x); //                if (_tiles [id].type == false) { //                    comparePoints.Add (_tiles [id]); //                    _tiles [id].idTex = a.idTex; //                } //            } //        } for (int i = (int)a.pos.y - 1; i >-1; i--) {
 
int id = (int)(i * x + a.pos.x);
 
//            print ("three step :" + id);
 
if (_tiles [id].type == false) {
 
comparePoints.Add (_tiles [id]);
 
_tiles [id].idTex = a.idTex;
 
print ("_tiles [id].idTex = a.idTex; " + _tiles [id].idTex);
 
} else
 
break;
 
}
 
for (int i = (int)a.pos.y + 1; i < y; i++) { int id = (int)(i * x + a.pos.x); //            print ("three step :" + id); if (_tiles [id].type == false) { comparePoints.Add (_tiles [id]); _tiles [id].idTex = a.idTex; print ("_tiles [id].idTex = a.idTex; " + _tiles [id].idTex); } else break; } for (int i = (int)a.pos.x -1; i >-1; i --) {
 
int id = (int)(a.pos.y * x + i);
 
if (_tiles [id].type == false) {
 
comparePoints.Add (_tiles [id]);
 
_tiles [id].idTex = a.idTex;
 
print ("_tiles [id].idTex = a.idTex; " + _tiles [id].idTex);
 
} else
 
break;
 
}
 
for (int i = (int)a.pos.x +1; i < x; i ++) {
 
int id = (int)(a.pos.y * x + i);
 
if (_tiles [id].type == false) {
 
comparePoints.Add (_tiles [id]);
 
_tiles [id].idTex = a.idTex;
 
print ("_tiles [id].idTex = a.idTex; " + _tiles [id].idTex);
 
} else
 
break;
 
}
 
//        for (int i = 0; i < x; i ++) {
 
//            if (i != a.x) {
 
//                int id = (int)(a.pos.y * x + i);
 
//                if (_tiles [id].type == false) {
 
//                    comparePoints.Add (_tiles [id]);
 
//                    _tiles [id].idTex = a.idTex;
 
//                }
 
//            }
 
//        }
 
}
 
private void CompareID (Tile a, Tile b, out int min, out int max)
 
{
 
if (a.id < b.id) {
 
min = a.id;
 
max = b.id;
 
} else {
 
min = b.id;
 
max = a.id;
 
}
 
}
 
Vector2 TexSize ()
 
{
 
Vector2 size = new Vector2 (1 / x, 1 / y);
 
return size;
 
}
 
Vector2 TexOffset (int _idTex)
 
{
 
int a = (int)(_idTex / x);
 
//        print (a + "a:" + _idTex);
 
int b = (int)(_idTex % x);
 
//        print (b + "b:" + _idTex);
 
Vector2 offset = new Vector2 (b / x, (y - 1 - a) / y);
 
return offset;
 
}
 
void Update ()
 
{
 
if (Input.GetMouseButtonUp (0)) {
 
mousePos = Input.mousePosition;
 
SelectTile ();
 
}
 
}
 
private void ClearTiles (List tiles)
 
{
 
tiles.Clear ();
 
//         this.gameObject.transform.DetachChildren();
 
}
 
}

DrawLine.cs,画线脚本,用的itween。

using UnityEngine;
 
using System.Collections;
 
using System.Collections.Generic;
 
public class DrawLine : MonoBehaviour
 
{
 
public List waypoints = new List ();
 
public float rate = 1;
 
private int currentWaypoint = 1;
 
public void MoveToWaypoint ()
 
{
 
print ("public void MoveToWaypoint (): " + waypoints.Count);
 
StartCoroutine ("move");
 
}
 
public void ClearPath ()
 
{
 
waypoints.Clear ();
 
print ("path.Clear ();");
 
}
 
IEnumerator move ()
 
{
 
for (int i = 0; i < waypoints.Count; i++) {
 
iTween.MoveTo (this.gameObject, waypoints [i].position, rate);
 
print ("now id:" + i);
 
yield return new WaitForSeconds(rate);
 
}
 
waypoints.Clear ();
 
}
 
}

Tile.cs

using UnityEngine;
 
using System.Collections;
 
public class Tile : MonoBehaviour
 
{
 
public int id;
 
public int idTex; //通过这个判断两个图片是否相同
 
public Vector2 pos ;
 
public bool type = true;//控制图片的状态,当销毁的时候为false,其他判断的时候可以通过该点
 
public float x, y;
 
public Texture texA, texB;//鼠标选中的时候可以换贴图
 
public GameObject mask;//鼠标选中的时候上边显示的框框
 
public void Init (int _idTex)
 
{
 
idTex = _idTex;
 
Vector2 offset = TexOffset (_idTex);
 
this.renderer.material.SetTextureOffset ("_MainTex", offset);
 
this.renderer.material.SetTextureScale ("_MainTex", new Vector2 (0.2f, 0.1f));}
 
//设置tile显示的贴图和大小
 
public void SetTileTexture (int i)
 
{
 
if (i == 0) {
 
this.renderer.material.mainTexture = texA;
 
mask.transform.localScale = Vector3.zero;
 
}if (i == 1) {
 
this.renderer.material.mainTexture = texB;
 
mask.transform.localScale = new  Vector3 (0.1380835f, 0.1380835f, 0.1380835f);
 
}
 
}
 
//这个就是裁剪一张大图,变成一个个小的,贴到tile上
 
Vector2 TexOffset (int _idTex)
 
{
 
int a = (int)(_idTex / x);
 
int b = (int)(_idTex % x);
 
Vector2 offset = new Vector2 (b / x, (y - 1 - a) / y);
 
return offset;
 
}
 
Vector2 TexSize ()
 
{
 
Vector2 size = new Vector2 (1 / x, 1 / y);
 
return size;
 
}
 
}

Menu.cs,添加两个按钮

using UnityEngine;
 
using System.Collections;
 
public class Menu : MonoBehaviour
 
{
 
public GameManager gameManager;
 
private GameManager _gameManger;
 
private bool start = true;void OnGUI ()
 
{
 
if (start) {
 
if (GUI.Button (new Rect (10, 10, 100, 50), "start")) {
 
start = false;
 
_gameManger = Instantiate (gameManager) as GameManager;
 
}
 
}
 
if (GUI.Button (new Rect (10, 70, 100, 50), "restart")) {
 
if (_gameManger != null) {
 
Destroy (_gameManger.gameObject);
 
print ("Destroy(_gameManger.gameObject);");
 
}
 
_gameManger = Instantiate (gameManager) as GameManager;
 
}}
 
}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值