Analysis of Mono life cycle function calling mechanism

 

1. The calling mechanism of life cycle functions is reflection, not inheritance.

    After we opened Mono, we found that it did not have the implementation of life cycle functions, nor did it in the parent class. Well, we can be sure that it is not inheritance.

Let’s verify whether it is reflection. Take the void start() function as an example, it was originally private, but if you insist on changing it to public, it will have no effect on its execution (but you definitely don’t want the outside world to manually call your Start) , Even you can set it to private IEnumerator Start(), because it is a launch call, and the name is right. As shown below

                       

(It is definitely possible to use IEnumerator for Start, but it seems that other life cycle functions can't be used in this way. Why can't I check the detailed answer? I think it should be that Unity's internal implementation mechanism stipulates that only start returns. The type can be IEnumerator, friends are welcome to leave a message for guidance on this aspect)

 

2. When Unity cannot find the corresponding function in the current Mono subclass, it will actively go to the parent class to find the corresponding function.

For example we set:

public class People : MonoBehaviour
{
    private void Start ()
    {
        Debug.Log( " Execute People's Start function " );
    }
}

public class Teacher : People
{
    void Start ()
    {
        Debug.Log( " Executed Teacher's Start function " );
    }
}

 

a. This is the most basic situation. Everyone must know that this is to execute the Start function of Teacher, while the Start function of people is not executed;

b. But if I remove the Teacher's Start function, as follows

public class Teacher : People
{
}

      At this point, if you run it again, you will find that it will execute the Start function of the parent class People. Note here that the Teacher's Start function must be removed directly, not as follows 

private void Start ()
{
    // Debug.Log("Execute People's Start function"); 
}

       Because this is also considered to have a Start function, Unity will find it in Teacher through reflection, so it will not go to People to find it. actually. In fact, this is very important. When many subclasses want to cover the life cycle function of the parent class, they often write an empty one to cover it. If you think this empty function is useless and delete it directly, you are wrong.

 

3. When the parent class Start function is called, what is the mechanism for calling the member function inside Start, and see the following:

public class People : MonoBehaviour
{
    private  void Start ()
    {
        Debug.Log( " Execute People's Start function + " + this .GetType().ToString());
        Debug.Log( " Execute People's Start function + " + this .Sign());
        Debug.Log( " Execute People's Start function + " + this .Mark());
    }
    public virtual string Sign()
    {
        return "This is belong to People";
    }
    public virtual string Mark()
    {
        return "People Mark";
    }
}

public class Teacher : People
{
    // void Start ()
     //    {
     //        Debug.Log("Teacher's Start function was executed");
     // } 
    public  override  string Sign()
    {
        return "This is belong to Teacher";
    }
    public new string Mark()
    {
        return "Teacher Mark";
    }
}

Final execution result:

 

     Therefore, when the Start function of the parent class is executed, the mechanism adopts polymorphism, and the implementation of the parent class and the child class is declared (the shell of the parent class, the filling of the child class), so we build the parent class framework in the project. If you want to rewrite which function in the subclass, you can directly override it, which is very convenient.

 

       At the same time, there is another point, you will find that in the above code, the Start function of people does not affect the result whether it is declared as public or private, that is to say, Unity's principle for this place is: let the private life cycle function in the parent class be It can also be accessed by subclasses (implemented by reflection simulation), and others still conform to the concepts of polymorphism and inheritance.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325368140&siteId=291194637