Character Instantiation

Overview


When you instantiate a character that contains MagicaCloth at runtime, MagicaCloth will automatically be initialized and begin working.
Therefore, you generally don’t need to do anything.
However, during this initialization, restrictions may be placed on the character’s posture, etc., depending on the situation.
These are classified into the following three types depending on the MagicaCloth version and construction type.

  • Runtime builds for v2.13.0 and earlier
  • Runtime builds for v2.13.0 and later
  • Pre-built

Here we’ll go into more detail about these and the internal process of instantiation.

Run-time and pre-build


First of all, there are two ways that MagicaCloth works: “run-time construction” and “pre-construction.”
Run-time construction is usually used.

Runtime Construction

With runtime construction, cloth data is created on the fly at runtime.
Creating this cloth data takes about 10ms to 30ms on average.
This work occurs in the background so does not affect the progress of the game.
However, because of this work, there is a delay of a few frames between instantiation and the actual start of cloth simulation.

Pre-built

With pre-building, cloth data is created during editing and saved as an asset.
This means that you can start a simulation immediately at runtime.
However, pre-building also has disadvantages, such as an increase in asset data and the need to do the building work manually.
See the pre-building documentation for more information.

Advantages and disadvantages of different build methods


Before we go into the details, let’s briefly summarize the advantages and disadvantages of each build type.
Please note that due to major improvements in run-time builds in v2.13.0, the disadvantages that existed before have been greatly reduced.

Pros Cons
Runtime builds for v2.13.0 and earlier There is no impact even if the MagicaCloth version changes or there are changes to the mesh or bone structure.
No special operations are required even if parameters are changed.
Initialization is very demanding.
It takes time to build the data, which causes a delay before the simulation starts.
Characters must be initialized with the same pose and scale as when they were edited.
Runtime builds for v2.13.0 and later It is not affected by changes in MagicaCloth versions or changes to the mesh or bone structure.
No special operations are required even if parameters are changed.
The character’s pose and scale at initialization can be in any state.
The initialization load is low.
Since it takes time to build the data, there is a delay before the simulation starts.
Pre-built Low initialization cost.
Simulation starts immediately.
The character’s pose and scale can be in any state at initialization.
Operations are required to create cloth data.
Cloth data cannot easily be used due to changes in the MagicaCloth version or changes to the mesh or bone structure.
Cloth data must be rebuilt every time parameters are changed.

Runtime build for V2.13.0 and earlier


In v2.13.0 and earlier, the character must be initialized in the same pose as when it was edited.
This is usually the A pose or T pose.
If the pose is not the same as when it was edited, the vertex attribute data etc. may be misaligned and the simulation may not work.
Specifically, the following items must be the same as when it was edited:

  • Character Posture (Animation Pose)
  • Character Scale

It’s OK to change the character’s coordinates and rotation.
If you want to change the character’s pose immediately after instantiation, you’ll need to manually call initialize before making the change.
How to do this is explained at the end of this page.

Runtime build for V2.13.0 and later


Starting with v2.13.0, the character’s posture is automatically saved when editing.
This data is called initialization data.
Because of this, while previously the posture had to be the same when initializing as when editing, this restriction has been removed.
There is no problem with changing the character’s posture or scale before initialization.

Check the initialization data

Whether a component has initialization data is displayed in the Inspector’s info window.
If [Init Data] is True, the component holds initialization data.

The Inspector’s info window will also show whether initialization data is being used at runtime.
If it says “Success” here, the initialization data was used correctly when the build was completed.

If any number and message is displayed, an error has occurred.
In this case, please recreate the initialization data.

Creating initialization data

Initialization data is created automatically.
Therefore, basically there is no need to do anything.
However, if for some reason the initialization data cannot be created properly, start Vertex Paint once and then close it immediately as follows.
This will create the initialization data.

Alternatively, you can force rebuild the initialization data by selecting “Rebuild InitData” from the component menu.

How to make components before v2.13.0 compatible with initialization data

Components older than v2.13.0 do not have initialization data, but you can easily make them compatible.
In that case, please follow one of the following two methods.

  • If the character is prefabricated, enter prefab mode once
  • Place the character in the scene once

This will create new initialization data.

Change the initialization location


Initialization is performed by the MonoBehaviour component’s Start(), but you can also change this to Awake().
To make this change, use the MagicaSettings component.

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().
Especially in v2.13.0 and earlier, you need to call initialize before changing the character’s pose or scale.
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);