【Unity3d】学习笔记(5)
目录
前言
我学习Unity3d的方法是通过实战案例来学习,碰到不知道的概念在反过来查找并理解概念的意思。我认为这样比单纯的了解一大堆概念之后在开始实践要更有趣,更有动力,理解也更深。但是,同时我也认为这种方法比较适合有一些编程经验的人,一些程序设计的通用概念你可能已经驾轻就熟,而Unity特有的概念可能与你熟知的概念有相似性,关联他们,有助于你加深理解。
正文
今天遇到了以下概念:
- Sprite Renderer
- Sprite Editor:Pivot
- Format
- 速度(velocity)
- Input
Sprite Renderer
可以在Sprite Renderer中设置Sorting Layer和Order in Layer方便的展示我们的Sprite图片。2D游戏的动画通常在Sprite Renderer中按一定时间变换帧来实现:
idleUpTimer += Time.deltaTime;
if(idleUpTimer > animTimeInterval){
idleUpTimer -= animTimeInterval;
idleUpIndex++;
idleUpIndex %= idleUpLength;
upRenderer.sprite = idleUpSpriteArray[idleUpIndex];
}
downRenderer.sprite = idleDownSprite;
其中upRenderer是SpriteRenderer类型,idleUpSpriteArray是Sprite类型的数组。
Sprite Editor
我们的Sprite图片可以在Sprite Editor中编辑,如设置支点(Pivot)。
Format
通常将图片的texture type选择为Sprite后,可以将format选择为true color防止失真。
速度(velocity)
今天用到较多刚体中的velocity属性。
//player direction
float x = 1;
if (myRigidBody.velocity.x > 0.05f) {
x = -1;
} else if (myRigidBody.velocity.x < 0.05f) {
x = 1;
} else {
x = 0;
}
代码中判断了刚体x方向上的速度。我们通过Input.GetAxis(“Horizontal”)获取水平输入量,然后改变角色位置时就回改变刚体x方向上的速度,从而令以上代码生效改变角色方向。
Input
今天又用到Input.GetKeyDown来检测按键,用法看代码就能理解:
if (Input.GetKeyDown (KeyCode.S)) {
isBottomBtnClick = true;
}
if (Input.GetKeyUp (KeyCode.S)) {
isBottomBtnClick = false;
}