File size: 1,706 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | using UnityObject = UnityEngine.Object;
namespace Unity.VisualScripting
{
/// <summary>
/// Provides a fallback value if the input value is null.
/// </summary>
[UnitCategory("Nulls")]
[TypeIcon(typeof(Null))]
public sealed class NullCoalesce : Unit
{
/// <summary>
/// The value.
/// </summary>
[DoNotSerialize]
public ValueInput input { get; private set; }
/// <summary>
/// The fallback to use if the value is null.
/// </summary>
[DoNotSerialize]
public ValueInput fallback { get; private set; }
/// <summary>
/// The returned value.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput result { get; private set; }
protected override void Definition()
{
input = ValueInput<object>(nameof(input)).AllowsNull();
fallback = ValueInput<object>(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;
}
}
}
|