(Updated for latest API) Tag: Visual edit |
Tag: Visual edit |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | '''NOTE: This script is included in AC v1.74.0 and later.''' |
||
⚫ | |||
+ | |||
− | # Create a new Global Variable in the Variable's Manager |
||
⚫ | |||
+ | |||
+ | # 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 |
# Set its Link to field to Custom Script |
||
− | # Place the script below into a C# file named |
+ | # Place the script below into a C# file named AnimatorParameterLink.cs |
# 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". |
+ | AnimatorParameterLink.cs:<syntaxhighlight lang="csharp"> |
||
− | AnimatorParameterVariableLink.cs |
||
− | + | using UnityEngine; |
|
− | + | using AC; |
|
+ | |||
− | <br> |
||
− | + | public class AnimatorParameterLink : MonoBehaviour |
|
+ | { |
||
− | {<br> |
||
+ | |||
− | <br> |
||
+ | public string sharedVariableName; |
||
⚫ | |||
+ | public bool downloadOnly; |
||
− | [SerializeField] private string parameterName = "";<br> |
||
⚫ | |||
− | [SerializeField] private int variableIDToSyncWith = 0;<br> |
||
⚫ | |||
− | [SerializeField] private LinkParameterType linkParameterType = LinkParameterType.Float;<br> |
||
+ | |||
− | private enum LinkParameterType { Float, Int, Bool };<br> |
||
+ | |||
− | <br> |
||
− | + | private void OnEnable() |
|
⚫ | |||
− | {<br> |
||
+ | variables = GetComponent <Variables>(); |
||
⚫ | |||
+ | _animator = GetComponent <Animator>(); |
||
⚫ | |||
+ | |||
− | }<br> |
||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | {<br> |
||
+ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
− | }<br> |
||
+ | { |
||
− | <br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | {<br> |
||
+ | } |
||
⚫ | |||
+ | |||
− | <br> |
||
+ | |||
− | if (variable.id == variableIDToSyncWith)<br> |
||
⚫ | |||
− | {<br> |
||
+ | { |
||
− | switch (linkParameterType)<br> |
||
⚫ | |||
⚫ | |||
+ | { |
||
− | case LinkParameterType.Bool:<br> |
||
+ | return; |
||
⚫ | |||
+ | } |
||
⚫ | |||
+ | |||
− | <br> |
||
+ | if (this.variables == variables && variable.label == sharedVariableName) |
||
⚫ | |||
+ | { |
||
⚫ | |||
+ | switch (variable.type) |
||
⚫ | |||
⚫ | |||
− | <br> |
||
− | + | case VariableType.Boolean: |
|
− | + | variable.BooleanValue = _animator.GetBool (sharedVariableName); |
|
− | + | break; |
|
+ | |||
⚫ | |||
+ | case VariableType.Integer: |
||
− | }<br> |
||
⚫ | |||
− | }<br> |
||
⚫ | |||
− | <br> |
||
+ | |||
⚫ | |||
⚫ | |||
− | {<br> |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
+ | |||
− | if (variable.id == variableIDToSyncWith)<br> |
||
+ | default: |
||
− | {<br> |
||
⚫ | |||
− | switch (linkParameterType)<br> |
||
− | + | } |
|
+ | } |
||
− | case LinkParameterType.Bool:<br> |
||
+ | } |
||
⚫ | |||
+ | |||
⚫ | |||
+ | |||
− | <br> |
||
+ | private void OnUpload (GVar variable, Variables variables) |
||
⚫ | |||
+ | { |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | { |
||
− | <br> |
||
+ | return; |
||
− | case LinkParameterType.Int:<br> |
||
+ | } |
||
⚫ | |||
+ | |||
⚫ | |||
+ | if (this.variables == variables && variable.label == sharedVariableName) |
||
− | }<br> |
||
− | + | { |
|
+ | switch (variable.type) |
||
− | }<br> |
||
+ | { |
||
− | <br> |
||
+ | case VariableType.Boolean: |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
+ | case VariableType.Integer: |
||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
+ | break; |
||
+ | |||
+ | default: |
||
+ | break; |
||
⚫ | |||
+ | |||
+ | } |
||
+ | |||
+ | } |
||
+ | } |
||
⚫ | |||
+ | </syntaxhighlight> |
||
[[Category:General]] |
[[Category:General]] |
Latest revision as of 08:05, 3 September 2021
NOTE: This script is included in AC v1.74.0 and later.
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;
}
}
}
}