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.

GetVariable.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using UnityEngine;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Gets the value of a variable.
  6. /// </summary>
  7. public sealed class GetVariable : UnifiedVariableUnit
  8. {
  9. /// <summary>
  10. /// The value of the variable.
  11. /// </summary>
  12. [DoNotSerialize]
  13. [PortLabelHidden]
  14. public ValueOutput value { get; private set; }
  15. /// <summary>
  16. /// The value to return if the variable is not defined.
  17. /// </summary>
  18. [DoNotSerialize]
  19. public ValueInput fallback { get; private set; }
  20. /// <summary>
  21. /// Whether a fallback value should be provided if the
  22. /// variable is not defined.
  23. /// </summary>
  24. [Serialize]
  25. [Inspectable]
  26. [InspectorLabel("Fallback")]
  27. public bool specifyFallback { get; set; } = false;
  28. protected override void Definition()
  29. {
  30. base.Definition();
  31. value = ValueOutput(nameof(value), Get).PredictableIf(IsDefined);
  32. Requirement(name, value);
  33. if (kind == VariableKind.Object)
  34. {
  35. Requirement(@object, value);
  36. }
  37. if (specifyFallback)
  38. {
  39. fallback = ValueInput<object>(nameof(fallback));
  40. Requirement(fallback, value);
  41. }
  42. }
  43. private bool IsDefined(Flow flow)
  44. {
  45. var name = flow.GetValue<string>(this.name);
  46. if (string.IsNullOrEmpty(name))
  47. {
  48. return false;
  49. }
  50. GameObject @object = null;
  51. if (kind == VariableKind.Object)
  52. {
  53. @object = flow.GetValue<GameObject>(this.@object);
  54. if (@object == null)
  55. {
  56. return false;
  57. }
  58. }
  59. var scene = flow.stack.scene;
  60. if (kind == VariableKind.Scene)
  61. {
  62. if (scene == null || !scene.Value.IsValid() || !scene.Value.isLoaded || !Variables.ExistInScene(scene))
  63. {
  64. return false;
  65. }
  66. }
  67. switch (kind)
  68. {
  69. case VariableKind.Flow:
  70. return flow.variables.IsDefined(name);
  71. case VariableKind.Graph:
  72. return Variables.Graph(flow.stack).IsDefined(name);
  73. case VariableKind.Object:
  74. return Variables.Object(@object).IsDefined(name);
  75. case VariableKind.Scene:
  76. return Variables.Scene(scene.Value).IsDefined(name);
  77. case VariableKind.Application:
  78. return Variables.Application.IsDefined(name);
  79. case VariableKind.Saved:
  80. return Variables.Saved.IsDefined(name);
  81. default:
  82. throw new UnexpectedEnumValueException<VariableKind>(kind);
  83. }
  84. }
  85. private object Get(Flow flow)
  86. {
  87. var name = flow.GetValue<string>(this.name);
  88. VariableDeclarations variables;
  89. switch (kind)
  90. {
  91. case VariableKind.Flow:
  92. variables = flow.variables;
  93. break;
  94. case VariableKind.Graph:
  95. variables = Variables.Graph(flow.stack);
  96. break;
  97. case VariableKind.Object:
  98. variables = Variables.Object(flow.GetValue<GameObject>(@object));
  99. break;
  100. case VariableKind.Scene:
  101. variables = Variables.Scene(flow.stack.scene);
  102. break;
  103. case VariableKind.Application:
  104. variables = Variables.Application;
  105. break;
  106. case VariableKind.Saved:
  107. variables = Variables.Saved;
  108. break;
  109. default:
  110. throw new UnexpectedEnumValueException<VariableKind>(kind);
  111. }
  112. if (specifyFallback && !variables.IsDefined(name))
  113. {
  114. return flow.GetValue(fallback);
  115. }
  116. return variables.Get(name);
  117. }
  118. }
  119. }