AnyPortrait > Manual > Main Logic Event

Main Logic Event


1.4.8

The Unity engine processes updates and rendering in a specific order.
Events in the MonoBehaviour script represent each step.
AnyPortrait is not a built-in feature of Unity, so it does not update exactly in the “order in which animations are processed”, but is developed to update at as close a point as possible.
(Note: Event execution order in Unity engine)


However, depending on the script written by the user or the characteristics of other assets, it may be necessary to change the update order of AnyPortrait.
You can change the timing at which animation and mesh are updated using the Main Logic Event option added in AnyPortrait v1.4.8.




Setting up an event where the Main Logic is called




(1) Open the Bake dialog.
(2) Select the Setting tab.
(3) You can set the event that updates the main logic by changing the value of Main Logic Event.


The values of each option are as follows:
- LateUpdate : The main logic is executed in the MonoBehaviour's "LateUpdate" event. This is the default.
- Update : The main logic is executed in the MonoBehaviour's "Update" event.




This option can also be changed in Inspector.
(1) Select the AnyPortrait character in the Unity scene.
(2) Select the Basic Settings tab.
(3) You can set Main Logic Event options.




About when to Call the Main Logic


When the main logic is called may or may not be a sensitive issue.
If each module in your game operates with loose relationships, this option may not be very important.
However, if the order in which each module is processed during one frame of the game is important, the order in which scripts are called becomes very important.
For example, the update function for a camera that follows a character would need to be called after the character logic has been processed.
The same goes for animation.
If the update order is not organized, animation playback requests, etc. may not be processed properly.
Therefore, the processing order of general scripts and animations must be clearly established with consistent rules.




This diagram summarizes the sequence of event calls in Unity related to animation.
After Awake and Start, which correspond to initialization, the Update event is called.
And then Animation is processed.
Finally, when LateUpdate is called and then Rendering is performed, one cycle is completed.
Because the script update and animation processing stages are separated, it can operate without error.




Since AnyPortrait is not a built-in feature of Unity, its processing method cannot be used as is.
However, it was developed so that the main logic is executed as above to operate as similar to Unity's animation processing order as possible.
In fact, AnyPortrait's main logic is handled by default in "LateUpdate".
Since users usually write logic in Update, this is quite similar to Unity's processing order.
However, as shown in the picture above, the limitation of this method is that errors may occur with other scripts within LateUpdate.
(For problems and solutions based on execution order, check Related Page.)


This basic structure generally works well, but those who develop elaborate script execution sequences may need more flexible options.




This is the calling sequence when the value of the Main Logic Event option is changed to “Update”.
AnyPortrait's main logic is processed in Update similar to other scripts.
Using this method, more complex processing in LateUpdate will be easier to implement.
Another advantage is that you can receive animation event callbacks quickly.


However, problems with other scripts and call order within Update are more likely to occur.
At a minimum, the script that “requests to play animation” must be executed before the AnyPortrait main logic.
Therefore, if you use this method, be sure to check the explanation below for the script execution order.




When setting the main logic to be executed in the Update event




(1) Change the value of Main Logic Event to Update.
(2) Open Project Settings.
(3) Select the Script Execution Order menu.
(4) Set AnyPortrait.apPortrait to be called later than Default Time. If you have other scripts or assets whose execution order is important, you should carefully consider them when setting the execution order.