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