常用.md 2.1 KB

Input.GetMouseButtonDown(0); // 鼠标左键按下
Input.GetKeyDown(KeyCode.W); // 键盘输入
    
this.GetComponent<想要获取的类型>; // 获取组件

// 获取其他物体的组件 以AudioSource组件为例
// 第一种
public GameObject node;
AudioSource as = node.GetComponent<AudioSource>();
as.play();
// 第二种
public AudioSource as;
as.play();

// 物体旋转
// 第一种
this.transform.Rotate(0, 1f, 0, Space.Self);
// 第二种

Application,targetFrameRate = 60; // 帧率

// 获取物体
// 第一种(不推荐)
GameObject node = GameObject.Find("飞机机体/机翼"); // 物体路径
// 第二种
public GameObject wingNode; // 在Unity中拖拽赋值
// 通过transform获取父级
Transform parent = this.transform.parent; // 获取父级transform
GameObject parentNode = this.transform.parent.gameObject; //获取父级节点
this.transform.SetParent(); // 设置新的父级节点,填入null变为顶级节点
foreach(Transform child in transform); // 遍历子节点transform
Transform child = this.transform.GetChild(0); // 获取0号子节点
Transform child = this.transform.Find("名字"); // 按名字查找子节点,也可在找孙子("子名字/孙名字")

// 物体的显示与隐藏
if (node.gameObject.activeSelf) node.gameObject.SetActive(false);
else node.gameObject.SetActive(true);

// 定时任务
this.Invoke(函数名(string), 时间); // 只执行一次
this.InvokeRepeating(函数名(string), 首次时间, 间隔时间); // 重复执行
IsInvoking("函数名"); // 函数是否被调用
CancelInvoke("函数名"); // 取消调用,不填函数名则取消所有Invoke调用

// 3维向量
Vector3 v = new Vector(3, 0, 4);
float len = v.magnitude; // 向量长度
Vector.Distance(one, otherone); // 向量长度
v.normalized; // 标准化(长度变为1)
    
// 动态实例创建
GameObject node = Object.Instantiate(prefab, 父节点);

// 销毁节点
Object.Destroy(this.gameObject);

文件类型

AudioClip 		// 音乐类型
Material  		// 材质
Prefab    		// 预制体
Rigidbody 		// 刚体
Sphere Collider // 碰撞体