This page is an effort to thoroughly document the different life cycles in Unity... contribution needed!
I'm using it for myself right now because I keep making the same tests to figure out the exact life-cycles, and figured this would be a good place to finally centralize the info.
Contents[hide]
|
This is called by .net as your assemblies are being loaded. The C# Reference makes very few guarantees about exactly when it will be called, but it does state that before any part of a class can be referenced its static constructor will be called.
If working from the editor, when you save a script (and thus force a recompilation), your static constructor is likely to be called immediately as Unity loads the built DLL. It will not likely be run again.
On a deployed game, this constructor will be called early in Unity's loading process.
Unity will call the default constructor of any object but at seemingly random times. While editing a game, the constructor is likely to be called immediately after saving a script (forcing a recompilation).
Do not use a constructor to assign values to fields. Unity's Awake() is designed specifically for that purpose.
This is the one and only event that will ALWAYS fire (unless the object is inactive, see above). It will still fire if the object is active but the script is disabled.
Not called if the behaviour is disabled.
Not called on the first level.
Not called if the object is created once the level is loaded.
Not called if the behaviour is disabled.
Undocumented.
In a Javascript behaviour, this is where your inline instructions go after compilation, but it's actually usable in C# as well.
Only think of it as a pre-Start, not another round of events.
All Awakes are called, then all Main & Start are called, NOT all Awake, all Main, then all Start.
Consider components Script1 & Script2, Script1 being first in the component list, the call order will be:
Not called if the behaviour is disabled.
Not called if the behaviour is disabled.
Not called if the behaviour is disabled.
Editor-only. Called even if the behaviour is disabled.
Editor-only, called only if the object is selected in the hierarchy view. Called even if the behaviour is disabled.
Only called if the level was network-loaded.
This event isn't actually built-in, but fired by the NetworkLevelLoad script that lots of people are using.
The +1 delay is also introduced by this script, but I'm not sure it would be wise to try and remove it.
Don't take my word for it, try it for yourself!
using UnityEngine; using System.Collections; public class TestLifeCycle : MonoBehaviour { void Awake() { logFrame("Awake"); } void Main() { logFrame("Main"); } void Start() { logFrame("Start"); } void OnLevelWasLoaded() { logFrame("OnLevelWasLoaded"); } void OnNetworkLoadedLevel() { logFrame("OnNetworkLoadedLevel"); } void OnEnable() { logFrame("OnEnable"); } void OnDisable() { logFrame("OnDisable"); } void OnApplicationQuit() { logFrame("OnApplicationQuit"); } void Update() { logFrame("Update"); } void LateUpdate() { logFrame("LateUpdate"); } void FixedUpdate() { logFrame("FixedUpdate"); } void LateFixedUpdate() { logFrame("LateFixedUpdate"); } void OnDrawGizmos() { logFrame("OnDrawGizmos"); } void OnDrawGizmosSelected() { logFrame("OnDrawGizmosSelected"); } static void logFrame(string message) { Debug.Log(Time.frameCount + " - " + message); } }
TODO...
explain FixedUpdate here, relation with OnCollisionXXX
TODO...
explain EventType
TODO...
explain MasterServer connection, peer connection / disconnection, etc...
If a method call Network.Disconnect(), OnDisconnectedFromServer will be called instantly (whether in the same script or any other script that subscribes to this callback).
Example:
void Disconnect() { logFrame("before Disconnect"); Network.Disconnect(); logFrame("after Disconnect"); } void OnDisconnectedFromServer() { logFrame("OnDisconnectedFromServer"); }
will lead to the following output:
before Disconnect OnDisconnectedFromServer after Disconnect
This behaviour is opposed to delayed callbacks, that occur one frame later than the event that triggered them (for example OnTriggerEnter is called the frame after the collision visually occurs). Also note that Debug.Break() will not interrupt the current call stack, merely pause the editor at the end of the current frame. TODO: reference instant/delayed callbacks.
原文:http://wiki.unity3d.com/index.php?title=Life_cycle
In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:
These functions get called when a scene starts (once for each object in the scene).
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.
For objects added to the scene, the Start function will be called on all scripts before Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.
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.
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.
Update: Update is called once per frame. It is the main workhorse function for frame updates.
LateUpdate: LateUpdate is called once per frame, after Update has finished. Any calculations that are performed in Update will have completed when LateUpdate begins. 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.
Normal coroutine updates are run after the Update function returns. A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes. Different uses of Coroutines:
These functions get called on all the active objects in your scene:
The following diagram summarises the ordering and repetition of event functions during a script’s lifetime.