找寻Component或者GameObject总结:
- 找Component:
gameObject.GetComponent<RigidBody>()
- 找子节点中的GameObject:
transform.Find("Gun")
- 找任意的GameObject:
GameObject.Find("Gun"),GameObject.FindWithTag("Player"), GameObject.FindGameObjectsWithTag("Enemy")
- 找Component:
MonoBehaviour中的event function:
Update(), Awake(), OnGUI(), OnCollisionEnter(), OnTriggerEnter()
等。Time.deltaTime:过去一帧实际经过的时间,若在FixedUpdate()中调用则等同于Time.fixedDeltaTime。
Time.fixedDeltaTime: 固定的delta时间,用于物理引擎。
Time.timeScale: 设置这个值会加快游戏的整体速度,设为0时暂停。
Instantiate():复制一个GameObject。
Destroy(): 销毁GameObject。
coroutine: 可以分帧执行的函数。作用是将耗时操作分帧执行。
WaitForSeconds():不立即从下一帧继续执行,而是有一个延迟。
event function的执行顺序(详见下图):
- 初始化: Awake -> OnEnable -> Start
- 每一帧:FixedUpdate -> OnTriggerXXX -> OnMouseXXX -> Update -> yield null -> LateUpdate -> OnRenderObject -> OnGUI
- 结束: OnDisable -> OnDestroy
event system: 基于input(键盘、鼠标、触摸等)来发送event到各种对象上,需要在scene中间添加EventSystem组件。
Raycaster:用于引导event发送到哪个对象上。
如何实现Button点击事件:
- 直接在Inspector下面OnClick处添加回调函数。
- 通过代码:
button.onClick.AddListener(functionName)
。
《Unity Manual》阅读笔记(二):Scripting
最新推荐文章于 2023-02-28 19:04:28 发布