暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SelectOnFlow.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Unity.VisualScripting
  4. {
  5. /// <summary>
  6. /// Selects a value from a set by matching it with an input flow.
  7. /// </summary>
  8. [UnitCategory("Control")]
  9. [UnitTitle("Select On Flow")]
  10. [UnitShortTitle("Select")]
  11. [UnitSubtitle("On Flow")]
  12. [UnitOrder(8)]
  13. [TypeIcon(typeof(ISelectUnit))]
  14. public sealed class SelectOnFlow : Unit, ISelectUnit
  15. {
  16. [SerializeAs(nameof(branchCount))]
  17. private int _branchCount = 2;
  18. [DoNotSerialize]
  19. [Inspectable, UnitHeaderInspectable("Branches")]
  20. public int branchCount
  21. {
  22. get => _branchCount;
  23. set => _branchCount = Mathf.Clamp(value, 2, 10);
  24. }
  25. [DoNotSerialize]
  26. public Dictionary<ControlInput, ValueInput> branches { get; private set; }
  27. /// <summary>
  28. /// Triggered when any selector is entered.
  29. /// </summary>
  30. [DoNotSerialize]
  31. [PortLabelHidden]
  32. public ControlOutput exit { get; private set; }
  33. /// <summary>
  34. /// The selected value.
  35. /// </summary>
  36. [DoNotSerialize]
  37. [PortLabelHidden]
  38. public ValueOutput selection { get; private set; }
  39. protected override void Definition()
  40. {
  41. branches = new Dictionary<ControlInput, ValueInput>();
  42. selection = ValueOutput<object>(nameof(selection));
  43. exit = ControlOutput(nameof(exit));
  44. for (int i = 0; i < branchCount; i++)
  45. {
  46. var branchValue = ValueInput<object>("value_" + i);
  47. var branchControl = ControlInput("enter_" + i, (flow) => Select(flow, branchValue));
  48. Requirement(branchValue, branchControl);
  49. Assignment(branchControl, selection);
  50. Succession(branchControl, exit);
  51. branches.Add(branchControl, branchValue);
  52. }
  53. }
  54. public ControlOutput Select(Flow flow, ValueInput branchValue)
  55. {
  56. flow.SetValue(selection, flow.GetValue(branchValue));
  57. return exit;
  58. }
  59. }
  60. }