File size: 2,962 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Assigns the value of a variable.
/// </summary>
[UnitShortTitle("Set Variable")]
public sealed class SetVariable : UnifiedVariableUnit
{
/// <summary>
/// The entry point to assign the variable.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ControlInput assign { get; set; }
/// <summary>
/// The value to assign to the variable.
/// </summary>
[DoNotSerialize]
[PortLabel("New Value")]
[PortLabelHidden]
public ValueInput input { get; private set; }
/// <summary>
/// The action to execute once the variable has been assigned.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ControlOutput assigned { get; set; }
/// <summary>
/// The value assigned to the variable.
/// </summary>
[DoNotSerialize]
[PortLabel("Value")]
[PortLabelHidden]
public ValueOutput output { get; private set; }
protected override void Definition()
{
base.Definition();
assign = ControlInput(nameof(assign), Assign);
input = ValueInput<object>(nameof(input)).AllowsNull();
output = ValueOutput<object>(nameof(output));
assigned = ControlOutput(nameof(assigned));
Requirement(name, assign);
Requirement(input, assign);
Assignment(assign, output);
Succession(assign, assigned);
if (kind == VariableKind.Object)
{
Requirement(@object, assign);
}
}
private ControlOutput Assign(Flow flow)
{
var name = flow.GetValue<string>(this.name);
var input = flow.GetValue(this.input);
switch (kind)
{
case VariableKind.Flow:
flow.variables.Set(name, input);
break;
case VariableKind.Graph:
Variables.Graph(flow.stack).Set(name, input);
break;
case VariableKind.Object:
Variables.Object(flow.GetValue<GameObject>(@object)).Set(name, input);
break;
case VariableKind.Scene:
Variables.Scene(flow.stack.scene).Set(name, input);
break;
case VariableKind.Application:
Variables.Application.Set(name, input);
break;
case VariableKind.Saved:
Variables.Saved.Set(name, input);
break;
default:
throw new UnexpectedEnumValueException<VariableKind>(kind);
}
flow.SetValue(output, input);
return assigned;
}
}
}
|