Character Instantiation

Overview


To generate a character with MagicaCloth set up at runtime, you will probably use Instantiate().
Here, we will explain the points to keep in mind when instantiating a character and the importance of initializing MagicaCloth.

The Importance of Initialization


When using MagicaCloth, you must first initialize it.
This will cause MagicaCloth to collect the necessary information for your character and begin setup.
Initialization is performed automatically by Start() of the MonoBehaviour component.
Therefore, basically, there is no need to do anything.

One thing to note is that you should not change the character’s animation pose or scale before this initialization is performed.
The character’s state and posture when instantiated should be the same as when you edited the MagicaCloth setup.

Specifically, the following items must not be done before initialization.
Doing so before initialization can cause a variety of unexpected problems.

  • Changing the character’s posture (animation pose)
  • Changing the character’s scale

There is no problem with changing the character’s coordinates or rotation.

Change the initialization location


Initialization is performed by the MonoBehaviour component’s Start(), but you can also change this to Awake().
This will cause initialization to be performed automatically immediately after instantiating the character.
This means you can immediately change the scale or animation pose.
To make this change, use the MagicaSettings component.

Place this component in your scene and change [Initialization Location] to Awake and you’re ready to go.
For more information, see the MagicaSettings component explanation.

You can also make the same changes via the scripting API.
To do this, use SetInitializationLocation().

MagicaManager.SetInitializationLocation(MagicaManager.InitializationLocation.Awake);

Manual Initialization


You can also call initialization manually.
To do this, use Initialize().
The following example instantiates a character, calls initialize manually, and then changes the character’s scale.

var obj = Instantiate(prefab);

// MagicaCloth Initialize
var clothList = obj.GetComponentsInChildren<MagicaCloth>();
foreach (var cloth in clothList)
  cloth.Initialize();

// Scale
obj.transform.localScale = new Vector3(2, 2, 2);

Use pre-built


Previous initialization issues were caused by the system in which MagicaCloth builds simulation data on the fly at runtime.
Because of this system, initialization had to be done in the same position as when editing.

However, it is possible to completely eliminate these problems by using the pre-building function.
The pre-building function creates the simulation data required for execution when editing and saves it as an asset.
This reduces the initialization load at runtime and prevents the initialization-related problems we mentioned above from occurring.
However, pre-building also has disadvantages, such as increasing the amount of asset data and the need to perform construction work manually.

See the pre-build documentation for more information.