Unity常用API - zLulus/My_Note GitHub Wiki
链接
主要方法
//查找自己的、父母的、后代的组件
public T GetComponent<T>();
public T GetComponentInChildren<T>([Internal.DefaultValue("false")] bool includeInactive);
public T[] GetComponentsInChildren<T>(bool includeInactive);
public T GetComponentInParent<T>();
public T[] GetComponentsInParent<T>(bool includeInactive);
//功能等于==,性能更佳
public bool CompareTag(string tag);
//对应的游戏物体
this.gameObject
链接
存储并操控对象的位置、旋转、缩放比例
场景中的每一个对象都有Transform
Transform可以有一个父级,允许分层次应用位置、旋转和缩放
提供查找(父、根、子-索引、名称)变换组件,改变位置、角度、大小的功能
//物体相对于世界坐标系原点的位置/旋转
this.transform.position
this.transform.rotation
//物体相对于父物体轴心点的位置/旋转
this.transform.localPosition
this.transform.localRotation
//Inspector中Transform的Positon/Rotation/Scale都是Local
//物体相对于父物体缩放比例
this.transform.localScale
//物体与模型缩放比例=自身的缩放比例*父物体缩放比例(只读)
this.transform.lossyScale
//获得父物体变换组件
transform.parent
//获得根物体变换组件(最顶级)
transform.root
//设置父物体
//parent可以在前端绑定/后端创建
public void SetParent(Transform p);
public void SetParent(Transform parent, bool worldPositionStays);
//移动
public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);
//根据自身坐标系移动
transform.Translate(0, 0, 1);
//根据世界坐标系移动
transform.Translate(0, 0, 1, Space.World);
//根据其他Transform为坐标系移动
public void Translate(Vector3 translation, Transform relativeTo);
//旋转(类似移动)
public void Rotate(Vector3 eulers, [DefaultValue("Space.Self")] Space relativeTo);
//根据名称查找子物体变换组件
public Transform Find(string n);
transform.Find("子物体名称");
//不推荐使用
transform.Find("子物体名称/子物体的子物体名称/.....(写路径)");
//根据索引查找子物体
public Transform GetChild(int index);
public int GetChildCount();
//循环子物体
foreach(Transform child in transform)
{
child.position += Vector3.up * 10.0f;
}
//在层级未知的情况下查找子物体:递归
//https://www.bilibili.com/video/av28779788/?p=127
//获得该物体的同级索引
public int GetSiblingIndex();
//修改该物体在同级的索引
public void SetAsFirstSibling();
public void SetAsLastSibling();
public void SetSiblingIndex(int index);
//解除当前对象的所有子对象与之的关系
public void DetachChildren();
//解除父级与之的关系
transform.SetParent(null);
//Rotates the transform so the forward vector points at target's current position.
public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);
链接
Unity场景中所有实体的基类(Hierarchy面板所有实体)
提供禁用、启用、添加、查询游戏物体的功能
激活状态:Unity Inspector中打勾/不打勾
//物体实际的激活状态(在场景中物体的激活状态)
gameObject.activeInHierarchy
//物体local的/自身的激活状态(只读),也是该物体在Inspector面板打勾的直接状态
gameObject.activeSelf
eg.物体Cube1是物体Cube2的父级,Cube1是未激活状态,Cube2是激活状态
则Cube2的activeInHierarchy=false,activeSelf=true
同样适用于物体的各组件
//设置物体的激活状态
public void SetActive(bool value);
//是否静态
gameObject.isStatic
//所在图层
gameObject.layer
//当前物体所在场景
gameObject.scene
//tag
gameObject.tag
//变换组件
gameObject.transform
//添加组件
public T AddComponent<T>() where T : Component;
//不能直接new组件!!!
//创建物体
GameObject lightGo = new GameObject();
//添加并获得组件
var light= lightGo.AddComponent<Light>();
//编辑组件
light.color = Color.blue;
light.type = LightType.Point;
小结:物体找组件GetComponent/AddComponent,组件找物体this.gameObject
//在场景中根据名称查找物体(不建议使用->效率低)
public static GameObject Find(string name);
var go= GameObject.Find("游戏对象名称");
//根据标签查询所有/一个使用这个tag的物体
//适用于脚本所在物体与目标操作物体没有直接关系(不是父子关系),性能比Find高
public static GameObject[] FindGameObjectsWithTag(string tag);
public static GameObject FindGameObjectWithTag(string tag);
//资源、游戏对象、组件等的名称
public string name { get; set; }
//销毁对象、组件、资源
public static void Destroy(Object obj);
Object.Destroy(obj);
//t秒后销毁(延迟销毁)
public static void Destroy(Object obj, [DefaultValue("0.0F")] float t);
//加载新场景时,使目标物体不被自动销毁
//Do not destroy the target Object when loading a new Scene.
public static void DontDestroyOnLoad(Object target);
//在场景中,根据Type找第一个/所有激活的加载对象
//Returns the first active loaded object of Type type.
public static T FindObjectOfType<T>() where T : Object;
//Returns a list of all active loaded objects of Type type.
public static T[] FindObjectsOfType<T>() where T : Object;
//克隆(多重重载)->刷怪使用
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static T Instantiate<T>(T original, Transform parent, bool worldPositionStays) where T : Object;
链接
获取时间信息的接口
//以秒计算,从游戏开始所消耗的时间(只读)-受时间缩放影响
Time.time
//游戏时间(只读)-不受时间缩放影响
Time.unscaledTime
//实际游戏运行时间(只读)-不受时间缩放影响
Time.realtimeSinceStartup
Time.unscaledTime、Time.realtimeSinceStartup基本是一样的
//以秒计算,完成最后一帧的时间(只读)=帧与帧之间的消耗时间
Time.deltaTime
eg.在Update()中让速度恒定(射击类游戏、赛车类游戏)
原来的代码
private void Update()
{
this.transform.Rotate(0, 1, 0);
}
上面代码的现象是,好的电脑旋转速度快,差的电脑旋转速度慢
电脑性能好,帧多,渲染帧多->1秒旋转速度快->希望1帧旋转量小(Time.deltaTime小)
电脑性能差,帧少,渲染帧少->1秒旋转速度慢->希望1帧旋转量大(Time.deltaTime大)
this.transform.Rotate(0, 1*Time.deltaTime, 0);
类似于高个和矮个子走路,高个步伐小点,矮个子步伐大点
小结:
移动/旋转速度*每帧消耗时间,可以保证移动/旋转速度不受机器性能(渲染)影响
在Update()中,每帧持续操作物体,必须*Time.deltaTime
在FixedUpdate()中操作物体,不加*Time.deltaTime(时间固定)
//时间缩放
//默认Time.timeScale=1
//慢镜头Time.timeScale<1
//Time.timeScale=0,游戏暂停(FixedUpdate也不再执行)
Time.timeScale
小结
FixedUpdate()受Time.timeScale的影响
Update()只和渲染有关,不受Time.timeScale的影响
->只是在Update()使用Time.deltaTime后,设置Time.timeScale=0,所以Time.deltaTime=0,所以看起来像是对Update()造成了影响
游戏暂停,部分物体不受影响->不能在FixedUpdate()中执行->在Update()中执行,并且不Time.deltaTime,使用Time.unscaledDeltaTime
//游戏暂停 物体不受影响
//不受时间缩放影响的每帧间隔
Time.unscaledDeltaTime
//Lerp(起点,终点,比例)
//线性插值:变化从大到小
public static float Lerp(float a, float b, float t);