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.

NullCoalesce.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityObject = UnityEngine.Object;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Provides a fallback value if the input value is null.
  6. /// </summary>
  7. [UnitCategory("Nulls")]
  8. [TypeIcon(typeof(Null))]
  9. public sealed class NullCoalesce : Unit
  10. {
  11. /// <summary>
  12. /// The value.
  13. /// </summary>
  14. [DoNotSerialize]
  15. public ValueInput input { get; private set; }
  16. /// <summary>
  17. /// The fallback to use if the value is null.
  18. /// </summary>
  19. [DoNotSerialize]
  20. public ValueInput fallback { get; private set; }
  21. /// <summary>
  22. /// The returned value.
  23. /// </summary>
  24. [DoNotSerialize]
  25. [PortLabelHidden]
  26. public ValueOutput result { get; private set; }
  27. protected override void Definition()
  28. {
  29. input = ValueInput<object>(nameof(input)).AllowsNull();
  30. fallback = ValueInput<object>(nameof(fallback));
  31. result = ValueOutput(nameof(result), Coalesce).Predictable();
  32. Requirement(input, result);
  33. Requirement(fallback, result);
  34. }
  35. public object Coalesce(Flow flow)
  36. {
  37. var input = flow.GetValue(this.input);
  38. bool isNull;
  39. if (input is UnityObject)
  40. {
  41. // Required cast because of Unity's custom == operator.
  42. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  43. isNull = (UnityObject)input == null;
  44. }
  45. else
  46. {
  47. isNull = input == null;
  48. }
  49. return isNull ? flow.GetValue(fallback) : input;
  50. }
  51. }
  52. }