No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValueInputDefinition.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. namespace Unity.VisualScripting
  3. {
  4. public sealed class ValueInputDefinition : ValuePortDefinition, IUnitInputPortDefinition
  5. {
  6. [SerializeAs(nameof(defaultValue))]
  7. private object _defaultvalue;
  8. [Inspectable]
  9. [DoNotSerialize]
  10. public override Type type
  11. {
  12. get
  13. {
  14. return base.type;
  15. }
  16. set
  17. {
  18. base.type = value;
  19. if (!type.IsAssignableFrom(defaultValue))
  20. {
  21. if (ValueInput.SupportsDefaultValue(type))
  22. {
  23. _defaultvalue = type.PseudoDefault();
  24. }
  25. else
  26. {
  27. hasDefaultValue = false;
  28. _defaultvalue = null;
  29. }
  30. }
  31. }
  32. }
  33. [Serialize]
  34. [Inspectable]
  35. public bool hasDefaultValue { get; set; }
  36. [DoNotSerialize]
  37. [Inspectable]
  38. public object defaultValue
  39. {
  40. get
  41. {
  42. return _defaultvalue;
  43. }
  44. set
  45. {
  46. if (type == null)
  47. {
  48. throw new InvalidOperationException("A type must be defined before setting the default value.");
  49. }
  50. if (!ValueInput.SupportsDefaultValue(type))
  51. {
  52. throw new InvalidOperationException("The selected type does not support default values.");
  53. }
  54. Ensure.That(nameof(value)).IsOfType(value, type);
  55. _defaultvalue = value;
  56. }
  57. }
  58. }
  59. }