# 入门

# 安装

# unity 关联 vs studio

点击编辑 -> 首选项 -> 外部工具 -> 外部脚本编辑器
选择 visual Studio

# 组件特性

# 刚体 Rigdbody

注意区分带 2D

即添加物理特性如重力

# 材质

可以放入物理材料

# 质量 Gravity Scale

影响物体的重力

# 线行阻尼 Collision Detection

相当于空气阻力

# 冻结轴体

img_2.png
无法移动跳跃或者旋转

# 碰撞体 collider

注意区分带 2D
如给两个物体添加碰撞体,双方有了碰撞的实体,如人物模型站在平台模型上

# 资产 Assets

# c# 脚本

可以拖动到物体的组件中

# 物理材料 Physics Material

可以拖动导刚体的 “材质” 中

可添加摩擦力和弹力

# 精灵图

和 css 的一样需要切片
单机精灵图,选择 sprite 模式,选择多个,点击 sprite editor 就可以开始切片了
Sword+Aim+Throw.png

一般按照单元格以长宽一定的比例平均分成一张精灵
然后就可以把第一个精灵拖入场景

更改精灵大小
点击精灵图 检查器中的每单元像素数就是修改大小
img_1.png

# 动画

有了精灵图我们就可以制作动画首先点开动画和动画器
img_3.png

# c#

新建后为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}

Start () 第一帧更新之前调用
一般用于初始化

Update () 每一帧调用一个
一般用于时长更新的操作,如移动跳跃等

# 变量

作用域
public 可以使变量在 unity 中显示
private 只在脚本中

例如玩家有一个血条,就应该设置为 public,让他可以从其他源修改,尽量不要公开,除非我们真的需要

如果想保持私有,且 unity 能访问可以 serialize 序列化

1
2
[SerializeField] private Rigidbody2D rb;
[SerializeField] private float moveSpeed = 5;

如果不使用 serialize,想获得刚体等类型可以通过构造

1
2
3
4
5
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}

# unity 组件

刚体

例子:给物体开始添加一个 x 轴为 5 的力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Player : MonoBehaviour
{
// 2D刚体
public Rigidbody2D rb;
private int xInput;
void Start()
{
// 给一个x轴+5的向量,y轴不变
rb.velocity = new Vector2(5, rb.velocity.y);
}
void Update()
{

}
}

按住物体的刚体拖动到脚本的刚体中
img.png

点击运行

# 函数

Input 输入
Input.GetKey () 按住某个键
Input.GetKeyDown () 按下某个键,键到最下
Input.GetKeyUp () 释放某个键

例如

1
2
3
4
5
void Update(){
if(Input.GetKey(KeyCode.Space)){
//按住空格
}
}

# 例子

首先创建项目
关联代码编辑器

阅读次数

请我喝[茶]~( ̄▽ ̄)~*

douk 微信支付

微信支付

douk 支付宝

支付宝