(typo) Tag: Visual edit |
No edit summary Tag: Visual edit |
||
Line 5: | Line 5: | ||
# Add the component Animator Parameter Variable Link to your Animator component |
# Add the component Animator Parameter Variable Link to your Animator component |
||
# 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". |
# 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: |
+ | AnimatorParameterLink.cs:<syntaxhighlight lang="csharp"> |
− | + | using UnityEngine; |
|
− | + | using AC; |
|
− | <br> |
||
⚫ | |||
− | {<br> |
||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
− | }<br> |
||
− | <br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
⚫ | |||
− | }<br> |
||
− | <br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
− | }<br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | }<br> |
||
− | }<br> |
||
− | <br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
− | }<br> |
||
− | <br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | }<br> |
||
− | }<br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
+ | |||
⚫ | |||
+ | { |
||
⚫ | |||
⚫ | |||
+ | } |
||
+ | |||
+ | |||
⚫ | |||
+ | { |
||
⚫ | |||
+ | { |
||
⚫ | |||
+ | } |
||
+ | |||
⚫ | |||
+ | { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | } |
||
+ | } |
||
+ | |||
+ | |||
⚫ | |||
+ | { |
||
⚫ | |||
+ | { |
||
⚫ | |||
+ | } |
||
+ | |||
⚫ | |||
+ | { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
+ | } |
||
+ | |||
+ | } |
||
+ | |||
+ | } |
||
+ | </syntaxhighlight> |
||
[[Category:General]] |
[[Category:General]] |
Revision as of 07:21, 30 June 2021
This script allows you to sync up an Animator parameter's value with an AC component variable. To use it:
- 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.
- Set its Link to field to Custom Script
- Place the script below into a C# file named AnimatorParameterLink.cs
- Add the component Animator Parameter Variable Link to your Animator component
- 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;
}
}
}
}