消消乐——三消

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class xxllianxi : MonoBehaviour {

    GameObject[,] cubes = new GameObject[10, 10];
    Vector3 p1;
    Vector3 p2;
    bool b = false;
    public Text text;
    AudioSource au;
    // Use this for initialization
    void Start () {
        au = GetComponent<AudioSource>();//初始化
        text = text.GetComponent<Text>();
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                GameObject obj = Instantiate(Resources.Load("cube" + Random.Range(1, 9))) as GameObject;//寻找预制体生成cube
                obj.transform.position = new Vector3(i, j, 0);//给定坐标
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                cubes[i, j] = obj;//存入二维数组


            }
        }
    }
    int num = 0;//分数
    int temp = 0;
    List<GameObject> listcube = new List<GameObject>();//点击的cube存入数组
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//射线检测
        RaycastHit hit;//检测信息
        if (listcube.Count == 1)
        {
            listcube[0].transform.Rotate(new Vector3(0, 1.5f, 0));//点击的cube以y轴旋转
        }
        if(Physics.Raycast(ray,out hit))
        {
            if (hit.collider.tag.Equals("cube"))//判断射线检测的是否是cube
            {
                if (Input.GetMouseButtonDown(0))
                {
                    listcube.Add(hit.collider.gameObject);
                    if (listcube.Count == 2)//满足两个后判断条件是否交换
                    {
                        if (Vector3.Distance(listcube[0].transform.position, listcube[1].transform.position) < 1.2f)
                        {
                            temp = num;//将分数赋值给temp
                            p1 = listcube[0].transform.position;
                            p2 = listcube[1].transform.position;

                            listcube[0].transform.LookAt(listcube[1].transform.position);
                            listcube[1].transform.LookAt(listcube[0].transform.position);//互相看向对方

                            b = true;
                        }
                    }
                }
            }
        }
        CheckY();
        CheckX();
        DestoryCube();

        if (b)
        {
            listcube[0].transform.Translate(new Vector3(0, 0, 0.05f));
            listcube[1].transform.Translate(new Vector3(0, 0, 0.05f));
            if (Vector3.Distance(listcube[0].transform.position, p1) >= 1)
            {
                if (Vector3.Distance(listcube[1].transform.position, p2) >= 1)
                {
                listcube[0].transform.position = p2;//交换位置
                    listcube[1].transform.position = p1;
                    for (int i = 0; i < listcube.Count; i++)
                    {
                        int a = (int)listcube[i].transform.position.x;
                        int b = (int)listcube[i].transform.position.y;
                        cubes[a, b] = listcube[i];//数组中换位置
                    }
                    b = false;
                    CheckY();
                    CheckX();
                    DestoryCube();
                    if (temp == num)
                    {
                        listcube[0].transform.position = p1;
                        listcube[1].transform.position = p2;//分数相等时,不换位置

                    }
                    listcube[0].transform.rotation = Quaternion.identity;//无旋转
                    listcube.Clear();//清空数组
                }

            }
        }
    }
    public void CheckY()//检查列是否有连续三个以上的
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 8; j++)//三个为一组检查,一共10个cube要检查八次
            {
                if (cubes[i, j].name == cubes[i, j + 1].name && cubes[i, j].name == cubes[i, j + 2].name)//如果三个名字一样就销毁
                {
                    for (int k = 0; k < 3; k++)
                    {
                    SaveDestoryCubes(cubes[i, j + k]);

                    }
                }
            }
        }
    }
    public void CheckX()//检查行是否有连续三个以上的
    {
        for (int j = 0; j < 10; j++)
        {
            for (int i = 0; i < 8; i++)//三个为一组检查,一共10个cube要检查八次
            {
                if (cubes[i, j].name == cubes[i + 1, j].name && cubes[i, j].name == cubes[i + 2, j].name)//如果三个名字一样就销毁
                {
                    for (int k = 0; k < 3; k++)
                    {
                        SaveDestoryCubes(cubes[i + k, j]);
                    }
                }
            }
        }
    }

    List<GameObject> listcubeDestory = new List<GameObject>();//要销毁的cube数组
    public void SaveDestoryCubes(GameObject obj)
    {
        if (listcubeDestory.Count == 0)
        {
            listcubeDestory.Add(obj);
        }
        else
        {
            if (!listcubeDestory.Contains(obj))//如果已经存进去了,就不能重复存取
            {
                listcubeDestory.Add(obj);
            }
        }
    }
    public void DestoryCube()
    {
        if (listcubeDestory.Count > 0)
        {
            for (int i = 0; i < listcubeDestory.Count; i++)
            {
                Vector3 vec = listcubeDestory[0].transform.position;//把销毁cube的坐标记录下来
                Destroy(listcubeDestory[0]);//销毁
                listcubeDestory.RemoveAt(0);//移除数组中的第一个
                num++;//得分加1
                au.Play();//音效播放
                text.text = num.ToString();//转字符串
                int a = (int)vec.x;
                int b = (int)vec.y;
                for (int j = b+1; j < 10; j++)
                {
                    cubes[a, j].transform.position = new Vector3(vec.x, j - 1, vec.z);//把当前销毁的物体的坐标y值大的本列物体下移一位
                    cubes[a, j - 1] = cubes[a, j];//赋值索引


                }
                GameObject obj = Instantiate(Resources.Load("cube" + Random.Range(1, 9))) as GameObject;
                obj.transform.position = new Vector3(a, 9, 0);
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                cubes[a, 9] = obj;
            }
        }
    }
}

此代码还有点问题,如果点击的两个cube相隔两个以外,game界面会卡死,,,,等我修改后再来更新

莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
原文地址:https://www.cnblogs.com/huang--wei/p/9609181.html