File size: 2,697 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 System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Triggers a custom event.
/// </summary>
[UnitSurtitle("Custom Event")]
[UnitShortTitle("Trigger")]
[TypeIcon(typeof(CustomEvent))]
[UnitCategory("Events")]
[UnitOrder(1)]
public sealed class TriggerCustomEvent : Unit
{
[SerializeAs(nameof(argumentCount))]
private int _argumentCount;
[DoNotSerialize]
public List<ValueInput> arguments { get; private set; }
[DoNotSerialize]
[Inspectable, UnitHeaderInspectable("Arguments")]
public int argumentCount
{
get => _argumentCount;
set => _argumentCount = Mathf.Clamp(value, 0, 10);
}
/// <summary>
/// The entry point to trigger the event.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ControlInput enter { get; private set; }
/// <summary>
/// The name of the event.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueInput name { get; private set; }
/// <summary>
/// The target of the event.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
[NullMeansSelf]
public ValueInput target { get; private set; }
/// <summary>
/// The action to do after the event has been triggered.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ControlOutput exit { get; private set; }
protected override void Definition()
{
enter = ControlInput(nameof(enter), Trigger);
exit = ControlOutput(nameof(exit));
name = ValueInput(nameof(name), string.Empty);
target = ValueInput<GameObject>(nameof(target), null).NullMeansSelf();
arguments = new List<ValueInput>();
for (var i = 0; i < argumentCount; i++)
{
var argument = ValueInput<object>("argument_" + i);
arguments.Add(argument);
Requirement(argument, enter);
}
Requirement(name, enter);
Requirement(target, enter);
Succession(enter, exit);
}
private ControlOutput Trigger(Flow flow)
{
var target = flow.GetValue<GameObject>(this.target);
var name = flow.GetValue<string>(this.name);
var arguments = this.arguments.Select(flow.GetConvertedValue).ToArray();
CustomEvent.Trigger(target, name, arguments);
return exit;
}
}
}
|