No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetGraph.cs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using UnityEngine;
  3. namespace Unity.VisualScripting
  4. {
  5. [UnitCategory("Graphs/Graph Nodes")]
  6. public abstract class SetGraph<TGraph, TMacro, TMachine> : Unit
  7. where TGraph : class, IGraph, new()
  8. where TMacro : Macro<TGraph>
  9. where TMachine : Machine<TGraph, TMacro>
  10. {
  11. /// <summary>
  12. /// The entry point for the node.
  13. /// </summary>
  14. [DoNotSerialize]
  15. [PortLabelHidden]
  16. public ControlInput enter { get; protected set; }
  17. /// <summary>
  18. /// The GameObject or the ScriptMachine where the graph will be set.
  19. /// </summary>
  20. [DoNotSerialize]
  21. [PortLabelHidden]
  22. [NullMeansSelf]
  23. public ValueInput target { get; protected set; }
  24. /// <summary>
  25. /// The script graph.
  26. /// </summary>
  27. [DoNotSerialize]
  28. [PortLabel("Graph")]
  29. [PortLabelHidden]
  30. public ValueInput graphInput { get; protected set; }
  31. /// <summary>
  32. /// The graph that has been set to the ScriptMachine.
  33. /// </summary>
  34. [DoNotSerialize]
  35. [PortLabel("Graph")]
  36. [PortLabelHidden]
  37. public ValueOutput graphOutput { get; protected set; }
  38. /// <summary>
  39. /// The action to execute once the graph has been set.
  40. /// </summary>
  41. [DoNotSerialize]
  42. [PortLabelHidden]
  43. public ControlOutput exit { get; protected set; }
  44. protected abstract bool isGameObject { get; }
  45. Type targetType => isGameObject ? typeof(GameObject) : typeof(TMachine);
  46. protected override void Definition()
  47. {
  48. enter = ControlInput(nameof(enter), SetMacro);
  49. target = ValueInput(targetType, nameof(target)).NullMeansSelf();
  50. target.SetDefaultValue(targetType.PseudoDefault());
  51. graphInput = ValueInput<TMacro>(nameof(graphInput), null);
  52. graphOutput = ValueOutput<TMacro>(nameof(graphOutput));
  53. exit = ControlOutput(nameof(exit));
  54. Requirement(graphInput, enter);
  55. Assignment(enter, graphOutput);
  56. Succession(enter, exit);
  57. }
  58. ControlOutput SetMacro(Flow flow)
  59. {
  60. var macro = flow.GetValue<TMacro>(graphInput);
  61. var targetValue = flow.GetValue(target, targetType);
  62. if (targetValue is GameObject go)
  63. {
  64. go.GetComponent<TMachine>().nest.SwitchToMacro(macro);
  65. }
  66. else
  67. {
  68. ((TMachine)targetValue).nest.SwitchToMacro(macro);
  69. }
  70. flow.SetValue(graphOutput, macro);
  71. return exit;
  72. }
  73. }
  74. }