class GameObject
GameObject类包含了一个物体本身的信息,并提供了一些编辑该物体的方法。
访问当前GameObject:直接写gameObject即可。只是因为Component类中有定义gameObject这个变量,而MonoBehaviour继承自Component。
常用字段:name,tag,layer,transform等等
常用方法:SetActive,Destory,Instantitate,AddComponent等等。
可以使用CreatePrimitive函数创建unity自带的物体!
struct Vector3
Vector3代表了一个三维向量(其实就是三个数字)
它可以用来表示位置,距离,方向等等
新建一个vector3的例子:Vector3 v = new Vector3(1.2f,1.5f,-5.0f);
获得它的分量:v.x v.y v.z
内置的模板:Vector3.Up,Vector3.Left等等
常用函数:Vector3.Distance(x,y)
class Transform
Transform管理GameObject的位置,旋转与缩放。
每一个GameObject都有Transform;它是唯一一个有此性质的Component。
transform这个变量在GameObject类和Component类都有定义,它对应当前GameObject的Transform组件。
访问Transform:gameObject.transform或component.transform
但我们可以也直接写transform,这是因为MonoBehaver继承自Component。
Transform类常用字段:position,rotation,scale
Transform类常用方法:Translate,GetChild等等
class Time
Time类包含了和游戏时间相关的信息
常用变量:
Time.deltaTime 代表一帧(两次update之间)的时长(不固定)
Time.fixedDeltaTime 代表两次FixedUpdate之间的时长(固定)
</>使用Time实现每秒钟执行一行代码。
float time = 0;
timer += Time.deltaTime;
if(timer > 1)
{
print(1);
timer = 0;
}
class Input
Input类提供了接受玩家输入的方法
常用方法:
Input.GetKeyDown() 当按下一个键盘按键的瞬间为true
Input.GetMouseButtonDowm() 当按下一个鼠标按键的瞬间为true
它们还有很多变体,比如
Input.GetKey() 当一个键盘按键被按着时一直为true
Input.GetKeyUp() 当抬起一个按键的瞬间为true
…
一定要在Update而非FixedUpdate里处理input。
</>使用Input实现每次按下一个键时执行的功能。
if(Input.GetKeyDown(KeyCode.W))
{
}