The life cycle of function members in unity scripts

Source: Internet
Author: User
Tags api manual

When learning unity, mastering how to write scripts is a basic skill that must be mastered. But how does the life cycle of the functions in Unity's game scripts start and end, and how are their order of execution arranged? We should understand this clearly.

We know that the scripts in unity are mostly derived from the Monobehaviour class, so you can rewrite the methods inside the Monobehaviour class to implement the functionality we want. Then the order of function execution in the custom script and the function execution order inside the monobehaviour are roughly the same.

First introduce the member variables and the member functions inside the Monobehaviour class.

1  Public classMonobehaviour:behaviour2     {3          PublicMonobehaviour ();4 5          Public BOOLUseguilayout {Get;Set; }6 7          Public voidCancelinvoke ();8          Public voidCancelinvoke (stringmethodName);9          Public voidInvoke (stringMethodName,floatTime );Ten          Public voidInvokerepeating (stringMethodName,floatTimefloatrepeatrate); One          Public BOOLisinvoking (); A          Public BOOLIsinvoking (stringmethodName); -          Public Static voidPrintObjectmessage); -          Publiccoroutine Startcoroutine (IEnumerator routine); the [Excludefromdocs] -          PublicCoroutine Startcoroutine (stringmethodName); -          PublicCoroutine Startcoroutine (stringMethodName,Objectvalue); -          Publiccoroutine Startcoroutine_auto (IEnumerator routine); +          Public voidstopallcoroutines (); -          Public voidStopcoroutine (Coroutine routine); +          Public voidStopcoroutine (IEnumerator routine); A          Public voidStopcoroutine (stringmethodName); at}

Note: For the member variables and member functions of the Monobehaviour class, only a few relevant introduction, the specific use of the method in the future will be illustrated in succession. Or you can refer to the Unity API manual.

Variables variable

Useguilayout disable this item, the Guilayout layout stage will be skipped

Functions function

  • Invoke prototype:void Invoke (string methodname,float time); The method that specifies the method name based on the time call.
  • Invokerepeating prototype: void invokerepeating (methodName : String, time: float, repeatrate : float); A method that invokes the specified method name at a specified time, starting from the first call and once every repeatrate time.
  • Cancelinvoke prototype: void cancelinvoke (); Cancels all calls on this monobehaviour. void Cancelinvoke (string methodName); Cancels all calls that are named MethodName.
  • Isinvoking prototype: BOOL Isinvoking (String methodName), a specified function is waiting to be called. Calling returns True, otherwise false is returned.
  • Startcoroutine prototype: Coroutine startcoroutine (IEnumerator routine); Start a collaboration program.
  • Stopcoroutline Stop all co-methodname of the program.
  • Stopallcoroutline stop all actions in a cooperative program.

Overridable Functions overridable function

    • Update prototype: void update (); When Monobehaviour is enabled, the update function is called at each frame.
    • Lateupdate prototype: void Lateupdate (); When Monobehaviour is enabled, the Lateupdate function is called at each frame. Lateupdate is called after all the update function calls. This can be used to adjust the script execution order.

    • Fixedupdate prototype: void Fixedupdate (); When Monobehaviour is enabled, the Fixedupdate function is called at each frame.

    • Awake prototype: void Awake (); Awake is called when the script instance is loaded. Awake is used to initialize a variable or game state before the game starts. It is called only once throughout the lifetime of the script.

    • Start prototype: void Start (); Start is called only before the update function is called the first time. Unlike awake, the awake function is called earlier than start, and the start function is only called when the script instance is enabled.

    • Reset prototype: void Reset (), reset to default value. Reset is called when the user taps the reset button in the View panel or when the component is first added. This function is called only in edit mode. Reset is most commonly used to give a default value that is most commonly used in the View panel.

These are commonly used member functions, and some member functions are related to mouse manipulation events, such as: OnMouseOver (), Onmouseenter (), and so on.

In addition, the Ongui () function is more commonly used, and it is mainly responsible for rendering and managing the window control.

The following is an official document that charts the life cycle of the functions in the script:

Image from: http://docs.unity3d.com/Manual/ExecutionOrder.html

It is visible that the unity script has a complete life cycle from creation to destruction, and the main sequence of calls is as follows:

The Ongui function is called more than once per frame, as the corresponding GUI events are always prepared.

The life cycle of function members in unity scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.