修改动画控制器

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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/Assets/Test.cs" charset="UTF-8" />
</component> </component>
</project> </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;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityTimer; using UnityTimer;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
namespace Ether namespace Ether
{ {
[RequireComponent(typeof(Animator), typeof(AnimancerComponent))]
public class AnimController : SerializedMonoBehaviour public class AnimController : SerializedMonoBehaviour
{ {
[Title("动画控制")] [Title("动画控制")]
@ -36,11 +38,12 @@ namespace Ether
[LabelText("动画列表")] [LabelText("动画列表")]
[PropertySpace(10)] [PropertySpace(10)]
[ShowInInspector] [ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "名称", ValueLabel = "动画资源")]
[OnValueChanged("OnValueChanged")] [OnValueChanged("OnValueChanged")]
[PropertyOrder(10)] [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)] // [PropertySpace(20)]
// [LabelText("预览动画")] // [LabelText("预览动画")]
// [ValueDropdown("GetAllAnim")] // [ValueDropdown("GetAllAnim")]
@ -87,20 +90,14 @@ namespace Ether
private void OnValueChanged() private void OnValueChanged()
{ {
// 这里可以添加逻辑来处理值变化,例如清理无效条目 // 这里可以添加逻辑来处理值变化,例如清理无效条目
List<string> keysToRemove = new List<string>(); for (int i = allAnimation.Count - 1; i >= 0; i--)
foreach (var pair in allAnimationSet)
{ {
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) private bool IsAllowedType(object value)
@ -117,7 +114,7 @@ namespace Ether
private IEnumerable<string> GetAllAnim() private IEnumerable<string> GetAllAnim()
{ {
return allAnimationSet.Keys; return allAnimation.Select(value => value.name);
} }
private void Awake() private void Awake()
@ -134,6 +131,8 @@ namespace Ether
animancer = gameObject.AddComponent<AnimancerComponent>(); animancer = gameObject.AddComponent<AnimancerComponent>();
animancer.Animator = animator; animancer.Animator = animator;
} }
allAnimationSet = allAnimation.ToDictionary(item => item.name, item => item);
} }
private void OnEnable() private void OnEnable()
@ -242,5 +241,6 @@ namespace Ether
animancer.Stop(); animancer.Stop();
disableAnimTimer?.Cancel(); disableAnimTimer?.Cancel();
} }
} }
} }

View File

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

View File

@ -1,52 +1,31 @@

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

View File

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

View File

@ -1,152 +1,46 @@

using Animancer;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using Sirenix.OdinInspector;
using UnityEngine;
namespace Ether namespace Ether
{ {
public class StateMachine : Entity public abstract class StateMachine : Entity
{ {
[Label("实际移动速度")] private StateBase currentState;
public float DirectionSpeed = 0f;
[Label("当前方向")] [ShowInInspector]
public Vector3 Direction = Vector3.down; [LabelText("状态列表")]
public List<StateBase> allStates = new List<StateBase>();
private Dictionary<Type, StateBase> allState = new Dictionary<Type, StateBase>();
private DictionaryEx<string, StateBase> allStatesDic = new DictionaryEx<string, 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 void Awake() private void Awake()
{ {
Animator = GetComponent<Animator>(); foreach (var state in allStates)
if (!Animator)
{ {
Animator = gameObject.AddComponent<Animator>(); state.StateMachine = this;
} allStatesDic.TryAdd(state.GetType().Name, state);
Animancer = GetComponent<AnimancerComponent>();
if (!Animancer)
{
Animancer = gameObject.AddComponent<AnimancerComponent>();
Animancer.Animator = Animator;
} }
} }
public void AddState<T>() where T : StateBase, new() public void ChangeState<T>() where T : StateBase
{ {
Type stateType = typeof(T); ChangeState(typeof(T).Name);
AddState(stateType);
} }
public void AddState(string stateType) public void ChangeState(string stateName)
{ {
Type type = CommonTools.GetType(stateType); if (allStatesDic.TryGetValue(stateName, out var state) && state != currentState)
AddState(type);
}
public void AddState(Type stateType)
{
if (allState.ContainsKey(stateType))
{ {
Debug.LogWarning($"该状态机({gameObject.name})已有该状态:{stateType}"); currentState?.Exit();
return; 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); currentState?.Execute();
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;
} }
} }
} }

View File

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

File diff suppressed because one or more lines are too long

View File

@ -9,9 +9,9 @@ public class MoveController : MonoBehaviour
Rigidbody2D rigidbody2d; Rigidbody2D rigidbody2d;
Blackboard blackboard; Blackboard blackboard;
[LabelText("速度属性名称")] [LabelText("速度属性名称")]
public string speedName = "speed"; public string speedName = "speed";
[LabelText("方向属性名称")] [LabelText("方向属性名称")]
public string directionName = "moveDirection"; public string directionName = "moveDirection";
void Start() void Start()
@ -20,7 +20,7 @@ public class MoveController : MonoBehaviour
if (rigidbody2d == null) if (rigidbody2d == null)
{ {
Debug.LogError("Rigidbody2D组件为空"); Debug.LogError("Rigidbody2D组件为空");
return; return;
} }
@ -28,7 +28,7 @@ public class MoveController : MonoBehaviour
if (blackboard == null) if (blackboard == null)
{ {
Debug.LogError("Blackboard组件为空"); Debug.LogError("Blackboard组件为空");
return; return;
} }
} }
@ -44,7 +44,7 @@ public class MoveController : MonoBehaviour
private void MovePosition() private void MovePosition()
{ {
float speed = blackboard.GetVariableValue<float>(speedName); 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); 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 namespace Ether
{ {
[RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour public class PlayerController : Entity
{ {
private Rigidbody2D playerRigid; private Rigidbody2D playerRigid;
private FSMOwner fsmOwer; private FSMOwner fsmOwer;
@ -30,6 +30,17 @@ namespace Ether
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Down, OnDown); EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Down, OnDown);
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Left, OnLeft); EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Left, OnLeft);
EventCenter.AddListener<EtherInputEvent, bool>(EtherInputEvent.Right, OnRight); 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) private void OnUp(bool isDown)

View File

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