private void MoveNum() { bool isAnyNumMove = false; int countCombine = 0; //得到触摸或键盘输入的方向 if (Input.GetMouseButtonDown(0)) { mouseDownPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0) == false) { return; } TouchDir dir = GetTouchDir(); switch (dir) { case TouchDir.None: return; case TouchDir.Right: for (int y = 0; y < 4; y++) { Num preNum = null; int index = 4; for (int x = 3; x >= 0; x--) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index--; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(index, y, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index--; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Left: for (int y = 0; y < 4; y++) { Num preNum = null; int index = -1; for (int x = 0; x < 4; x++) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index++; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(index, y, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index++; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Top: for (int x = 0; x < 4; x++) { Num preNum = null; int index = -1; for (int y = 0; y < 4; y++) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index++; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(x, index, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index++; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Bottom: for (int x = 0; x < 4; x++) { Num preNum = null; int index = 4; for (int y = 3; y >= 0; y--) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index--; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(x, index, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index--; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; } if (countCombine > 0) { } if (isAnyNumMove) { GenerateNumber(); } if (CheckWin() || CheckGameOver()) { //显示出游戏结束的界面 } }