namespace Unity.VisualScripting { /// /// Selects a value from a set by checking if a condition is true or false. /// [UnitCategory("Control")] [UnitTitle("Select")] [TypeIcon(typeof(ISelectUnit))] [UnitOrder(6)] public sealed class SelectUnit : Unit, ISelectUnit { /// /// The condition to check. /// [DoNotSerialize] [PortLabelHidden] public ValueInput condition { get; private set; } /// /// The value to return if the condition is true. /// [DoNotSerialize] [PortLabel("True")] public ValueInput ifTrue { get; private set; } /// /// The value to return if the condition is false. /// [DoNotSerialize] [PortLabel("False")] public ValueInput ifFalse { get; private set; } /// /// The returned value. /// [DoNotSerialize] [PortLabelHidden] public ValueOutput selection { get; private set; } protected override void Definition() { condition = ValueInput(nameof(condition)); ifTrue = ValueInput(nameof(ifTrue)).AllowsNull(); ifFalse = ValueInput(nameof(ifFalse)).AllowsNull(); selection = ValueOutput(nameof(selection), Branch).Predictable(); Requirement(condition, selection); Requirement(ifTrue, selection); Requirement(ifFalse, selection); } public object Branch(Flow flow) { return flow.GetValue(flow.GetValue(condition) ? ifTrue : ifFalse); } } }