20270人加入学习
(121人评价)
Unity初级案例 - 贪吃蛇(Unity2017.2.0)

制作于2017.12.16

价格 免费

初级案例。

Toggle 

UI 

空格加速。

暂停。

分数换f

[展开全文]

Bg下建GameObject,重命名UP作为一个碰撞器,锚点Pos X:105,Pos Y:10,Width:1070,Height:30;加Box Collider 2D,Size X:-105,Y:0

复制粘贴UP,重命名Down,锚点Pos X:105,Pos Y:-10,Width:1070,Height:30;

复制粘贴Down,重命名Right,锚点Pos X:30,Pos Y:0,Width:70,Height:720;Size X:10,Y:720

复制粘贴Right,重命名Left,锚点Pos X:210,Pos Y:0,Width:10,Height:720;Size X:10,Y:720,Offset X:-20

给所有的主键挂上Image,导入BackGround,color:红色,

[展开全文]

分辨率

Build Setting /player setttings

不需要默认全屏。

resolution 分辨率。

ps 填充。制作背景。

[展开全文]

蛇身的生成

跟蛇头一样做预制体

上面要引入UI

用List来存蛇身

变量 拿预制体

还有一个图片数组 两段不同颜色

生成方法 实例化预制体 位置先不管

数组奇数偶数图片不一样  来一个判断index

?就是等于0 : 否则等于1

相同操作 找渲染器/ setParent 拿canvas

再把多的身子(transform)加入List

再在Move后调用

[展开全文]

在Canvas里建Image,重命名为Sh,把sh02拖入场景,    Pos X:0,Pos Y:0 Width:45 Height:45,新建Prefabs,将Sh拖入该文件,点击Apply。新建Scripts文件,在该文件下建SnaKeHead,双击打开,敲代码:

 {

public float velocity=0.35f;

public int step;

private int x;

private int y;

private Vector3 headPos;

void Start()

{

  InvokeRepeating("Move",0,velocity);

x=step;y=0;

}

void Update()

{

if(Input.GetKey(KeyCode.W))

{

 x=0;y=step;

 }

if(Input.GetKey(KeyCode.S))

{

 x=0;y=-step;

 }

if(Input.GetKey(KeyCode.A))

{

 x=-step;y=0;

 }

if(Input.GetKey(KeyCode.D))

{

 x=step;y=0;

 }

 

 void Move() 

 {

headPos=gameObject.transform.localPosition;

gameObject.transform.localPosition=new Vector3(headPos.x+x,headPos.y+y)

}

}

保存代码,然后将代码挂上,点击Apply,调整Step:30。

[展开全文]

UGUI

Carmera 渲染层,Canvas 改成用Camera 渲染

2D.  制作2d游戏,

alt加左键, 

锚点进行设置。

outline shadow 描边,阴影。

[展开全文]

 

 {

public float velocity=0.35f;

public int step;

private int x;

private int y;

private Vector3 headPos;

void Start()

{

  InvokeRepeating("Move",0,velocity);

x=0;y=step;

}

void Update()

{

 if(Input.GetKeyDown(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity-0.2f);

}

 if(Input.GetKeyUp(KeyCode.Space))

{

CancelInvoke();

InvokeRepeating("Move",0,velocity);

}

if(Input.GetKey(KeyCode.W)&&y!=-step)

{ gameObject.transform.localRotation=Quaternion.Euler(0,0,0);

 x=0;y=step;

 }

if(Input.GetKey(KeyCode.S)&&y!=step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,180);

 x=0;y=-step;

 }

if(Input.GetKey(KeyCode.A)&&x!=step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,90);

 x=-step;y=0;

 }

if(Input.GetKey(KeyCode.D)&&x!=-step)

{

gameObject.tranform.localRotation=Quaternion.Euler(0,0,-90);

 x=step;y=0;

 }

 

 void Move() 

 {

headPos=gameObject.transform.localPosition;

gameObject.transform.localPosition=new Vector3(headPos.x+x,headPos.y+y)

}

}

[展开全文]

在ControPanel下建Image,Width:40,Height:40,再写脚本FoodMaker

{

 public int xlimit=21;

public int ylimit=11;

public int xoffset=7;

public GameObject foodPrefab;

public Sprite[] foodSprites;

private Transform foodHolder;

void Start()

{

foodHolder=GameObject.Find("FoodHolder").transform;

MakeFood();

}

 

void MakeFood()

{

 int index=Random.Range(0,foodSprites.Length);

GameObject food=Instantiate(foodPrefab);

food.GetComponent<Image>().sprite=foodSprites[index];

food.transform.SetParent(foodHolder,false);

int x=Random.Range(-xlimit+xoffset,xlimit);

int y=Random.Range(-ylimit,ylimit);

food.transform.localPosition=new Vector3(x*30,y*30,0);

 

}

}

创建一个GameObject,重命名ScriptsHolder,将Food Maker放里,把Icon文件里的Food放入 Food Sprites。

[展开全文]

图片虚化

Toggle

锚点

UGUI有渲染顺序。

坦克那个是直接加的image。它根据order 

[展开全文]