1. 激活游戏对象示例
gameObject.SetActive(false);
Debug.Log("Active Self: " + myObject.activeSelf);
Debug.Log("Active in Hierarchy" + myObject.activeInHierarchy);
销毁游戏对象示例
Destroy(gameObject);
public GameObject other;
Destroy(other);
Destroy(GetComponent<MeshRenderer>());
2. 按键检测,尽量用getButton
bool down = Input.GetButtonDown("Jump");
bool held = Input.GetButton("Jump");
bool up = Input.GetButtonUp("Jump");
3.GetAxis
返回-1到1的f值
gravity 影响多快回到0
sensitive 影响多快到1
dead 表示摇杆的盲区
snap 表示同时按正负按钮时归零
Input.GetAxis("Raw")表示返回整数,不返回非整数
4. OnMouseDown
OnMouseDown is called when the user has pressed the mouse button while over the Collider.
点击位置需要有碰撞盒
5.Instantiate 克隆游戏对象示例
public Rigidbody rocketPrefab;
public Transform barrelEnd;
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody; //强制转换为rigidbody
记得destroy
5. 数组示例
public GameObject[] players;
players = GameObject.FindGameObjectsWithTag("Player"); 获得所有tag为Player的游戏对象
6. Invoke 将函数调用安排在指定延迟之后
Invoke ("SpawnObject", 2); 延迟单位为s ,只有无参数且返回类型为void的函数可以被Invoke调用
CancelInvoke("SpawnObject");停止调用,可以通过指定函数名来指定停止的Invoke
7.枚举示例
enum Direction{North, East, South, West} //默认每个枚举值按从小到大顺序关联一个大于等于0的整数,可以直接赋值决定,例如North = 1 ,后面依次定增
更改整数类型: enum Direction : short {North, East, South, West}
Direction myDirection;
myDirection = Direction.North;
8. Switch示例
小技巧:在vs中输入sw然后快速点击两次tab可以生成switch模板
switch(intelligence)
{
case 5 :
print("string1");
break;
default :
print("string2")
break;
}
9. LateUpdate用于在Update之后进行更新
虽然是在同一帧中执行的,但是Update会先执行,LateUpdate会晚执行。
10.Vector3自带属性
Vector3.forward 为 (0,0,1)
Vector3.up 为 (0,1,0)