The life cycle of unity scripts
Preface: 10 important event functions are defined in unity, followed by the following in order of execution:
(1): Reset: Reset function, edit period when a script is assigned to a game object, it is executed only once.
(2): Awake: Wake-up function, the first event function executed for the highest priority event processing, executed only once.
(3): onenable: Enable the function, when the script starts to trigger, as the script continues to be enabled and disabled can be executed multiple times.
(4): Start: function, usually used to assign the initial value to the Script field, only once.
(5): fixedupdate: Fixed update function, the default 0.02s clock frequency execution, often used in object simulation to deal with rigid body movement, etc., executed multiple times per second.
(6): Update: Updates the function, the frequency of execution is not fixed, and the computer's current performance consumption is inversely proportional, often used in logical calculations, executed multiple times per second.
(7): Lateupdate: Post-update function, executed after the remaining two update functions, often used in the camera's control case, executed multiple times per second.
(8): Ongui: Graph drawing function: Draws the system UI interface, executes multiple times per second.
(9): ondisable: Script Disable function, when the script is disabled when triggered, as the script is continuously enabled and disabled, can be executed multiple times.
(Ten): OnDestroy: Destroy function, the script belongs to the game object destroyed when the execution of this script. Executes only once.
1. Write down we write the script to test the life cycle of each function:
Public classdemo10_scriptlifecycle:monobehaviour{voidReset () {print ("Reset function Reset"); } voidAwake () {print ("Wake-up event function awake"); } voidonenable () {print ("script Enable event function onenable"); } voidStart () {print ("start the function start"); //Turn on Call functionInvokerepeating ("invoketest", 0f, 1f); //start co-formationStartcoroutine ("coroutinetest"); } voidfixedupdate () {print ("Fixed update function fixedupdate"); } voidUpdate () {print ("Updating function Update"); //stop calling a function if(Input.getkey (keycode.i)) {Cancelinvoke ("invoketest"); } //Stop co-forming Else if(Input.getkey (keycode.c)) {Stopcoroutine ("coroutinetest"); } } voidlateupdate () {print ("deferred update function Lateupdate"); } voidOngui () {print ("interface drawing function Ongui"); } voidondisable () {print ("script Disable function ondisable"); } voidOnDestroy () {print ("Destroy function Ondestory"); } voidinvoketest () {print ("method of the called function to be tested"); } IEnumerator Coroutinetest () { while(true) { yield return NewWaitforseconds (1f); Print ("co-formation tested"); } }}//Class_end
When we assign a script to an empty object in the hierarchy view, notice that a message appears in our console view:
The result of the above output is that the reset () function in the unity life cycle is triggered during editing (when we assign a value to our script to an empty object). OK, next we'll execute our program:
The results of the run we can see the sequence of execution of some of the event functions in the script and the number of executions. But we found no output of the ondisable and OnDestroy event functions. Let's start disabling this script:
When we disable the script we will find that the "method of the called function being tested" and "tested covariance" will still be executed:
the onenable () event function is executed when we enable the script:
We use a structure diagram to indicate the order in which each function executes:
The life cycle of unity scripts