各位朋友大家好,今天我给大家分享一下我多年的工作经验中如何快速有效的使用Unity3D制作2048并且有分数 操作代码如下,希望对大家有所帮助。
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public UILabel valueLabel; bool gameover = false; void Start() { gameover = false; valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren<UILabel>(); valueLabel.text = "Game Start"; valueLabel.color = Color.green; } // Update is called once per frame void Update() { if (!gameover) { if (Input.GetKeyDown(KeyCode.D)) { moveR(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.A)) { moveL(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.W)) { moveU(); CreateNumber(); } else if (Input.GetKeyDown(KeyCode.S)) { moveD(); CreateNumber(); } } } void moveU() { for (int i = 1; i <= 4; i++) { bool flag = false; for (int j = 2; j <= 4; j++) { for (int k = j - 1; k >= 1; k--) { //获取当前元素 GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren<UILabel>(); //获取下一个元素 GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren<UILabel>(); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveD() { for (int i = 1; i <= 4; i++) { bool flag = false; for (int j = 3; j >= 1; j--) { for (int k = j + 1; k <= 4; k++) { //获取当前元素 GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren<UILabel>(); //获取下一个元素 GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren<UILabel>(); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveL() { for (int i = 1; i <= 4; i++) { bool flag = false; for (int j = 2; j <= 4; j++) { for (int k = j - 1; k >=1 ; k--) { //获取当前元素 GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren<UILabel>(); //获取下一个元素 GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren<UILabel>(); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void moveR() { for (int i = 1; i <= 4; i++) { bool flag = false; for (int j = 3; j >= 1; j--) { for (int k = j + 1; k <= 4; k++) { //获取当前元素 GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString()); print("当前对象" + go.name); UILabel I = go.GetComponentInChildren<UILabel>(); //获取下一个元素 GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString()); print("下一个对象" + goNext.name); UILabel INext = goNext.GetComponentInChildren<UILabel>(); //比较代码 if (I.text != "") { if (INext.text == "") { INext.text = I.text; I.text = ""; } else if (I.text == INext.text) { if (!flag) { int a = int.Parse(INext.text) + int.Parse(I.text); INext.text = a.ToString(); I.text = ""; flag = true; } } } } } } } void CreateNumber() { int count = 0; bool flag = false; for (int i = 1; i <= 4; i++) { if (!flag) { for (int j = 1; j <= 4; j++) { GameObject go = GameObject.Find("L" + i.ToString() + j.ToString()); UILabel label = go.GetComponentInChildren<UILabel>(); if (label.text == "") { int r = Random.Range(1, 10); if (r <= 5) { int value = Random.Range(1, 5); if (value < 4) label.text = "2"; else label.text = "4"; flag = true; break; } } else { if (++count == 16) { valueLabel.text = "Game Over"; valueLabel.color = Color.red; gameover = true; } } } } else break; } } }