Use Unity OnValidate

        The OnValidate method is an editor-only function that is called when Unity loads a script or when a value in the inspector changes. Its calling timing is very special, here is a summary.

        1. OnValidate is not affected by the playback mode, as long as its value changes, it will also be called in the non-playing state (it can be used in the non-playing mode to update after modifying the parameters).

        2. Not affected by the enabled state, even if the script where it is located is disabled, it will be called normally when modifying the value.

        3. When the script enabled status is changed, OnValidate will be called once. If in Play Mode, OnValidated is called before OnDisable or OnEnable.

        4. Changing the GameObject active state will not call OnValidate, only OnDisable and OnEnable will be called.

        5. During the initial loading, regardless of the enabled state and active state, it will be called multiple times. Its call timing is before Awake.

        Common usage:

        1. Used to modify parameter values ​​in Play Mode and view the effect in real time

        2. Update asset files in real time, such as materials, shaders, etc.

Guess you like

Origin blog.csdn.net/paserity/article/details/130014259