FANDOM


This script allows you to sync up an Animator parameter's value with an AC global variable. To use it:

  1. Create a new Global Variable in the Variable's Manager
  2. Set its Link to field to Custom Script
  3. Place the script below into a C# file named AnimatorParameterVariableLink.cs
  4. Add the component Animator Parameter Variable Link to your Animator component
  5. Configure this component's Inspector: setting the Animator, parameter name and type, as well as the ID number of the Global Variable. This ID number can be found to the left of its name in the Variables Manager

AnimatorParameterVariableLink.cs

using UnityEngine;
using AC;

public class AnimatorParameterVariableLink : MonoBehaviour
{

[SerializeField] private Animator _animator = null;
[SerializeField] private string parameterName = "";
[SerializeField] private int variableIDToSyncWith = 0;
[SerializeField] private LinkParameterType linkParameterType = LinkParameterType.Float;
private enum LinkParameterType { Float, Int, Bool };

private void OnEnable ()
{
EventManager.OnDownloadVariable += OnDownload;
EventManager.OnUploadVariable += OnUpload;
}

private void OnDisable ()
{
EventManager.OnDownloadVariable -= OnDownload;
EventManager.OnUploadVariable -= OnUpload;
}

private void OnDownload (GVar variable, Variables variables)
{
if (_animator == null || string.IsNullOrEmpty (parameterName)) return;

if (variable.id == variableIDToSyncWith)
{
switch (linkParameterType)
{
case LinkParameterType.Bool:
variable.BooleanValue = _animator.GetBool (parameterName);
break;

case LinkParameterType.Float:
variable.FloatValue = _animator.GetFloat (parameterName);
break;

case LinkParameterType.Int:
variable.IntegerValue = _animator.GetInteger (parameterName);
break;
}
}
}

private void OnUpload (GVar variable, Variables variables)
{
if (_animator == null || string.IsNullOrEmpty (parameterName)) return;

if (variable.id == variableIDToSyncWith)
{
switch (linkParameterType)
{
case LinkParameterType.Bool:
_animator.SetBool (parameterName, variable.BooleanValue);
break;

case LinkParameterType.Float:
_animator.SetFloat (parameterName, variable.FloatValue);
break;

case LinkParameterType.Int:
_animator.SetInteger (parameterName, variable.IntegerValue);
break;
}
}
}

}
Community content is available under CC-BY-SA unless otherwise noted.