Runtime Changes

Overview


MagicaCloth2 supports parameter changes during execution.
Here, I will explain how to manipulate parameters from the script during execution.
It is assumed that the reader is familiar with C# programming in Unity.

Change procedure


Follow the steps below to change it.

  1. Get the SerializeData class of the MagicaCloth component
  2. Change the contents of SerializeData
  3. Call SetParameterChange() of MagicaCloth component

All of MagicaCloth’s modifiable parameters are contained in this SerializeData class.
Rewrite this class directly.

However, not all parameters can be changed.
A comment in the source code indicates whether the parameter can be changed or not.

/// [OK] Runtime changes.
/// [NG] Export/Import with Presets

Finally, notify the system of the change by calling SetParameterChange().
See the ScriptingAPI page for the API used.

Example


Parameter change (1)

using MagicaCloth2;
using UnityEngine;

public class RuntimeParameterTest : MonoBehaviour
{
  public MagicaCloth cloth;
  public ColliderComponent col;

  bool sw = false;

  void Start()
  {

  }

  void Update()
  {
    if (cloth == null || col == null)
      return;

    // Check if MagicaCloth is running
    if (cloth.IsValid() == false)
      return;

    if (Time.frameCount % 100 == 0)
    {
      sw = !sw;
      UpdateParameter();
    }
  }

  /// <summary>
  /// Parameter change.
  /// </summary>
  void UpdateParameter()
  {
    // It manages all parameters that cloth.SerializeData can change at runtime.
    var sdata = cloth.SerializeData;
    if (sw)
    {
      // add collider.
      sdata.colliderCollisionConstraint.colliderList.Add(col);

      // gravity on
      sdata.gravity = 5.0f;
    }
    else
    {
      // remove collider
      sdata.colliderCollisionConstraint.colliderList.Remove(col);

      // gravity off
      sdata.gravity = 0.0f;
    }

    // change notification
    cloth.SetParameterChange();
  }
}

This example adds/removes colliders every 100 frames.
Also change gravity.

This case contains all parameter changes.
Other parameters can be changed by applying this operation.