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

CreateStruct.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Creates a struct with its default initializer.
  6. /// </summary>
  7. [SpecialUnit]
  8. public sealed class CreateStruct : Unit
  9. {
  10. [Obsolete(Serialization.ConstructorWarning)]
  11. public CreateStruct() : base() { }
  12. public CreateStruct(Type type) : base()
  13. {
  14. Ensure.That(nameof(type)).IsNotNull(type);
  15. if (!type.IsStruct())
  16. {
  17. throw new ArgumentException($"Type {type} must be a struct.", nameof(type));
  18. }
  19. this.type = type;
  20. }
  21. [Serialize]
  22. public Type type { get; internal set; }
  23. // Shouldn't happen through normal use, but can happen
  24. // if deserialization fails to find the type
  25. // https://support.ludiq.io/communities/5/topics/1661-x
  26. public override bool canDefine => type != null;
  27. /// <summary>
  28. /// The entry point to create the struct. You can
  29. /// still get the return value without connecting this port.
  30. /// </summary>
  31. [DoNotSerialize]
  32. [PortLabelHidden]
  33. public ControlInput enter { get; private set; }
  34. /// <summary>
  35. /// The action to call once the struct has been created.
  36. /// </summary>
  37. [DoNotSerialize]
  38. [PortLabelHidden]
  39. public ControlOutput exit { get; private set; }
  40. /// <summary>
  41. /// The created struct.
  42. /// </summary>
  43. [DoNotSerialize]
  44. [PortLabelHidden]
  45. public ValueOutput output { get; private set; }
  46. protected override void Definition()
  47. {
  48. enter = ControlInput(nameof(enter), Enter);
  49. exit = ControlOutput(nameof(exit));
  50. output = ValueOutput(type, nameof(output), Create);
  51. Succession(enter, exit);
  52. }
  53. private ControlOutput Enter(Flow flow)
  54. {
  55. flow.SetValue(output, Activator.CreateInstance(type));
  56. return exit;
  57. }
  58. private object Create(Flow flow)
  59. {
  60. return Activator.CreateInstance(type);
  61. }
  62. }
  63. }