浏览代码

AnimatorParameterActionSO and refactoring

/main
Ciro Continisio 4 年前
当前提交
ee1d998c
共有 9 个文件被更改,包括 159 次插入40 次删除
  1. 9
      UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs
  2. 2
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs
  3. 88
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs
  4. 8
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor.meta
  5. 42
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs
  6. 11
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs.meta
  7. 39
      UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorBoolActionSO.cs
  8. 0
      /UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs.meta

9
UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs


public virtual void OnStateEnter() { }
public virtual void OnStateExit() { }
/// <summary>
/// This enum is used to create flexible <c>StateActions</c> which can execute in any of the 3 available "moments".
/// The StateAction in this case would have to implement all the relative functions, and use an if statement with this enum as a condition to decide whether to act or not in each moment.
/// </summary>
public enum SpecificMoment
{
OnStateEnter, OnStateExit, OnUpdate,
}
}
}

2
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs


public abstract class StateConditionSO : ScriptableObject
{
[SerializeField]
[Tooltip("The condition will only be evaluated once each frame and cached for subsequent uses.\r\n\r\nThe caching is based on each instance of the State Machine and of this Scriptable Object.")]
[Tooltip("The condition will only be evaluated once each frame, and cached for subsequent uses.\r\n\r\nThe caching is unique to each instance of the State Machine and of the Scriptable Object (i.e. Instances of the State Machine or Condition don't share results if they belong to different GameObjects).")]
internal bool cacheResult = true;
/// <summary>

88
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs


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
{
[SerializeField] private ParameterType _parameterType = default;
[SerializeField] private string _parameterName = default;
[SerializeField] private bool _boolValue = default;
[SerializeField] private int _intValue = default;
[SerializeField] private float _floatValue = default;
[SerializeField] private Moment _whenToRun = default; // Allows this StateActionSO type to be reused for all 3 state moments.
// Bit of a waste to send all three parameters to the StateAction, but it's a small price to pay for a lot of convenience
protected override StateAction CreateAction() => new AnimatorParameterAction(_whenToRun, _parameterName, _parameterType,
_boolValue, _intValue, _floatValue);
public enum ParameterType
{
Bool, Int, Float, Trigger,
}
}
public class AnimatorParameterAction : StateAction
{
//Component references
private Animator _animator;
private int _parameterHash;
private AnimatorParameterActionSO.ParameterType _parameterType;
private bool _boolValue;
private int _intValue;
private float _floatValue;
private SpecificMoment _whenToRun;
public AnimatorParameterAction(SpecificMoment whenToRun, string parameterName,
AnimatorParameterActionSO.ParameterType parameterType,
bool newBoolValue, int newIntValue, float newFloatValue)
{
_whenToRun = whenToRun;
_parameterHash = Animator.StringToHash(parameterName);
_parameterType = parameterType;
_boolValue = newBoolValue;
_intValue = newIntValue;
_floatValue = newFloatValue;
}
public override void Awake(StateMachine stateMachine)
{
_animator = stateMachine.GetComponent<Animator>();
}
public override void OnStateEnter()
{
if(_whenToRun == SpecificMoment.OnStateEnter)
SetParameter();
}
public override void OnStateExit()
{
if (_whenToRun == SpecificMoment.OnStateExit)
SetParameter();
}
private void SetParameter()
{
switch (_parameterType)
{
case AnimatorParameterActionSO.ParameterType.Bool: _animator.SetBool(_parameterHash, _boolValue); break;
case AnimatorParameterActionSO.ParameterType.Int: _animator.SetInteger(_parameterHash, _intValue); break;
case AnimatorParameterActionSO.ParameterType.Float: _animator.SetFloat(_parameterHash, _floatValue); break;
case AnimatorParameterActionSO.ParameterType.Trigger: _animator.SetTrigger(_parameterHash); break;
}
}
public override void OnUpdate() { }
}

8
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor.meta


fileFormatVersion: 2
guid: 6ca1fbffea74a6042b672fad9b27854a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

42
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs


using UnityEditor;
using UnityEngine;
using UOP1.StateMachine;
[CustomEditor(typeof(AnimatorParameterActionSO)), CanEditMultipleObjects]
public class AnimatorParameterActionSOEditor : UnityEditor.Editor
{
private bool _boolValue = default;
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("_whenToRun"));
EditorGUILayout.Space();
EditorGUILayout.LabelField("Animator Parameter", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(serializedObject.FindProperty("_parameterName"), new GUIContent("Name"));
// Draws the appropriate value depending on the type of parameter this SO is going to change on the Animator
SerializedProperty animParamValue = serializedObject.FindProperty("_parameterType");
EditorGUILayout.PropertyField(animParamValue, new GUIContent("Type"));
switch (animParamValue.intValue)
{
case (int)AnimatorParameterActionSO.ParameterType.Bool:
EditorGUILayout.PropertyField(serializedObject.FindProperty("_boolValue"), new GUIContent("Target value"));
break;
case (int)AnimatorParameterActionSO.ParameterType.Int:
EditorGUILayout.PropertyField(serializedObject.FindProperty("_intValue"), new GUIContent("Target value"));
break;
case (int)AnimatorParameterActionSO.ParameterType.Float:
EditorGUILayout.PropertyField(serializedObject.FindProperty("_floatValue"), new GUIContent("Target value"));
break;
}
serializedObject.ApplyModifiedProperties();
}
}

11
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs.meta


fileFormatVersion: 2
guid: 0c57af73454c70b40b825e2933efd6db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

39
UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorBoolActionSO.cs


using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "SetAnimatorBoolAction", menuName = "State Machines/Actions/Set Animator Bool")]
public class AnimatorBoolActionSO : StateActionSO
{
[SerializeField] private string _parameterName = default;
[SerializeField] private bool _newValue = default;
protected override StateAction CreateAction() => new AnimatorParameterAction(_parameterName, _newValue);
}
public class AnimatorParameterAction : StateAction
{
//Component references
private Animator _animator;
private int _parameterHash;
private bool _newValue;
public AnimatorParameterAction(string parameterName, bool newValue)
{
_parameterHash = Animator.StringToHash(parameterName);
_newValue = newValue;
}
public override void Awake(StateMachine stateMachine)
{
_animator = stateMachine.GetComponent<Animator>();
}
public override void OnStateEnter()
{
_animator.SetBool(_parameterHash, _newValue);
}
public override void OnUpdate() { }
}

/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorBoolActionSO.cs.meta → /UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs.meta

正在加载...
取消
保存