using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Unity.VisualScripting { /// /// Triggers a custom event. /// [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 arguments { get; private set; } [DoNotSerialize] [Inspectable, UnitHeaderInspectable("Arguments")] public int argumentCount { get => _argumentCount; set => _argumentCount = Mathf.Clamp(value, 0, 10); } /// /// The entry point to trigger the event. /// [DoNotSerialize] [PortLabelHidden] public ControlInput enter { get; private set; } /// /// The name of the event. /// [DoNotSerialize] [PortLabelHidden] public ValueInput name { get; private set; } /// /// The target of the event. /// [DoNotSerialize] [PortLabelHidden] [NullMeansSelf] public ValueInput target { get; private set; } /// /// The action to do after the event has been triggered. /// [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(nameof(target), null).NullMeansSelf(); arguments = new List(); for (var i = 0; i < argumentCount; i++) { var argument = ValueInput("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(this.target); var name = flow.GetValue(this.name); var arguments = this.arguments.Select(flow.GetConvertedValue).ToArray(); CustomEvent.Trigger(target, name, arguments); return exit; } } }