using UnityObject = UnityEngine.Object; namespace Unity.VisualScripting { /// /// Provides a fallback value if the input value is null. /// [UnitCategory("Nulls")] [TypeIcon(typeof(Null))] public sealed class NullCoalesce : Unit { /// /// The value. /// [DoNotSerialize] public ValueInput input { get; private set; } /// /// The fallback to use if the value is null. /// [DoNotSerialize] public ValueInput fallback { get; private set; } /// /// The returned value. /// [DoNotSerialize] [PortLabelHidden] public ValueOutput result { get; private set; } protected override void Definition() { input = ValueInput(nameof(input)).AllowsNull(); fallback = ValueInput(nameof(fallback)); result = ValueOutput(nameof(result), Coalesce).Predictable(); Requirement(input, result); Requirement(fallback, result); } public object Coalesce(Flow flow) { var input = flow.GetValue(this.input); bool isNull; if (input is UnityObject) { // Required cast because of Unity's custom == operator. // ReSharper disable once ConditionIsAlwaysTrueOrFalse isNull = (UnityObject)input == null; } else { isNull = input == null; } return isNull ? flow.GetValue(fallback) : input; } } }