Maze / Library /PackageCache /com.unity.visualscripting@1.8.0 /Runtime /VisualScripting.Flow /Framework /Control /SwitchOnEnum.cs
| using System; | |
| using System.Collections.Generic; | |
| namespace Unity.VisualScripting | |
| { | |
| /// <summary> | |
| /// Branches flow by switching over an enum. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| public sealed class SwitchOnEnum : Unit, IBranchUnit | |
| { | |
| [] | |
| public Dictionary<Enum, ControlOutput> branches { get; private set; } | |
| [] | |
| [] | |
| [] | |
| public Type enumType { get; set; } | |
| /// <summary> | |
| /// The entry point for the switch. | |
| /// </summary> | |
| [] | |
| [] | |
| public ControlInput enter { get; private set; } | |
| /// <summary> | |
| /// The enum value on which to switch. | |
| /// </summary> | |
| [] | |
| [] | |
| public ValueInput @enum { get; private set; } | |
| public override bool canDefine => enumType != null && enumType.IsEnum; | |
| protected override void Definition() | |
| { | |
| branches = new Dictionary<Enum, ControlOutput>(); | |
| enter = ControlInput(nameof(enter), Enter); | |
| @enum = ValueInput(enumType, nameof(@enum)); | |
| Requirement(@enum, enter); | |
| foreach (var valueByName in EnumUtility.ValuesByNames(enumType)) | |
| { | |
| var enumName = valueByName.Key; | |
| var enumValue = valueByName.Value; | |
| // Just like in C#, duplicate switch labels for the same underlying value is prohibited | |
| if (!branches.ContainsKey(enumValue)) | |
| { | |
| var branch = ControlOutput("%" + enumName); | |
| branches.Add(enumValue, branch); | |
| Succession(enter, branch); | |
| } | |
| } | |
| } | |
| public ControlOutput Enter(Flow flow) | |
| { | |
| var @enum = (Enum)flow.GetValue(this.@enum, enumType); | |
| if (branches.ContainsKey(@enum)) | |
| { | |
| return branches[@enum]; | |
| } | |
| else | |
| { | |
| return null; | |
| } | |
| } | |
| } | |
| } | |