这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

79 行
2.3 KiB

using System;
using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
using Moment = UOP1.StateMachine.StateAction.SpecificMoment;
/// <summary>
/// Flexible StateActionSO for the StateMachine which allows to set any parameter on the Animator, in any moment of the state (OnStateEnter, OnStateExit, or each OnUpdate).
/// </summary>
[CreateAssetMenu(fileName = "AnimatorParameterAction", menuName = "State Machines/Actions/Set Animator Parameter")]
public class AnimatorParameterActionSO : StateActionSO
{
public ParameterType parameterType = default;
public string parameterName = default;
public bool boolValue = default;
public int intValue = default;
public float floatValue = default;
public Moment whenToRun = default; // Allows this StateActionSO type to be reused for all 3 state moments
protected override StateAction CreateAction() => new AnimatorParameterAction(Animator.StringToHash(parameterName));
public enum ParameterType
{
Bool, Int, Float, Trigger,
}
}
public class AnimatorParameterAction : StateAction
{
//Component references
private Animator _animator;
private AnimatorParameterActionSO _originSO => (AnimatorParameterActionSO)base.OriginSO; // The SO this StateAction spawned from
private int _parameterHash;
public AnimatorParameterAction(int parameterHash)
{
_parameterHash = parameterHash;
}
public override void Awake(StateMachine stateMachine)
{
_animator = stateMachine.GetComponent<Animator>();
}
public override void OnStateEnter()
{
if (_originSO.whenToRun == SpecificMoment.OnStateEnter)
SetParameter();
}
public override void OnStateExit()
{
if (_originSO.whenToRun == SpecificMoment.OnStateExit)
SetParameter();
}
private void SetParameter()
{
switch (_originSO.parameterType)
{
case AnimatorParameterActionSO.ParameterType.Bool:
_animator.SetBool(_parameterHash, _originSO.boolValue);
break;
case AnimatorParameterActionSO.ParameterType.Int:
_animator.SetInteger(_parameterHash, _originSO.intValue);
break;
case AnimatorParameterActionSO.ParameterType.Float:
_animator.SetFloat(_parameterHash, _originSO.floatValue);
break;
case AnimatorParameterActionSO.ParameterType.Trigger:
_animator.SetTrigger(_parameterHash);
break;
}
}
public override void OnUpdate() { }
}