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.

SetVariable.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using UnityEngine;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Assigns the value of a variable.
  6. /// </summary>
  7. [UnitShortTitle("Set Variable")]
  8. public sealed class SetVariable : UnifiedVariableUnit
  9. {
  10. /// <summary>
  11. /// The entry point to assign the variable.
  12. /// </summary>
  13. [DoNotSerialize]
  14. [PortLabelHidden]
  15. public ControlInput assign { get; set; }
  16. /// <summary>
  17. /// The value to assign to the variable.
  18. /// </summary>
  19. [DoNotSerialize]
  20. [PortLabel("New Value")]
  21. [PortLabelHidden]
  22. public ValueInput input { get; private set; }
  23. /// <summary>
  24. /// The action to execute once the variable has been assigned.
  25. /// </summary>
  26. [DoNotSerialize]
  27. [PortLabelHidden]
  28. public ControlOutput assigned { get; set; }
  29. /// <summary>
  30. /// The value assigned to the variable.
  31. /// </summary>
  32. [DoNotSerialize]
  33. [PortLabel("Value")]
  34. [PortLabelHidden]
  35. public ValueOutput output { get; private set; }
  36. protected override void Definition()
  37. {
  38. base.Definition();
  39. assign = ControlInput(nameof(assign), Assign);
  40. input = ValueInput<object>(nameof(input)).AllowsNull();
  41. output = ValueOutput<object>(nameof(output));
  42. assigned = ControlOutput(nameof(assigned));
  43. Requirement(name, assign);
  44. Requirement(input, assign);
  45. Assignment(assign, output);
  46. Succession(assign, assigned);
  47. if (kind == VariableKind.Object)
  48. {
  49. Requirement(@object, assign);
  50. }
  51. }
  52. private ControlOutput Assign(Flow flow)
  53. {
  54. var name = flow.GetValue<string>(this.name);
  55. var input = flow.GetValue(this.input);
  56. switch (kind)
  57. {
  58. case VariableKind.Flow:
  59. flow.variables.Set(name, input);
  60. break;
  61. case VariableKind.Graph:
  62. Variables.Graph(flow.stack).Set(name, input);
  63. break;
  64. case VariableKind.Object:
  65. Variables.Object(flow.GetValue<GameObject>(@object)).Set(name, input);
  66. break;
  67. case VariableKind.Scene:
  68. Variables.Scene(flow.stack.scene).Set(name, input);
  69. break;
  70. case VariableKind.Application:
  71. Variables.Application.Set(name, input);
  72. break;
  73. case VariableKind.Saved:
  74. Variables.Saved.Set(name, input);
  75. break;
  76. default:
  77. throw new UnexpectedEnumValueException<VariableKind>(kind);
  78. }
  79. flow.SetValue(output, input);
  80. return assigned;
  81. }
  82. }
  83. }