AnyPortrait > Manual > Initialize with Script

Initialize with Script


1.1.7

You can use Unity's basic function, "Instantiate", to place objects in the prefab into the scene, or to duplicate objects in the scene.
Instantiate is probably one of the most commonly used functions when creating games in Unity.
When copying characters created by AnyPortrait with the Instantiate function, sometimes you get an error in initialization.
This page explains initialization through a simple code example.
(Please refer to related page about initialization function.)




Let's duplicate the AnyPortrait character placed in the scene.
In this sample, if a player press "Space Bar", the character will be duplicated, placed slightly to the right, and "Move" animation will be played.
In the case, perhaps, you will write a script like below:



Let's put this script into Unity scene and run the game.




If you run the game and press the Space Bar, the character will be cloned but an error will occur.
And "Move" animation is not played.
This error is because the character placed in the Unity scene is not initialized and the Play function is called immediately.


AnyPortrait's system automatically initializes in the first update frame when the character loads.
However, if you call an apPortrait function immediately after it is loaded with the Instantiate function, an error occurs because it is not initialized yet, and the function does not work normally.


There are two ways to solve this problem:
(1) Execute the initialization directly using the script.
(2) Do not call the function of apPortrait at the loaded frame, but wait until the next frame.


This page explains the method in (1).


Let's modify the code as follows.
(Unnecessary comments are deleted.)



The script was modified to call the Initialize function of apPortrait before executing the Play function.
The Initialize function initializes apPortrait so that it can be updated.


Now let's run the game.




The Slime character is duplicated and the Move animation plays without error.


In addition to Initialize, there is also an AsyncInitialize function.
These functions have the following characteristics.


1. Initialize()
: This is the basic initialization function.
When the function is called, all initialization processing is done.
This has the advantage of controlling apPortrait as soon as initialization is complete when the function is called.
If your character has a lot of animations or modifiers, the initialization code is very heavy, and you may experience a temporary decrease in FPS (Frames Per Second) of the game.


2. AsyncInitialize(OnAsyncLinkCompleted onAsyncLinkCompleted)
: This is an asynchronous initialization function.
This function will be a good choice if it is a problem that the FPS drops significantly during initialization while the game is running.
Even if the function is called, the initialization is completed after some updates have been made.
Immediately after this function is called, apPortrait can not be directly controlled.
When the initialization is completed, the event entered as an argument is called.


3. AsyncInitialize(int timePerYield, OnAsyncLinkCompleted onAsyncLinkCompleted)
: This is a function added in v1.1.7.
The timePerYield argument in msec is added to specify the timeout for asynchronous processing units.
The processing time is increased compared to the existing AsyncInitialize function, but it has been improved to recognize the FPS of the game and not affect performance as much as possible.
Good for loading lots of characters at the same time.


Let's write code using asynchronous initialization.



Asynchronous initialization is not complete immediately, so apPortrait can not be controlled immediately after the initialization function call.
We recommend using the event.