修改动画控制器

This commit is contained in:
梦语 2024-10-30 17:58:20 +08:00
parent bcf4dba613
commit 16288a41fc
15 changed files with 189 additions and 217 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/Assets/Test.cs" charset="UTF-8" />
</component>
</project>

File diff suppressed because one or more lines are too long

View File

@ -4,12 +4,14 @@ using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityTimer;
using Object = UnityEngine.Object;
namespace Ether
{
[RequireComponent(typeof(Animator), typeof(AnimancerComponent))]
public class AnimController : SerializedMonoBehaviour
{
[Title("动画控制")]
@ -36,11 +38,12 @@ namespace Ether
[LabelText("动画列表")]
[PropertySpace(10)]
[ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "名称", ValueLabel = "动画资源")]
[OnValueChanged("OnValueChanged")]
[PropertyOrder(10)]
public Dictionary<string, Object> allAnimationSet = new Dictionary<string, Object>();
//
public List<Object> allAnimation = new List<Object>();
private Dictionary<string, Object> allAnimationSet;
// [PropertySpace(20)]
// [LabelText("预览动画")]
// [ValueDropdown("GetAllAnim")]
@ -87,20 +90,14 @@ namespace Ether
private void OnValueChanged()
{
// 这里可以添加逻辑来处理值变化,例如清理无效条目
List<string> keysToRemove = new List<string>();
foreach (var pair in allAnimationSet)
for (int i = allAnimation.Count - 1; i >= 0; i--)
{
if (!IsAllowedType(pair.Value))
if (!IsAllowedType(allAnimation[i]))
{
keysToRemove.Add(pair.Key);
allAnimation.Remove(allAnimation[i]);
Debug.LogError("添加的类型只能是AnimationClip或者AnimationSet");
}
}
foreach (var key in keysToRemove)
{
allAnimationSet.Remove(key);
Debug.LogError("添加的类型只能是AnimationClip或者AnimationSet");
}
}
private bool IsAllowedType(object value)
@ -117,7 +114,7 @@ namespace Ether
private IEnumerable<string> GetAllAnim()
{
return allAnimationSet.Keys;
return allAnimation.Select(value => value.name);
}
private void Awake()
@ -134,6 +131,8 @@ namespace Ether
animancer = gameObject.AddComponent<AnimancerComponent>();
animancer.Animator = animator;
}
allAnimationSet = allAnimation.ToDictionary(item => item.name, item => item);
}
private void OnEnable()
@ -242,5 +241,6 @@ namespace Ether
animancer.Stop();
disableAnimTimer?.Cancel();
}
}
}

View File

