Adventure Creator Wikia
(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.'''
This script allows you to sync up an Animator parameter's value with an AC global variable. To use it:
 
  +
# Create a new Global Variable in the Variable's Manager
 
 
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
 
# Set its Link to field to Custom Script
# Place the script below into a C# file named AnimatorParameterVariableLink.cs
+
# 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
# 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
+
# 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;<br>
+
using UnityEngine;
using AC;<br>
+
using AC;
  +
<br>
 
public class AnimatorParameterVariableLink : MonoBehaviour<br>
+
public class AnimatorParameterLink : MonoBehaviour
  +
{
{<br>
 
  +
<br>
 
  +
public string sharedVariableName;
[SerializeField] private Animator _animator = null;<br>
 
  +
public bool downloadOnly;
[SerializeField] private string parameterName = "";<br>
 
 
private Variables variables;
[SerializeField] private int variableIDToSyncWith = 0;<br>
 
 
private Animator _animator;
[SerializeField] private LinkParameterType linkParameterType = LinkParameterType.Float;<br>
 
  +
private enum LinkParameterType { Float, Int, Bool };<br>
 
  +
<br>
 
private void OnEnable ()<br>
+
private void OnEnable()
 
{
{<br>
 
  +
variables = GetComponent <Variables>();
EventManager.OnDownloadVariable += OnDownload;<br>
 
  +
_animator = GetComponent <Animator>();
EventManager.OnUploadVariable += OnUpload;<br>
 
  +
}<br>
 
 
EventManager.OnDownloadVariable += OnDownload;
<br>
 
 
EventManager.OnUploadVariable += OnUpload;
private void OnDisable ()<br>
 
 
}
{<br>
 
  +
EventManager.OnDownloadVariable -= OnDownload;<br>
 
  +
EventManager.OnUploadVariable -= OnUpload;<br>
 
 
private void OnDisable()
}<br>
 
  +
{
<br>
 
 
EventManager.OnDownloadVariable -= OnDownload;
private void OnDownload (GVar variable, Variables variables)<br>
 
 
EventManager.OnUploadVariable -= OnUpload;
{<br>
 
  +
}
if (_animator == null || string.IsNullOrEmpty (parameterName)) return;<br>
 
  +
<br>
 
  +
if (variable.id == variableIDToSyncWith)<br>
 
 
private void OnDownload (GVar variable, Variables variables)
{<br>
 
  +
{
switch (linkParameterType)<br>
 
 
if (_animator == null || variables == null || string.IsNullOrEmpty (sharedVariableName))
{<br>
 
  +
{
case LinkParameterType.Bool:<br>
 
  +
return;
variable.BooleanValue = _animator.GetBool (parameterName);<br>
 
  +
}
break;<br>
 
  +
<br>
 
  +
if (this.variables == variables && variable.label == sharedVariableName)
case LinkParameterType.Float:<br>
 
  +
{
variable.FloatValue = _animator.GetFloat (parameterName);<br>
 
  +
switch (variable.type)
break;<br>
 
 
{
<br>
 
case LinkParameterType.Int:<br>
+
case VariableType.Boolean:
variable.IntegerValue = _animator.GetInteger (parameterName);<br>
+
variable.BooleanValue = _animator.GetBool (sharedVariableName);
break;<br>
+
break;
  +
}<br>
 
  +
case VariableType.Integer:
}<br>
 
 
variable.IntegerValue = _animator.GetInteger (sharedVariableName);
}<br>
 
 
break;
<br>
 
  +
private void OnUpload (GVar variable, Variables variables)<br>
 
 
case VariableType.Float:
{<br>
 
 
variable.FloatValue = _animator.GetFloat (sharedVariableName);
if (_animator == null || string.IsNullOrEmpty (parameterName)) return;<br>
 
 
break;
<br>
 
  +
if (variable.id == variableIDToSyncWith)<br>
 
  +
default:
{<br>
 
 
break;
switch (linkParameterType)<br>
 
{<br>
+
}
  +
}
case LinkParameterType.Bool:<br>
 
  +
}
_animator.SetBool (parameterName, variable.BooleanValue);<br>
 
  +
break;<br>
 
  +
<br>
 
  +
private void OnUpload (GVar variable, Variables variables)
case LinkParameterType.Float:<br>
 
  +
{
_animator.SetFloat (parameterName, variable.FloatValue);<br>
 
 
if (_animator == null || variables == null || string.IsNullOrEmpty (sharedVariableName) || downloadOnly)
break;<br>
 
  +
{
<br>
 
  +
return;
case LinkParameterType.Int:<br>
 
  +
}
_animator.SetInteger (parameterName, variable.IntegerValue);<br>
 
  +
break;<br>
 
  +
if (this.variables == variables && variable.label == sharedVariableName)
}<br>
 
}<br>
+
{
  +
switch (variable.type)
}<br>
 
  +
{
<br>
 
  +
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;
 
}
  +
  +
}
  +
  +
}
   
  +
}
 
  +
</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:

  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;
			}

		}

	}

}