I read the documentation.
https://docs.unity3d.com/Packages/com.unity.visualscripting@1.7/manual/vs-creating-custom-visual-script-event-unit.html
The only way shown by the documentation is to use an event unit and an eventhook. I don’t feel it is a good solution. Because either the rest of my code has to switch to visual scripting events. Or there has to be some extra code interfacing the two.
You can also use the Custom Event API: Events node | Visual Scripting | 1.7.8
That will work. But it is not what I want.
I want my custom unit to be a nice monolithic unit with a control output that probably listen to a c# event and trigger itself.
Yea, UnityVS is pretty insular right now and doesn’t play nice with C# on several levels. C# event support is one of those things. It’s sorta on their roadmap in the up for consideration part of it: https://unity.com/roadmap/unity-platform/gameplay-ui-design
You’ll want to inherit from ManualEventUnit, and Mark it with the attribute UnityCategory. Make sure the root of that path starts with “Events/”.
From there you can override Start and Stop Listening methods with your event/delegate. Here you can bind to them and Unbind. Those methods have a GraphStack or Reference as a parameter. Use this to access your game object, components, ect.
I’ll look into making a tutorial for creating event units.
Thanks for the replies. The following code worked for me.
public class SelectTarget : Unity.VisualScripting.ManualEventUnit<Unit>
{
protected override string hookName => "Target Selected";
[DoNotSerialize]
public ValueOutput Target;
Unit _unit;
CellGrid _cellGrid;
GraphReference _graph;
protected override void Definition()
{
base.Definition();
Target = ValueOutput<Unit>("Target", (flow) => _unit);
}
public override void StartListening(GraphStack stack)
{
base.StartListening(stack);
_graph = stack.AsReference();
_cellGrid = stack.gameObject.GetComponent<CellGrid>();
_cellGrid.UnitClicked -= OnUnitClicked;
_cellGrid.UnitClicked += OnUnitClicked;
}
private void OnUnitClicked(object sender, EventArgs e)
{
_unit = sender as Unit;
Trigger(_graph, sender as Unit);
}
public override void StopListening(GraphStack stack)
{
base.StopListening(stack);
_cellGrid.UnitClicked -= OnUnitClicked;
}
}
Another problem emerged from this.
A ManualEventUnit has one built-in ControlOutput that can be triggered using EventUnit.Trigger(GraphReference, args).
How do I trigger additional control outputs? Say I want to trigger a cancel controloutput in addition to the completed controloutput.
In order to have custom ControlOutputs, you can:
Flow flow = Flow.New(graph_);
// IF method
flow.Run(output);
// IF coroutine
flow.StartCoroutine(output);
For some reason flow.isCoroutine is not working on my end so there might be something wrong with my approach.