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

HasGraph.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace Unity.VisualScripting
  5. {
  6. [UnitCategory("Graphs/Graph Nodes")]
  7. public abstract class HasGraph<TGraph, TMacro, TMachine> : Unit
  8. where TGraph : class, IGraph, new()
  9. where TMacro : Macro<TGraph>
  10. where TMachine : Machine<TGraph, TMacro>
  11. {
  12. /// <summary>
  13. /// The entry point for the node.
  14. /// </summary>
  15. [DoNotSerialize]
  16. [PortLabelHidden]
  17. public ControlInput enter { get; private set; }
  18. /// <summary>
  19. /// The GameObject or the Machine where to look for the graph.
  20. /// </summary>
  21. [DoNotSerialize]
  22. [PortLabelHidden]
  23. [NullMeansSelf]
  24. public ValueInput target { get; private set; }
  25. /// <summary>
  26. /// The Graph to look for.
  27. /// </summary>
  28. [DoNotSerialize]
  29. [PortLabel("Graph")]
  30. [PortLabelHidden]
  31. public ValueInput graphInput { get; private set; }
  32. /// <summary>
  33. /// True if a Graph if found.
  34. /// </summary>
  35. [DoNotSerialize]
  36. [PortLabel("Has Graph")]
  37. [PortLabelHidden]
  38. public ValueOutput hasGraphOutput { get; private set; }
  39. /// <summary>
  40. /// The action to execute once the graph has been set.
  41. /// </summary>
  42. [DoNotSerialize]
  43. [PortLabelHidden]
  44. public ControlOutput exit { get; private set; }
  45. protected abstract bool isGameObject { get; }
  46. Type targetType => isGameObject ? typeof(GameObject) : typeof(TMachine);
  47. protected override void Definition()
  48. {
  49. enter = ControlInput(nameof(enter), TriggerHasGraph);
  50. target = ValueInput(targetType, nameof(target)).NullMeansSelf();
  51. target.SetDefaultValue(targetType.PseudoDefault());
  52. graphInput = ValueInput<TMacro>(nameof(graphInput), null);
  53. hasGraphOutput = ValueOutput(nameof(hasGraphOutput), OutputHasGraph);
  54. exit = ControlOutput(nameof(exit));
  55. Requirement(graphInput, enter);
  56. Assignment(enter, hasGraphOutput);
  57. Succession(enter, exit);
  58. }
  59. ControlOutput TriggerHasGraph(Flow flow)
  60. {
  61. flow.SetValue(hasGraphOutput, OutputHasGraph(flow));
  62. return exit;
  63. }
  64. bool OutputHasGraph(Flow flow)
  65. {
  66. var macro = flow.GetValue<TMacro>(graphInput);
  67. var targetValue = flow.GetValue(target, targetType);
  68. if (targetValue is GameObject gameObject)
  69. {
  70. if (gameObject != null)
  71. {
  72. var stateMachines = gameObject.GetComponents<TMachine>();
  73. macro = flow.GetValue<TMacro>(graphInput);
  74. return stateMachines
  75. .Where(currentMachine => currentMachine != null)
  76. .Any(currentMachine => currentMachine.graph != null && currentMachine.graph.Equals(macro.graph));
  77. }
  78. }
  79. else
  80. {
  81. TMachine machine = flow.GetValue<TMachine>(target);
  82. if (machine.graph != null && machine.graph.Equals(macro.graph))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. }
  90. }