FANDOM


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

  1. Attach a Variables component to the Animator, and define a Bool, Integer, or Float parameter that matches the name of the Animator parameter you want to sync with.
  2. Set its Link to field to Custom Script
  3. Place the script below into a C# file named AnimatorParameterLink.cs
  4. Add the component Animator Parameter Variable Link to your Animator component
  5. In the component's Inspector, enter the name of the variable/parameter into the "Shared Variable Name" field. If the parameter is only intended to be read - and not set via the variable's value - check "Download Only".

AnimatorParameterLink.cs:

using UnityEngine;
using AC;

public class AnimatorParameterLink : MonoBehaviour
{

public string sharedVariableName;
public bool downloadOnly;
private Variables variables;
private Animator _animator;


private void OnEnable()
{
variables = GetComponent <Variables>();
_animator = GetComponent <Animator>();

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 || variables == null || string.IsNullOrEmpty (sharedVariableName))
{
return;
}

if (this.variables == variables && variable.label == sharedVariableName)
{
switch (variable.type)
{
case VariableType.Boolean:
variable.BooleanValue = _animator.GetBool (sharedVariableName);
break;

case VariableType.Integer:
variable.IntegerValue = _animator.GetInteger (sharedVariableName);
break;

case VariableType.Float:
variable.FloatValue = _animator.GetFloat (sharedVariableName);
break;

default:
break;
}
}
}


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

if (this.variables == variables && variable.label == sharedVariableName)
{
switch (variable.type)
{
case VariableType.Boolean:
_animator.SetBool (sharedVariableName, variable.BooleanValue);
break;

case VariableType.Integer:
_animator.SetInteger (sharedVariableName, variable.IntegerValue);
break;

case VariableType.Float:
_animator.SetFloat (sharedVariableName, variable.FloatValue);
break;

default:
break;
}
}
}
}
Community content is available under CC-BY-SA unless otherwise noted.