用钢体。list,二维数组生成10*10的矩阵的消消乐

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class DestroyTenAndTenRang : MonoBehaviour {
  6    public GameObject cube;
  7     public Material[] m;
  8     GameObject cubeObj;
  9     Vector3 vec;
 10     int num = 0;
 11     // Use this for initialization
 12     void Start () {
 13         for (int i = 0; i < 10; i++)
 14         {
 15             for (int j = 0; j < 10; j++)
 16             {
 17                 GameObject obj = Instantiate(cube);//用预制体创建物体
 18                 obj.transform.position = new Vector3(i, j, 0);
 19                 obj.transform.localScale = new Vector3(0.9f, 0.9f,0.9f);
 20                 obj.tag = "cube";//将名字都统一改成cube
 21                 obj.GetComponent<MeshRenderer>().material = m[Random.Range(0,m.Length)];//随机材质球的颜色
 22          
 23                 //让cube消除的时候不倒塌 用钢体方法
 24                 obj.AddComponent<Rigidbody>();//创建钢体
 25                 obj.GetComponent<Rigidbody>().freezeRotation = true;//把钢体 的xyz固定 不至于倒塌
 26                 obj.GetComponent<BoxCollider>().size=new Vector3(1.1f,1.1f,1.1f);//把钢体的出发范围增大 为了缝隙
 27 
 28             }
 29         }
 30     }
 31     
 32     // Update is called once per frame
 33     void Update () {
 34         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 35         RaycastHit hit;
 36         if (Physics.Raycast(ray,out hit))
 37         {
 38             if (Input.GetMouseButtonDown(0)&&hit.collider.gameObject.tag=="cube")
 39             {
 40                 cubeObj=hit.collider.gameObject;
 41                 cubeObj.transform.localScale=new Vector3(1, 1, 1);//变大地方
 42 
 43                 Destroy(cubeObj);
 44                 vec = cubeObj.transform.position;//存放消除的cube的位置坐标
 45 
 46                 GameObject c = Instantiate(cube);//消除后就新建一个cube 
 47                 c.transform.position = new Vector3(vec.x,9,vec.z);
 48                 c.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
 49                 //给新创建的cube都事实创建钢体,固定位置,新的钢体大小
 50                 c.AddComponent<Rigidbody>();
 51                 c.GetComponent<Rigidbody>().freezeRotation=true;
 52                 c.GetComponent<BoxCollider>().size = new Vector3(1.1f, 1.1f, 1.1f);
 53 
 54 
 55 
 56             }
 57         }
 58         
 59     }
 60 }
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 using System.Collections;
 69 using System.Collections.Generic;
 70 using UnityEngine;
 71 
 72 public class DestroyTenRangePostion : MonoBehaviour {
 73     public GameObject cube;
 74     public Material[] m;
 75     List<GameObject> list = new List<GameObject>();
 76     // Use this for initialization
 77     void Start () {
 78         for (int i = 0; i < 10; i++)
 79         {
 80             for (int j = 0; j < 10; j++)
 81             {
 82                 GameObject obj = Instantiate(cube);//用预制体创建物体
 83                 obj.transform.position = new Vector3(i, j, 0);
 84                 obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
 85                 obj.tag = "cube";//将名字都统一改成cube
 86                 obj.GetComponent<MeshRenderer>().material = m[Random.Range(0, m.Length)];//随机材质球的颜色
 87                 list.Add(obj);//往list里面添加物体
 88            
 89             }
 90         }
 91     }
 92     
 93     // Update is called once per frame
 94     void Update () {
 95         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//射线检测
 96         RaycastHit hit;//检测信息
 97         if (Physics.Raycast(ray,out hit))//是否检测到
 98         {
 99             if (hit.collider.tag=="cube"&&Input.GetMouseButtonDown(0))
100             {
101                 cube = hit.collider.gameObject;
102                 Vector3 pos = cube.transform.position;//存放cube的坐标
103                 Destroy(cube);//消除cube
104                 list.Remove(cube);//list里的就要移除
105                 foreach (var item in list)
106                 {
107                     if (item.transform.position.x==pos.x&&item.transform.position.y>pos.y)
108                     {
109                        // item.transform.position = new Vector3(pos.x, item.transform.position.y - 1, pos.z);//将销毁的物体的坐标上方的物体y轴减一
110                         item.transform.position -= new Vector3(0, 1, 0);//将销毁的物体y轴都减一
111                     }
112                 }
113                 GameObject obj = Instantiate(cube);//再加物体进入
114                 obj.transform.position = new Vector3(pos.x, 9, 0);
115                 obj.transform.localScale = new Vector3(0.9f,0.9f,0.9f);
116                 list.Add(obj);
117             }
118 
119         }
120         
121     }
122 }
123 
124 
125 
126 
127 
128 
129 using System.Collections;
130 using System.Collections.Generic;
131 using UnityEngine;
132 
133 public class ErWeiArrayXiaoxiaole : MonoBehaviour {
134     public GameObject cube;
135     public Material[] m;
136     GameObject[,] arr = new GameObject[10,10];//二维数组
137     // Use this for initialization
138     void Start () {
139         for (int i = 0; i < 10; i++)
140         {
141             for (int j = 0; j < 10; j++)
142             {
143                 GameObject obj = Instantiate(cube);//用预制体创建物体
144                 obj.transform.position = new Vector3(i, j, 0);
145                 obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
146                 obj.tag = "cube";//将名字都统一改成cube
147                 obj.GetComponent<MeshRenderer>().material = m[Random.Range(0, m.Length)];//随机材质球的颜色
148                 arr[i, j] = obj;//往二维数组中加物体
149             }
150         }
151 
152     }
153     
154     // Update is called once per frame
155     void Update () {
156         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
157         RaycastHit hit;
158         if (Physics.Raycast(ray,out hit))
159         {
160             if (hit.collider.tag=="cube"&&Input.GetMouseButtonDown(0))
161             {
162                 cube = hit.collider.gameObject;
163                 Vector3 vc = cube.transform.position;//存放cube的位置坐标
164                 int a = (int )vc.x;//逗号前面的数字
165                 int b = (int)vc.y;//逗号后的位置
166                 Destroy(cube);
167 
168                 for (int i = b+1; i < 10; i++)//索引删除物体上方的值,删除的物体索引是b
169                 {
170                     arr[a, i].transform.position += new Vector3(0,-1,0);//消除的物体上方的东西一次落下来
171                                                                        //找到比销毁物体坐标的y大的物体把其y-1
172                     arr[a, i - 1] = arr[a, i];//坐标统一减去 ,导致索引对应物体改变                                                 
173                 }
174                 GameObject obj = Instantiate(cube);
175                 obj.transform.position = new Vector3(vc.x, 9, 0);//新生成的物体坐标是从最上边起
176                 obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);//大小为以前一样
177                 obj.tag = "cube";
178                 obj.GetComponent<MeshRenderer>().material = m[Random.Range(0, 3)];
179                 arr[a, 9] = obj;//添加最上方的物体
180             }
181         }
182         
183     }
184 }
View Code

 

posted @ 2018-09-12 20:46  白纸菇凉  阅读(444)  评论(0编辑  收藏  举报