Unity's function life cycle and static generation method

I graduated some time ago, and I ran around playing around. After being stupid, I joined the job and touched fish for more than a month~ Take it easy, the recent study plan is to start graphics in the second half of the year, and finish linear algebra in mid-September, so Articles on unity and game development are still rarely updated. Wait until you learn graphics later and then slowly update and study records. Some problems encountered in the middle of the work will still be recorded to give some help to the rookies who have just entered the workplace like me. Bar.
When I was working today, I used a method in a script to call a certain static method in another method to generate a scene or object, but I was curious about the latter’s function life cycle and the sequence of static methods and their relationship when calling the code in this way. Is there any impact? With such doubts, I want to record the answer to such a question through log printing today.
40c936d671714a38b66.png)

The two scripts are childScript and buttonscript; the first one is mounted to the cube to be generated as a static generation method, and buttonscript is mounted to the button to achieve dynamic generation and hiding effects for printing logs and observing status

public class buttonscript : MonoBehaviour
{
    
    
    public GameObject Cube;
    bool isSetActive = false;
    void Start()
    {
    
    
    }
    public void onClick()
    {
    
    
        if (isSetActive == false)
        {
    
    
            childScript.SetData(Cube);
        }
        else
        {
    
    
            Cube.SetActive(false);

        }
        isSetActive = !isSetActive;
    }
}
public class childScript : MonoBehaviour
{
    
    
    public static int meathodSum = 0; 
    private void Awake()
    {
    
    
        meathodSum++;
        Debug.Log("there  is  Awake  meathod!"+ meathodSum);
    }
    private void OnEnable()
    {
    
    
        meathodSum++;
        Debug.Log("there is  OnEnable meathod!" + meathodSum);   
    }
    // Start is called before the first frame update
    void Start()
    {
    
    
        meathodSum++;
        Debug.Log("there is Start meathod!" + meathodSum);    
    }

    public static void SetData(GameObject cube)
    {
    
    
        meathodSum++;
        Debug.Log("there is  SetData  Meathod before setactive !" + meathodSum);
        cube.SetActive(true);
        Debug.Log("there is  SetData  Meathod behind setactive!" + meathodSum);
    }
}

Before running the program, set the Cube to hide and display respectively, and use the button to call the static method to switch the state, so as to observe the function sequence of the log. When it is first hidden, the log print content is empty. insert image description here
When the button is pressed once
insert image description here
. After the button is pressed again, it is closed and opened again
insert image description here
. It is obvious from the log that any method will not run when the prefab or game object is closed. When called from the outside When the static method is generated, the first method is the SetData method, so when we write code, we always like to use setdata to pass parameters and pass in data to the chidscript script to instantiate and initialize the game object or scene. After the data is assigned and initialized, If setactive is set to true, awake will be run directly at this time, that is, when the display is generated for the first time, awake is the first, followed by oneable, and after setdata runs, it is start before the first frame, such log printing logic The order is clear. Next, continue to look at the log after closing and opening, even if we reopen or display, but because the cube has been generated for the first time, we did not destroy it but hide it, so the wake and the object before the first frame after generation start will no longer run, but oneable is indeed called every time, but there is a question here, is oneable called because setactive is true, or is it called back by default?
Then let's try it out. Add an if(meathodSum<3) cube.SetActive(true) to the setdata method in the childscript
;
this will ensure whether the oneable is related to the setactive being true when the second time is true.
insert image description here
Obviously, the oneable is in It will be called every time it is displayed, so that we can understand the operation logic of awake, oneable and start, and the static generation method setdata, and we can complete some of our strange functions according to the different operation logic sequences and characteristics of each of these four methods. Weird demand function~
Alright, let’s write this down for today, I hope it can give you some inspiration, the learning line is 5555555

Guess you like

Origin blog.csdn.net/weixin_50746193/article/details/126373223