当前位置:网站首页>In unity, inherit the lifecycle of monobehavior game objects

In unity, inherit the lifecycle of monobehavior game objects

2022-06-09 02:38:00 God of Luo Xiu

In this article Unity Object lifecycle .

We know , Inherit monobehaviour Class , All of them should execute some event functions in a certain order , such as
awake, onenable, start, update etc. , If you want to Unity Related development work , These sequences must be mastered , To put it bluntly , Interview questions are required ! ha-ha

This flow chart is a screenshot of the official website

 Insert picture description here

blog.csdnimg.cn/fbe3c826afac49f4b7379976cd3f9ccd.png) Insert picture description here
 Insert picture description here
As can be seen from the above flow chart , There are many event functions , Here we only record some commonly used code ,

1、Awake
Always in any Start This function is called before and after the prefab is instantiated. .( If the game object is inactive during startup , Will be called after activation Awake.) This function The entire cycle will only call once , And according to the introduction in brackets , Only after instantiation will .

2、OnEnable
stay Awake after , stay Start Previous call , This function may be executed many times , For example, in a script, you can use setActive(true), Then it will be transferred to the event function ,setActive(false) Will be called to OnDisable Time function , So it is equivalent to listening to the object setActive With .

3、Start
Only when the script instance is enabled , It will be called before the first frame update Start. It means that at the beginning of execution update Function before , This function must be called once , This function The entire cycle will only call once , It can be used as the constructor of a function , Initializing some game information can be done here .

4、FixedUpdate
Fixed frequency update update function , Generally used to update physical simulation , If you encounter a certain frame, it's too laggy , For example, at a certain frame GC Or complex rendering operations , It may cause this frame to get too laggy , So the next time we get here , This function will be executed many times , Ensure the correct frequency .

5、Update
Generally, some game logic is written here

6、LateUpdate

Generally, the direction and position of the camera are modified

7、PreCull
Call before the camera eliminates the scene . Culling determines which objects the camera can see . Just before calling out. OnPreCull. Here are a bunch of render related event function calls , Usually not very useful , Here is a simple record of

8、OnDisable
Pass... In the script setActive(false), Will call here

9、OnApplicationQuit
Call this function on all game objects before exiting the application . In the editor , When the user stops playing mode , Call function .

10、OnDestroy
After all frame updates have been completed for the last frame of the object's existence , Call this function

Small partner door as practice , You can create a new script ,Debug Under test , You can know the order , The code is as follows

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FunOrderTest : MonoBehaviour {
    

	GameObject Sphere;
	void Awake() {
    
		Debug.Log("Awake");
	}

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

	// Use this for initialization
	void Start () {
    
		Debug.Log("Start");
    }


	void FixedUpdate()
    {
    
		Debug.Log("FixedUpdate");
	}
	
	// Update is called once per frame
	void Update () {
    
		Debug.Log("Update");
	}
	void LateUpdate()
    {
    
		Debug.Log("LateUpdate");
	}


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

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

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

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

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

原网站

版权声明
本文为[God of Luo Xiu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090235421079.html