Unity 生命周期

原文翻译:
            Execution Order of Event Functions
            事件函数的执行顺序
           
            Editor编辑器
            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.
            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.)
                Awake函数在游戏物体实例化执行,即使物体被隐藏了awake还是会执行
               
                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.
                每次激活游戏物体setActive(true)才会执行一次OnEnable(),游戏物体实例化后会执行一次OnEnable,如果游戏物体开始就是avtive=false,第一次就不会执行OnEnable()
               
           
            Update Order更新列表:
                FixedUpdate: 它于游戏帧之间没有关联,定时间每调用一次,FixedUpdate是一个可靠的计时器
                Update: 每帧调用更新一次
                LateUpdate:比如要第三人称摄像机跟随人物移动,应该写在该方法内,不然会造成摄像机抖屏现象

               
            Rendering   
                OnPreCull:
                OnBecameVisible/OnBecameInvisible:
                OnWillRenderObject:
                OnPostRender: 之后调用相机完成渲染场景。
                OnRenderImage:称为场景渲染完成后允许屏幕图像的后处理。
                OnDrawGizmos: 用于绘制小场景视图的可视化的目的。
           
            Coroutines协同程序
                正常运行协同程序更新后更新函数返回。协同程序是一个函数,可以停止其执行给定YieldInstruction(收益率),直到完成。不同用途的协同程序:
                yield The coroutine will continue after all Update functions have been called on the next frame.
                yield WaitForSeconds 暂停几秒继续向下执行
                yield WaitForFixedUpdate
                yield WWW Continue after a WWW download has completed.
                yield StartCoroutine Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
               
            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).
                OnDestroy()函数,当对象呗销毁,场景跳转关闭的时候执行一次该方法
       
       
            When Quitting: These functions get called on all the active objects in your scene,
                OnApplicationQuit(),当结束游戏时候,Unity会向该场景中所有的游戏物体调用一次OnApplicationQuit()方法
                OnDisable()当游戏物体setAactive(false)的时候,每一个组件都会调用一次

Script Lifecycle Flowchart脚本生命周期流程图

 

41DEF221-DA2C-4965-AE8E-395AC0013902

 

 

总结下生命周期比较重要的方法:
  awake:游戏物体实例化执行一次
  OnEable:每次setActive(true) 执行一次
  Update:
  FixedUpdate:
  LateUpdate:一些物理运动是写在该方法中
  OnDestroy:Destriy(gameobject)当物体被销毁的执行一次
  OnDisable:每次setActive(false) 执行一次

 

posted @ 2015-02-08 17:04  盘子脸  阅读(1523)  评论(0编辑  收藏  举报