暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StateTransition.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. namespace Unity.VisualScripting
  3. {
  4. public abstract class StateTransition : GraphElement<StateGraph>, IStateTransition
  5. {
  6. public class DebugData : IStateTransitionDebugData
  7. {
  8. public Exception runtimeException { get; set; }
  9. public int lastBranchFrame { get; set; }
  10. public float lastBranchTime { get; set; }
  11. }
  12. protected StateTransition() { }
  13. protected StateTransition(IState source, IState destination)
  14. {
  15. Ensure.That(nameof(source)).IsNotNull(source);
  16. Ensure.That(nameof(destination)).IsNotNull(destination);
  17. if (source.graph != destination.graph)
  18. {
  19. throw new NotSupportedException("Cannot create transitions across state graphs.");
  20. }
  21. this.source = source;
  22. this.destination = destination;
  23. }
  24. public IGraphElementDebugData CreateDebugData()
  25. {
  26. return new DebugData();
  27. }
  28. public override int dependencyOrder => 1;
  29. [Serialize]
  30. public IState source { get; internal set; }
  31. [Serialize]
  32. public IState destination { get; internal set; }
  33. public override void Instantiate(GraphReference instance)
  34. {
  35. base.Instantiate(instance);
  36. if (this is IGraphEventListener listener && instance.GetElementData<State.Data>(source).isActive)
  37. {
  38. listener.StartListening(instance);
  39. }
  40. }
  41. public override void Uninstantiate(GraphReference instance)
  42. {
  43. if (this is IGraphEventListener listener)
  44. {
  45. listener.StopListening(instance);
  46. }
  47. base.Uninstantiate(instance);
  48. }
  49. #region Lifecycle
  50. public void Branch(Flow flow)
  51. {
  52. if (flow.enableDebug)
  53. {
  54. var editorData = flow.stack.GetElementDebugData<DebugData>(this);
  55. editorData.lastBranchFrame = EditorTimeBinding.frame;
  56. editorData.lastBranchTime = EditorTimeBinding.time;
  57. }
  58. try
  59. {
  60. source.OnExit(flow, StateExitReason.Branch);
  61. }
  62. catch (Exception ex)
  63. {
  64. source.HandleException(flow.stack, ex);
  65. throw;
  66. }
  67. source.OnBranchTo(flow, destination);
  68. try
  69. {
  70. destination.OnEnter(flow, StateEnterReason.Branch);
  71. }
  72. catch (Exception ex)
  73. {
  74. destination.HandleException(flow.stack, ex);
  75. throw;
  76. }
  77. }
  78. public abstract void OnEnter(Flow flow);
  79. public abstract void OnExit(Flow flow);
  80. #endregion
  81. #region Analytics
  82. public override AnalyticsIdentifier GetAnalyticsIdentifier()
  83. {
  84. return null;
  85. }
  86. #endregion
  87. }
  88. }