摘要
上周面了一家小型游戏公司,见了一位领导(一般我们喜欢叫自己的上级老大,这样比较亲切)。当然结果肯定不算好,不过当时那个哥们问我做个2048我要多久,当时我不是很清楚,大概回了句2天。不过好像还是太小看自己了,昨天下午花了一下午,基本把游戏实现了。(当然这个游戏是开源的,在github上应该有源码,不过为了证明自己,肯定全自己手写)
架构
先预览下
U3D架构如下:
1.一个cavas容器来存储所有UI,参数:
render mode:screen space-camera;
render canmera:main camera;
UI scale mode:scale with scree size;
添加image组件,背景颜色最好白色
2.panel对象,用来装格子的预制体,
添加image组件,背景颜色自己选,在Rect Transform更改panel大小;
3.新建3个text文档,分别表示:
游戏结束显示;
再玩一次,记得添加button组件;
分数显示
4.新建空对象game,用来挂载游戏逻辑
5.创建方块预制体,一个100*100的image,挂一个子对象text用来显示格子的数字
脚本
FieldParameter挂在预制体下,主要是添加属性:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FieldParameter : MonoBehaviour {
public int row;
public int col;
}
GameControl脚本挂在game对象,在Inspector界面填入对应对象:
Rect输入预制体对象,Cavas输入panel对象,Over,Again,Score_text分别输入三个对应的文本对象(结束,再玩,分数)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameControl : MonoBehaviour {
private static GameObject[,] fields;
public GameObject rect;
public GameObject cavas;
public int score;
public bool gameover;
public Text over,again,score_text;
private int move_state; //1:上移 -1:下移 2:右移 -2:左移
// Use this for initialization
void Start () {
Game_Init ();
}
// Update is called once per frame
void Update () {
if (!gameover) {
Press ();
score_text.text = "SCORE:\n"+score.ToString();
}
}
//初始化
public void Game_Init(){
gameover = false;
over.enabled = false;
again.enabled = false;
score = 0;
fields = new GameObject[4, 4];
float distence = 110; //两个预制体的距离
Vector2 position_init=new Vector2(-177f,165f); //初始化预制体的位置
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
GameObject field = Instantiate (rect);
field.transform.parent = cavas.transform;
field.transform.localPosition = new Vector3 (position_init.x + distence * j, position_init.y - distence * i);
field.transform.localScale = new Vector3 (1, 1, 1);
field.GetComponent<Image> ().color = Color.grey;
field.GetComponent<FieldParameter> ().row = i;
field.GetComponent<FieldParameter> ().col = j;
field.GetComponentInChildren<Text>().text = "";
fields [i, j] = field;
}
}
Creat_Num ();
Creat_Num ();
}
//寻找Fileds中为""的field
private List<GameObject> Find_Field(){
List<GameObject> no_num = new List<GameObject> ();
foreach (GameObject item in fields) {
if (item.GetComponentInChildren<Text>().text.Equals("")) {
no_num.Add (item);
}
}
return no_num;
}
//随机生成数字
private GameObject Creat_Num(){
string num="2";
if (Random.value > 0.5) {
num = "4";
}
List<GameObject> no_num = Find_Field ();
if (no_num.Count > 0) {
int index = (int)(Random.value * no_num.Count);
no_num [index].GetComponentInChildren<Text> ().text = num;
no_num [index].GetComponent<Image> ().color = Color.white;
return no_num [index];
} else return null;
}
//上 下 左 右控制
void Press(){
if (Application.platform == RuntimePlatform.Android && Input.touchCount == 1 && Input.touches [0].phase == TouchPhase.Moved) {
float s01=Input.GetAxis("Mouse X");
float s02=Input.GetAxis("Mouse Y");
float distence = 5f;
if (s01 < -1 * distence && s02>-1*distence && s02<distence) {
move_state=-2;
} else if (s01 > distence && s02>-1*distence && s02<distence) {
move_state=2;
} else if (s02 > distence && s01>-1*distence && s01<distence) {
move_state=1