Ciro Continisio
4 年前
当前提交
ee1d998c
共有 9 个文件被更改,包括 159 次插入 和 40 次删除
-
9UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs
-
2UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs
-
88UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs
-
8UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor.meta
-
42UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs
-
11UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/Editor/AnimatorParameterActionSOEditor.cs.meta
-
39UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorBoolActionSO.cs
-
0/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/AnimatorParameterActionSO.cs.meta
|
|||
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() { } |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 6ca1fbffea74a6042b672fad9b27854a |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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(); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0c57af73454c70b40b825e2933efd6db |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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() { } |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue