File size: 1,818 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 | using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Throws an exception.
/// </summary>
[UnitCategory("Control")]
[UnitOrder(16)]
public sealed class Throw : Unit
{
/// <summary>
/// Whether a custom exception object should be specified manually.
/// </summary>
[Serialize]
[Inspectable, UnitHeaderInspectable("Custom")]
[InspectorToggleLeft]
public bool custom { get; set; }
/// <summary>
/// The entry point to throw the exception.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ControlInput enter { get; private set; }
/// <summary>
/// The message of the exception.
/// </summary>
[DoNotSerialize]
public ValueInput message { get; private set; }
/// <summary>
/// The exception to throw.
/// </summary>
[DoNotSerialize]
public ValueInput exception { get; private set; }
protected override void Definition()
{
if (custom)
{
enter = ControlInput(nameof(enter), ThrowCustom);
exception = ValueInput<Exception>(nameof(exception));
Requirement(exception, enter);
}
else
{
enter = ControlInput(nameof(enter), ThrowMessage);
message = ValueInput(nameof(message), string.Empty);
Requirement(message, enter);
}
}
private ControlOutput ThrowCustom(Flow flow)
{
throw flow.GetValue<Exception>(exception);
}
private ControlOutput ThrowMessage(Flow flow)
{
throw new Exception(flow.GetValue<string>(message));
}
}
}
|