@ -13,9 +13,12 @@ namespace Ether
[DisallowMultipleComponent]
public class Entity : SerializedMonoBehaviour
{
[Header("实例配置")]
[LabelText("唯一Key值")]
[PropertyOrder(-10)]
public string key;
[LabelText("不销毁对象")]
[PropertyOrder(-9)]
public bool dontDestroy;
private void Awake()
@ -46,9 +49,10 @@ namespace Ether
}
}
[PropertySpace(5, 5)]
[PropertySpace(5, 20)]
[ButtonGroup("ButtonGroup")]
[Button("生成唯一Key", ButtonSizes.Medium)]
[PropertyOrder(-8)]
public void GenerateKey()
{
// 获取当前时间戳并转换为int类型

View File

@ -1,52 +1,31 @@

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ether
{
/// <summary>
/// 状态类型
/// </summary>
public enum StateType
public abstract class StateBase
{
Loop = 0, //循环如idle,walk,run等
Once = 1, //一次:如技能播放等,结束后自动切换会默认状态
public StateMachine StateMachine{ get; set; }
public abstract void Enter();
public abstract void Execute();
public abstract void Exit();
}
/// <summary>
/// 状态所处阶段
/// </summary>
public enum StateStage
public class IdleState : StateBase
{
Playing = 0, //播放中
Over = 1, //结束
}
public abstract class StateBase : IDisposable
{
protected StateMachine machine;
private StateType stateType = StateType.Loop;
public virtual StateType StateType { get { return stateType; } set { stateType = value; } }
public void Init(StateMachine stateMachine)
public override void Enter()
{
this.machine = stateMachine;
OnInit();
}
public virtual void OnInit() { }
public abstract void EnterState();
public abstract void ExitState();
public virtual bool CheckStateIsCanSwitch()
{
return true;
Debug.LogError("IdleState Enter");
}
public virtual void Dispose()
public override void Execute()
{
PoolManager.Inst.Push(this);
}
public override void Exit()
{
}
}
}

View File

@ -1,11 +1,3 @@
fileFormatVersion: 2
guid: d632074cac350ca43972a96ab463b069
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
guid: e1b07429e4a34b499465466ebee1ad57
timeCreated: 1730270948

View File

@ -1,152 +1,46 @@

using Animancer;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Sirenix.OdinInspector;
namespace Ether
{
public class StateMachine : Entity
public abstract class StateMachine : Entity
{
[Label("实际移动速度")]
public float DirectionSpeed = 0f;
[Label("当前方向")]
public Vector3 Direction = Vector3.down;
private StateBase currentState;
private Dictionary<Type, StateBase> allState = new Dictionary<Type, StateBase>();
[ShowInInspector]
[LabelText("状态列表")]
public List<StateBase> allStates = new List<StateBase>();
private StateBase defaultState;
public StateBase DefaultState
{
get
{
if (defaultState == null)
{
allState.First();
defaultState.EnterState();
}
return defaultState;
}
set
{
defaultState = value;
}
}
private StateBase curState;
public StateBase CurState
{
get
{
if (curState == null)
{
allState.First();
}
return curState;
}
set
{
curState = value;
}
}
private Animator Animator { get; set; }
public AnimancerComponent Animancer { get; private set; }
private DictionaryEx<string, StateBase> allStatesDic = new DictionaryEx<string, StateBase>();
private void Awake()
{
Animator = GetComponent<Animator>();
if (!Animator)
foreach (var state in allStates)
{
Animator = gameObject.AddComponent<Animator>();
}
Animancer = GetComponent<AnimancerComponent>();
if (!Animancer)
{
Animancer = gameObject.AddComponent<AnimancerComponent>();
Animancer.Animator = Animator;
state.StateMachine = this;
allStatesDic.TryAdd(state.GetType().Name, state);
}
}
public void AddState<T>() where T : StateBase, new()
public void ChangeState<T>() where T : StateBase
{
Type stateType = typeof(T);
AddState(stateType);
ChangeState(typeof(T).Name);
}
public void AddState(string stateType)
public void ChangeState(string stateName)
{
Type type = CommonTools.GetType(stateType);
AddState(type);
}
public void AddState(Type stateType)
{
if (allState.ContainsKey(stateType))
if (allStatesDic.TryGetValue(stateName, out var state) && state != currentState)
{
Debug.LogWarning($"该状态机({gameObject.name})已有该状态:{stateType}");
return;
currentState?.Exit();
currentState = state;
currentState.Enter();
}
StateBase state = (StateBase)PoolManager.Inst.Get(stateType);
state.Init(this);
allState.Add(stateType, state);
}
public void RemoveState<T>() where T : StateBase, new()
private void Update()
{
Type stateType = typeof(T);
RemoveState(stateType);
}
public void RemoveState(Type stateType)
{
if (!allState.ContainsKey(stateType))
{
Debug.LogWarning($"该状态机({gameObject.name})没有该状态:{stateType}");
return;
}
allState[stateType].Dispose();
allState.Remove(stateType);
}
public bool SwitchState<T>(bool resetState = false) where T : StateBase, new()
{
Type stateType = typeof(T);
return SwitchState(stateType, resetState);
}
public bool SwitchState(Type stateType, bool resetState = false)
{
if (!allState.ContainsKey(stateType))
{
Debug.LogError($"该状态机({gameObject.name})没有该状态:{stateType}");
return false;
}
if (CurState != null)
{
if (stateType == CurState.GetType() && !resetState)
{
return false;
}
if (!CurState.CheckStateIsCanSwitch())
{
Debug.Log($"当前状态:{CurState}不能打断");
return false;
}
CurState.ExitState();
}
var state = allState[stateType];
state.EnterState();
return true;
currentState?.Execute();
}
}
}

View File

@ -1,11 +1,3 @@
fileFormatVersion: 2
guid: e35336666c3d99f4a9c43c045d4b53d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
guid: 6a8cc8e6b8824d55b16e7dbdaeff559a
timeCreated: 1730269951

File diff suppressed because one or more lines are too long

View File

@ -9,9 +9,9 @@ public class MoveController : MonoBehaviour
Rigidbody2D rigidbody2d;
Blackboard blackboard;
[LabelText("速度属性名称")]
[LabelText("速度属性名称")]
public string speedName = "speed";
[LabelText("方向属性名称")]
[LabelText("方向属性名称")]
public string directionName = "moveDirection";
void Start()
@ -20,7 +20,7 @@ public class MoveController : MonoBehaviour
if (rigidbody2d == null)
{
Debug.LogError("Rigidbody2D组件为空");
Debug.LogError("Rigidbody2D组件为空");
return;
}
@ -28,7 +28,7 @@ public class MoveController : MonoBehaviour
if (blackboard == null)
{
Debug.LogError("Blackboard组件为空");
Debug.LogError("Blackboard组件为空");
return;
}
}
@ -44,7 +44,7 @@ public class MoveController : MonoBehaviour
private void MovePosition()
{
float speed = blackboard.GetVariableValue<float>(speedName);
Vector2 tempMove = blackboard.GetVariableValue<Vector2>(directionName) * 0.02f * speed;
Vector2 tempMove = blackboard.GetVariableValue<Vector2>(directionName) * (0.02f * speed);
rigidbody2d.MovePosition(rigidbody2d.position + tempMove);
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0659524d3a26446aa5ae62540c4969fb
timeCreated: 1730274575

View File

@ -7,7 +7,7 @@ using UnityEngine;
namespace Ether
{
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
public class PlayerController : Entity
{
private Rigidbody2D playerRigid;
private FSMOwner fsmOwer;
@ -30,6 +30,17 @@ namespace Ether
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Down, OnDown);
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Left, OnLeft);
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Right, OnRight);
// blackboard.GetVariable<Vector2>("moveDirection").onValueChanged += (value) =>
// {
// if ((Vector2)value == Vector2.zero)
// {
// Debug.LogError("moveDirection zero");
// }
// else
// {
// Debug.LogError("moveDirection :" + (Vector2)value);
// }
// };
}
private void OnUp(bool isDown)

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 59f152b53066484bad12bbc481163a0f
timeCreated: 1730274601