Unity脚本 组件生命周期与事件

 

我们把脚本组件分为多个阶段

Editor

这个阶段能触发的只有一个Reset函数

  • Reset: Reset is called to initialize the script’s properties when it is first attached to the object and also when the Reset command is used.

当脚本被挂载到Object 对象上,并且在使用Reset命令的时候触发

 

First Scene Load

These functions get called when a scene starts (once for each object in the scene).

  • Awake: This function is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active, or a function in any script attached to it is called.)
  • OnEnable: (only called if the Object is active): This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.

Note that for objects added to the scene, the Awake and OnEnable functions for all scripts will be called before Start, Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.

Awake 函数这个只会在组件生成的时候调用或者预设体被初始化的时候调用,每个组件的实例只会调用一次。当脚本组件挂载到一个GameObject上的时候,不管他是否是激活的他本身都是会调用Awake函数。但是如果他挂载的GameObject对象是未激活那么他本身也是不会被调用。

OnEnable 函数。这个函数式当脚本组件被激活时候调用的,如果脚本一个开始是激活挂载在GameObject上,他也是会调用的。如果在运行时,从未激活变成激活,还是重新调用OnEnable 函数

 

Before the first frame update

  • Start: Start is called before the first frame update only if the script instance is enabled. 

当物体加入场中,Start函数会在Update函数前调用。

In between frames

  • OnApplicationPause: This is called at the end of the frame where the pause is detected, effectively between the normal frame updates. One extra frame will be issued after OnApplicationPause is called to allow the game to show graphics that indicate the paused state.、

Update Order

   When you’re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update function, but there are also other functions you can use.

  在游戏中有很多消息更新时机,在Unity中 分为三块。

   FixedUpdate: FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate. When applying movement calculations inside FixedUpdate, you do not need to multiply your values by Time.deltaTime. This is because FixedUpdate is called on a reliable timer, independent of the frame rate.

  FiexdUpdate 可以认为是物理帧更新,它比一般的游戏帧 Update可能调用方更频繁点,如果帧很低话,在一次游戏帧中会调用多次的FiexdUpdate。根据生命周期图可以知道,所有的物理计算和更新都是在FiexdUpdate。所以一般物理相关的计算我们是放在FiexdUpdate中的。

      you do not need to multiply your values by Time.deltaTime.  在FiexdUpdate中 我们不用Time.deltaTime这个参数,因为FiexdUpdate的DeltaTime都是固定的,他根据物理帧率的定时器触发的,跟实际帧率无关。

 

  Update:Update is called once per frame. It is the main workhorse function for frame updates.

 

  LateUpdateLateUpdate is called once per frame, after Update has finished. Any calculations that are performed in Update will have completed when LateUpdatebegins. A common use for LateUpdate would be a following third-person camera. If you make your character move and turn inside Update, you can perform all camera movement and rotation calculations in LateUpdate. This will ensure that the character has moved completely before the camera tracks its position.

  LateUpdate 也是每帧调用一次的,在所以游戏更新完之后调用。文档里面说常用地方就是摄像机跟随,我们可以在LateUpdate中重新计算摄像机位置,因为目标的物理位置在LateUpdate之前已经计算完成。

 

 

Rendering

    游戏逻辑计算完就开始渲染,渲染里面分为 裁剪时机 渲染时机 后期特效 GUI渲染 Editor辅助线

裁剪时机:

 

  • OnPreCull: Called before the camera culls the scene. Culling determines which objects are visible to the camera. OnPreCull is called just before culling takes place.
  • OnBecameVisible/OnBecameInvisible: Called when an object becomes visible/invisible to any camera.

      OnPreCull 是在裁剪计算之前调用的,这个消息只有在Camera组件才有效果。如果在渲染中要动态改变摄像机的属性,在这个事件中可以修改。

    OnBecameVisible/OnBecameInvisible: 这两个比较好理解,就是一个渲染的组件,在摄像机裁剪计算完,会触发可见性改变触发。

   

  • OnWillRenderObject :Called once for each camera if the object is visible. 这个函数在每次摄像机遍历,如果物件可见的话就是调用。一个物件从可见到不可见,调用顺序应该是
  • OnPreRender: Called before the camera starts rendering the scene.
  • OnPostRender: Called after a camera finishes rendering the scene.
  • OnRenderObject: Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.

  Camera:OnPreCull

     ↓

  Object:OnBecameVisible(如果正常在视锥体内话,这步是没有的)

               ↓

  Object:OnWillRenderObject 

     ↓

  Camera:OnPreRender

     ↓

  Camera:OnPostRender

    ↓

  Object:OnRenderObject

 

OnPreRender 和OnPostRender 是摄像机遍历场景渲染的开始和介绍。这里我们设置在这个摄像机里面所有物件渲染状态,在Post还原,还有一点 OnRenderObject和OnPostRender 渲染时机非常相近,都是在场景渲染完毕调用。

OnRender不是说在场景渲染时候调用的,是在场景渲染完毕,我们可以附加渲染自己想要的代码。

 

OnRenderImage:Called after scene rendering is complete to allow postprocessing of the image, see ImageEffects. OnRenderImage 场景里面所有的摄像机渲染完毕,我们做后期特效,场景所有东西渲染到一个RT上面,

我们这里可以使用ImageEffects.做画面做些特效处理。

 

OnGUI: Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.

UI响应时间函数

 

When the Object is Destroyed

  • OnDestroy: This function is called after all frame updates for the last frame of the object’s existence (the object might be destroyed in response to Object.Destroy or at the closure of a scene).

When Quitting

These functions get called on all the active objects in your scene:

  • OnApplicationQuit: This function is called on all game objects before the application is quit. In the editor it is called when the user stops playmode. In the web player it is called when the web view is closed.
  • OnDisable: This function is called when the behaviour becomes disabled or inactive.

 

 后面三个都比较好理解,

OnDestroy  在当调用Destroy 释放对象调用的,

OnApplicationQuit 是在游戏退出时候调用

OnDisable 是在对象inative时候触发的,这个跟OnEnable是对应的

 

Physics

  OnCollisionEnter :OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

  OnCollisionExite: OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.

In contrast to OnTriggerExit, OnCollisionExit is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

   OnCollisionStay: is called once per frame for every collider/rigidbody that is touching rigidbody/collider.

In contrast to OnTriggerStay, OnCollisionStay is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

Collision stay events are not sent for sleeping Rigidbodies.

  基本三个碰撞测试消息事件,当一个刚体或者碰撞体 碰撞另一个刚体或者碰撞盒的时候触发, 参数是Collision 不是Collider,具体内容看Collision的定义,如果一方碰撞也是 non-kinematic rigidbody attached 那么碰撞会传递下去。还有一点. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. 脚本即使不是被激活,这个消息会发送过去。

物理碰撞还有情况的使用就是当事件触发器,在Collider Trigger属性勾选,那么Collider本身会触发这三个事件,其他Collider 不会触发OnCollisionEnter事件

    OnTriggerEnter: OnTriggerEnter is called when the Collider other enters the trigger.

    OnTriggerExit:OnTriggerExit is called when the Collider other has stopped touching the trigger.

    OnTriggerStay:OnTriggerStay is called once per frame for every Collider other that is touching the trigger.

 

This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached. Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

posted @ 2016-05-18 10:54  BaoqingWu  阅读(1529)  评论(0编辑  收藏  举报