EnumStateMachineExample.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. namespace QFramework.Example
  3. {
  4. public class EnumStateMachineExample : MonoBehaviour,
  5. IOnEvent<EnumStateChangeEvent<EnumStateMachineExample.LeftMouseButtonStates>>
  6. {
  7. /// <summary>
  8. /// 定义枚举状态
  9. /// </summary>
  10. public enum LeftMouseButtonStates
  11. {
  12. Idle,
  13. MousePressed
  14. }
  15. /// <summary>
  16. /// 定义状态机
  17. /// </summary>
  18. private EnumStateMachine<LeftMouseButtonStates> mLeftMouseButtonState;
  19. private void Start()
  20. {
  21. // 第二个参数是,是否发送状态变更事件?
  22. mLeftMouseButtonState = new EnumStateMachine<LeftMouseButtonStates>(gameObject, true);
  23. this.RegisterEvent();
  24. }
  25. public void OnEvent(EnumStateChangeEvent<LeftMouseButtonStates> e)
  26. {
  27. Debug.Log(e.PreviousState + "=>" + e.NewState);
  28. }
  29. private void Update()
  30. {
  31. if (Input.GetMouseButtonDown(0))
  32. {
  33. mLeftMouseButtonState.ChangeState(LeftMouseButtonStates.MousePressed);
  34. }
  35. if (Input.GetMouseButtonUp(0))
  36. {
  37. mLeftMouseButtonState.ChangeState(LeftMouseButtonStates.Idle);
  38. }
  39. }
  40. private void OnDestroy()
  41. {
  42. this.UnRegisterEvent();
  43. }
  44. }
  45. }