The use of life cycle functions (event functions) in unity

Life cycle function (event function)

Insert image description here

The order of assigning values ​​to variables (the left side is assigned first, the right side is assigned last, and so on):

Variable declaration and direct assignment->Inspection panel assignment->Awake>OnEnable->Start->External assignment

We generally assign a value to an object, or want to change the value of an object's attributes or member variables from the outside. The premise is that the object already exists (meaning that the start method has been called), so the external assignment will occur after Start. method to call

1.Reset event

Call situation: This function can only be called in editor mode (not allowed)
Call time: After the script is mounted to the object for the first time or the Reset() command is used.
Number of calls: Will call Reset once and initialize all values ​​
Function: Provided Default value, reset value.

public class No2_EventFunction : MonoBehaviour
{
    
    
    public float value=10;
  private void Reset()
    {
    
    
        Debug.Log("调用了Reset");
    }
}

2.Awake event

Call situation:
a. When initializing the GameObject containing the activation status of the script when loading the scene
b. GameObject changes from inactive to activated Status
c. After initializing the GameObject created using Instantiate
Call time: During the lifetime of the script instance, Unity Call Awake only once. A script's lifetime lasts until the scene containing it is unloaded.
Number of calls: The order in which Unity calls Awake of each GameObject is uncertain, and human intervention (i.e. design) is required to ensure the correctness and stability of the program.
Function: Awake instead of the constructor for initialization. In Unity, the constructor is not used for component initialization
In the game Function: can load the resources required by the UI

private void Awake()
    {
    
    
        Debug.Log("调用了Awake方法"); 
    }

3.OnEnable event

Call situation:
a. The game object is activated
b. The script component is activated
Calling time, Number of times: It will be called once every time the game object or script is activated.
Function: Repeated assignment to the initial state a> Create an object pool. If the monster is destroyed, in some cases (such as resurrection), activating this game object again can initialize the monster's health
Function in the game:

public class No2_EventFunction : MonoBehaviour
{
    
    
    public float currentHP=10;
    
    private void OnEnable()
    {
    
    
        Debug.Log("调用了OnEnable方法");
        currentHP = 100;
        Debug.Log("当前血量为:"+currentHP);
    }
}

4.Start event

Call situation:
a. The game object is activated
b. The script component is activated
Calling time, Number of times: Called before Update of the first frame when the script instance is activated
Effect: Executed after Awake, Convenient to control the order of calling logic before and after

public class No2_EventFunction : MonoBehaviour
{
    
    
    public float attackValue=10000;
    void Start()
    {
    
    
        Debug.Log("调用了Start方法")
        attackValue = 3;
    }
}

5.Update event

Call situation:
a. The game object is activated
b. The script component is activated
Calling time, Number of times: Called every frame, it is the most commonly used function, called about 60 times per second (according to the performance and status of the current computer)
Function: Update data in real time and accept input data

 void Update()
    {
    
    
        Debug.Log("调用了Update方法");
    }

6.LateUpdate event

Call situation:
a. The game object is activated
b. The script component is activated
Calling time, Number of times: LateUpdate is called after calling all Update functions, about 60 times per second.
Function: Arrange the execution sequence of the script, such as camera following. The character must move first before the camera will follow

        private void LateUpdate()
    {
    
    
        Debug.Log("调用了LateUpdate方法");
    }

7.OnDisable event

Call situation:
a. Game object is disabled
b. Script component is disabled
c. Game The object is destroyed
Call time and number: Call once immediately when the calling situation is met
Function: Used for state reset, resource recycling and cleanup of some objects

    private void OnDisable()
    {
    
    
        Debug.Log("调用了OnDisable方法");
    }

8.OnApplicatoinQuit Incident

Call situation:
a. All game objects will call this function before the program exits
b. In the editor, it will be called when the user terminates Called when playing mode
c. Called when the web page view is closed
Calling time and number: Called immediately when the calling situation is met Once
Function: Used to handle some logic after exiting the game
Note!!!: It needs to be tested after the project is packaged

    void OnApplicatoinQuit()
    {
    
    
        Debug.Log("OnApplicationQuit");
    }

9.OnDestroy event

Call situation:
a. The scene or game ends
b. Stopping the play mode will terminate the application
c. Called when the web view is closed
d. The current script is removed
e. The game object mounted to the current script is deleted< a i=6>Call time and number: Call once immediately when the calling situation is metFunction: Used for some game objects Destruction

    void OnDestroy()
    {
    
    
        Debug.Log("OnDestroy");
    }

Guess you like

Origin blog.csdn.net/qq_43007056/article/details/130758445