Ei kuvausta
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.

Literal.cs 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. namespace Unity.VisualScripting
  3. {
  4. /// <summary>
  5. /// Returns a constant value defined from the editor.
  6. /// </summary>
  7. [SpecialUnit]
  8. public sealed class Literal : Unit
  9. {
  10. [Obsolete(Serialization.ConstructorWarning)]
  11. public Literal() : base() { }
  12. public Literal(Type type) : this(type, type.PseudoDefault()) { }
  13. public Literal(Type type, object value) : base()
  14. {
  15. Ensure.That(nameof(type)).IsNotNull(type);
  16. Ensure.That(nameof(value)).IsOfType(value, type);
  17. this.type = type;
  18. this.value = value;
  19. }
  20. // Shouldn't happen through normal use, but can happen
  21. // if deserialization fails to find the type
  22. // https://support.ludiq.io/communities/5/topics/1661-x
  23. public override bool canDefine => type != null;
  24. [SerializeAs(nameof(value))]
  25. private object _value;
  26. [Serialize]
  27. public Type type { get; internal set; }
  28. [DoNotSerialize]
  29. public object value
  30. {
  31. get => _value;
  32. set
  33. {
  34. Ensure.That(nameof(value)).IsOfType(value, type);
  35. _value = value;
  36. }
  37. }
  38. [DoNotSerialize]
  39. [PortLabelHidden]
  40. public ValueOutput output { get; private set; }
  41. protected override void Definition()
  42. {
  43. output = ValueOutput(type, nameof(output), (flow) => value).Predictable();
  44. }
  45. #region Analytics
  46. public override AnalyticsIdentifier GetAnalyticsIdentifier()
  47. {
  48. var aid = new AnalyticsIdentifier
  49. {
  50. Identifier = $"{GetType().FullName}({type.Name})",
  51. Namespace = type.Namespace,
  52. };
  53. aid.Hashcode = aid.Identifier.GetHashCode();
  54. return aid;
  55. }
  56. #endregion
  57. }
  58. }