using System.Collections.Generic; using UnityEngine; namespace Unity.VisualScripting { /// /// Selects a value from a set by matching it with an input flow. /// [UnitCategory("Control")] [UnitTitle("Select On Flow")] [UnitShortTitle("Select")] [UnitSubtitle("On Flow")] [UnitOrder(8)] [TypeIcon(typeof(ISelectUnit))] public sealed class SelectOnFlow : Unit, ISelectUnit { [SerializeAs(nameof(branchCount))] private int _branchCount = 2; [DoNotSerialize] [Inspectable, UnitHeaderInspectable("Branches")] public int branchCount { get => _branchCount; set => _branchCount = Mathf.Clamp(value, 2, 10); } [DoNotSerialize] public Dictionary branches { get; private set; } /// /// Triggered when any selector is entered. /// [DoNotSerialize] [PortLabelHidden] public ControlOutput exit { get; private set; } /// /// The selected value. /// [DoNotSerialize] [PortLabelHidden] public ValueOutput selection { get; private set; } protected override void Definition() { branches = new Dictionary(); selection = ValueOutput(nameof(selection)); exit = ControlOutput(nameof(exit)); for (int i = 0; i < branchCount; i++) { var branchValue = ValueInput("value_" + i); var branchControl = ControlInput("enter_" + i, (flow) => Select(flow, branchValue)); Requirement(branchValue, branchControl); Assignment(branchControl, selection); Succession(branchControl, exit); branches.Add(branchControl, branchValue); } } public ControlOutput Select(Flow flow, ValueInput branchValue) { flow.SetValue(selection, flow.GetValue(branchValue)); return exit; } } }