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.

NullCheck.cs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityObject = UnityEngine.Object;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Branches flow depending on whether the input is null.
  6. /// </summary>
  7. [UnitCategory("Nulls")]
  8. [TypeIcon(typeof(Null))]
  9. public sealed class NullCheck : Unit
  10. {
  11. /// <summary>
  12. /// The input.
  13. /// </summary>
  14. [DoNotSerialize]
  15. [PortLabelHidden]
  16. public ValueInput input { get; private set; }
  17. /// <summary>
  18. /// The entry point for the null check.
  19. /// </summary>
  20. [DoNotSerialize]
  21. [PortLabelHidden]
  22. public ControlInput enter { get; private set; }
  23. /// <summary>
  24. /// The action to execute if the input is not null.
  25. /// </summary>
  26. [DoNotSerialize]
  27. [PortLabel("Not Null")]
  28. public ControlOutput ifNotNull { get; private set; }
  29. /// <summary>
  30. /// The action to execute if the input is null.
  31. /// </summary>
  32. [DoNotSerialize]
  33. [PortLabel("Null")]
  34. public ControlOutput ifNull { get; private set; }
  35. protected override void Definition()
  36. {
  37. enter = ControlInput(nameof(enter), Enter);
  38. input = ValueInput<object>(nameof(input)).AllowsNull();
  39. ifNotNull = ControlOutput(nameof(ifNotNull));
  40. ifNull = ControlOutput(nameof(ifNull));
  41. Requirement(input, enter);
  42. Succession(enter, ifNotNull);
  43. Succession(enter, ifNull);
  44. }
  45. public ControlOutput Enter(Flow flow)
  46. {
  47. var input = flow.GetValue(this.input);
  48. bool isNull;
  49. if (input is UnityObject)
  50. {
  51. // Required cast because of Unity's custom == operator.
  52. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  53. isNull = (UnityObject)input == null;
  54. }
  55. else
  56. {
  57. isNull = input == null;
  58. }
  59. if (isNull)
  60. {
  61. return ifNull;
  62. }
  63. else
  64. {
  65. return ifNotNull;
  66. }
  67. }
  68. }
  69. }