177 lines
6.5 KiB
C#
177 lines
6.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace DarkTonic.MasterAudio
|
|
{
|
|
public class MechanimStateParameterCommands : StateMachineBehaviour
|
|
{
|
|
[Tooltip("Select for parameter command to re-invoke each time animation loops without exiting state")]
|
|
[Header("Retrigger Parameter Commands Each Time Anim Loops w/o Exiting State")]
|
|
public bool RetriggerWhenStateLoops;
|
|
|
|
[Tooltip("Invoke A Parameter Command When State Is Entered")]
|
|
[Header("Enter Parameter Command")]
|
|
public bool invokeEnterCommand;
|
|
|
|
[ParameterCommand]
|
|
public string enterParameterCommand = "[None]";
|
|
|
|
[Tooltip("Invoke a Parameter Command when state is Exited")]
|
|
[Header("Exit Parameter Command")]
|
|
public bool invokeExitCommand;
|
|
|
|
[ParameterCommand]
|
|
public string exitParameterCommand = "[None]";
|
|
|
|
[Tooltip("Invoke a Parameter Command timed to the animation state's normalized time. Normalized time is simply the length in time of the animation. Time is represented as a float from 0f - 1f. 0f is the beginning, .5f is the middle, 1f is the end...etc.etc. Select a Start time from 0 - 1.")]
|
|
[Header("Invoke Parameter Command Timed to Animation")]
|
|
public bool invokeAnimTimeCommand;
|
|
|
|
[Tooltip("This value will be compared to the normalizedTime of the animation you are playing. NormalizedTime is represented as a float so 0 is the beginning, 1 is the end and .5f would be the middle etc.")]
|
|
[Range(0f, 1f)]
|
|
public float whenToInvokeCommand;
|
|
|
|
[ParameterCommand]
|
|
public string timedParameterCommand = "[None]";
|
|
|
|
[Tooltip("Invoke a Parameter Command with timed to the animation. This allows you to time your Parameter Command to the actions in you animation. Select the number of Parameter Commands to be invoked, up to 4. Then set the time you want each Parameter Command to invoke with each subsequent time greater than the previous time.")]
|
|
[Header("Invoke Multiple Parameter Commands Timed to Anim")]
|
|
public bool invokeMultiAnimTimeCommand;
|
|
|
|
[Range(0f, 4f)]
|
|
public int numOfMultiCommandsToInvoke;
|
|
|
|
[Tooltip("This value will be compared to the normalizedTime of the animation you are playing. NormalizedTime is represented as a float so 0 is the beginning, 1 is the end and .5f would be the middle etc.")]
|
|
[Range(0f, 1f)]
|
|
public float whenToInvokeMultiCommand1;
|
|
|
|
[Tooltip("This value will be compared to the normalizedTime of the animation you are playing. NormalizedTime is represented as a float so 0 is the beginning, 1 is the end and .5f would be the middle etc.")]
|
|
[Range(0f, 1f)]
|
|
public float whenToInvokeMultiCommand2;
|
|
|
|
[Tooltip("This value will be compared to the normalizedTime of the animation you are playing. NormalizedTime is represented as a float so 0 is the beginning, 1 is the end and .5f would be the middle etc.")]
|
|
[Range(0f, 1f)]
|
|
public float whenToInvokeMultiCommand3;
|
|
|
|
[Tooltip("This value will be compared to the normalizedTime of the animation you are playing. NormalizedTime is represented as a float so 0 is the beginning, 1 is the end and .5f would be the middle etc.")]
|
|
[Range(0f, 1f)]
|
|
public float whenToInvokeMultiCommand4;
|
|
|
|
[ParameterCommand]
|
|
public string MultiTimedCommand = "[None]";
|
|
|
|
private bool _playMultiCommand1 = true;
|
|
|
|
private bool _playMultiCommand2 = true;
|
|
|
|
private bool _playMultiCommand3 = true;
|
|
|
|
private bool _playMultiCommand4 = true;
|
|
|
|
private bool _invokeTimedCommand = true;
|
|
|
|
private Transform _actorTrans;
|
|
|
|
private int _lastRepetition = -1;
|
|
|
|
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
|
{
|
|
_lastRepetition = 0;
|
|
_actorTrans = ActorTrans(animator);
|
|
if (invokeEnterCommand && !(enterParameterCommand == "[None]") && !string.IsNullOrEmpty(enterParameterCommand))
|
|
{
|
|
MasterAudio.InvokeParameterCommand(enterParameterCommand, _actorTrans);
|
|
}
|
|
}
|
|
|
|
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
|
{
|
|
int num = (int)stateInfo.normalizedTime;
|
|
float num2 = stateInfo.normalizedTime - (float)num;
|
|
if (invokeAnimTimeCommand)
|
|
{
|
|
if (!_invokeTimedCommand && RetriggerWhenStateLoops && _lastRepetition >= 0 && num > _lastRepetition)
|
|
{
|
|
_invokeTimedCommand = true;
|
|
}
|
|
if (_invokeTimedCommand && num2 > whenToInvokeCommand)
|
|
{
|
|
_invokeTimedCommand = false;
|
|
MasterAudio.InvokeParameterCommand(timedParameterCommand, _actorTrans);
|
|
}
|
|
}
|
|
if (invokeMultiAnimTimeCommand)
|
|
{
|
|
if (RetriggerWhenStateLoops)
|
|
{
|
|
if (!_playMultiCommand1 && _lastRepetition >= 0 && num > _lastRepetition)
|
|
{
|
|
_playMultiCommand1 = true;
|
|
}
|
|
if (!_playMultiCommand2 && _lastRepetition >= 0 && num > _lastRepetition)
|
|
{
|
|
_playMultiCommand2 = true;
|
|
}
|
|
if (!_playMultiCommand3 && _lastRepetition >= 0 && num > _lastRepetition)
|
|
{
|
|
_playMultiCommand3 = true;
|
|
}
|
|
if (!_playMultiCommand4 && _lastRepetition >= 0 && num > _lastRepetition)
|
|
{
|
|
_playMultiCommand4 = true;
|
|
}
|
|
}
|
|
if (_playMultiCommand1 && !(num2 < whenToInvokeMultiCommand1) && numOfMultiCommandsToInvoke >= 1)
|
|
{
|
|
_playMultiCommand1 = false;
|
|
MasterAudio.InvokeParameterCommand(MultiTimedCommand, _actorTrans);
|
|
}
|
|
if (_playMultiCommand2 && !(num2 < whenToInvokeMultiCommand2) && numOfMultiCommandsToInvoke >= 2)
|
|
{
|
|
_playMultiCommand2 = false;
|
|
MasterAudio.InvokeParameterCommand(MultiTimedCommand, _actorTrans);
|
|
}
|
|
if (_playMultiCommand3 && !(num2 < whenToInvokeMultiCommand3) && numOfMultiCommandsToInvoke >= 3)
|
|
{
|
|
_playMultiCommand3 = false;
|
|
MasterAudio.InvokeParameterCommand(MultiTimedCommand, _actorTrans);
|
|
}
|
|
if (_playMultiCommand4 && !(num2 < whenToInvokeMultiCommand4) && numOfMultiCommandsToInvoke >= 4)
|
|
{
|
|
_playMultiCommand4 = false;
|
|
MasterAudio.InvokeParameterCommand(MultiTimedCommand, _actorTrans);
|
|
}
|
|
}
|
|
_lastRepetition = num;
|
|
}
|
|
|
|
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
|
{
|
|
if (invokeExitCommand && exitParameterCommand != "[None]" && !string.IsNullOrEmpty(exitParameterCommand))
|
|
{
|
|
MasterAudio.InvokeParameterCommand(exitParameterCommand, _actorTrans);
|
|
}
|
|
if (invokeMultiAnimTimeCommand)
|
|
{
|
|
_playMultiCommand1 = true;
|
|
_playMultiCommand2 = true;
|
|
_playMultiCommand3 = true;
|
|
_playMultiCommand4 = true;
|
|
}
|
|
if (invokeAnimTimeCommand)
|
|
{
|
|
_invokeTimedCommand = true;
|
|
}
|
|
}
|
|
|
|
private Transform ActorTrans(Animator anim)
|
|
{
|
|
if (_actorTrans != null)
|
|
{
|
|
return _actorTrans;
|
|
}
|
|
_actorTrans = anim.transform;
|
|
return _actorTrans;
|
|
}
|
|
}
|
|
}
|