This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
using System.ComponentModel;
using UnityEngine;
using UnityObject = UnityEngine.Object;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when an animation event points to TriggerAnimationEvent.
/// </summary>
[UnitCategory("Events/Animation")]
[UnitShortTitle("Animation Event")]
[UnitTitle("Animation Event")]
[TypeIcon(typeof(Animation))]
[DisplayName("Visual Scripting Animation Event")]
public sealed class BoltAnimationEvent : MachineEventUnit<AnimationEvent>
{
protected override string hookName => EventHooks.AnimationEvent;
/// <summary>
/// The string parameter passed to the event.
/// </summary>
[DoNotSerialize]
[PortLabel("String")]
public ValueOutput stringParameter { get; private set; }
/// <summary>
/// The float parameter passed to the event.
/// </summary>
[DoNotSerialize]
[PortLabel("Float")]
public ValueOutput floatParameter { get; private set; }
/// <summary>
/// The integer parameter passed to the function.
/// </summary>
[DoNotSerialize]
[PortLabel("Integer")]
public ValueOutput intParameter { get; private set; }
/// <summary>
/// The Unity object parameter passed to the function.
/// </summary>
[DoNotSerialize]
[PortLabel("Object")]
public ValueOutput objectReferenceParameter { get; private set; }
protected override void Definition()
{
base.Definition();
stringParameter = ValueOutput<string>(nameof(stringParameter));
floatParameter = ValueOutput<float>(nameof(floatParameter));
intParameter = ValueOutput<int>(nameof(intParameter));
objectReferenceParameter = ValueOutput<UnityObject>(nameof(objectReferenceParameter));
}
protected override void AssignArguments(Flow flow, AnimationEvent args)
{
flow.SetValue(stringParameter, args.stringParameter);
flow.SetValue(floatParameter, args.floatParameter);
flow.SetValue(intParameter, args.intParameter);
flow.SetValue(objectReferenceParameter, args.objectReferenceParameter);
}
}
}

View File

@@ -0,0 +1,71 @@
using System.ComponentModel;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when an animation event points to TriggerAnimationEvent.
/// This version allows you to use the string parameter as the event name.
/// </summary>
[UnitCategory("Events/Animation")]
[UnitShortTitle("Animation Event")]
[UnitTitle("Named Animation Event")]
[TypeIcon(typeof(Animation))]
[DisplayName("Visual Scripting Named Animation Event")]
public sealed class BoltNamedAnimationEvent : MachineEventUnit<AnimationEvent>
{
protected override string hookName => EventHooks.AnimationEvent;
/// <summary>
/// The name of the event. The event will only trigger if this value
/// is equal to the string parameter passed in the animation event.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueInput name { get; private set; }
/// <summary>
/// The float parameter passed to the event.
/// </summary>
[DoNotSerialize]
[PortLabel("Float")]
public ValueOutput floatParameter { get; private set; }
/// <summary>
/// The integer parameter passed to the function.
/// </summary>
[DoNotSerialize]
[PortLabel("Integer")]
public ValueOutput intParameter { get; private set; }
/// <summary>
/// The Unity object parameter passed to the function.
/// </summary>
[DoNotSerialize]
[PortLabel("Object")]
public ValueOutput objectReferenceParameter { get; private set; }
protected override void Definition()
{
base.Definition();
name = ValueInput(nameof(name), string.Empty);
floatParameter = ValueOutput<float>(nameof(floatParameter));
intParameter = ValueOutput<int>(nameof(intParameter));
objectReferenceParameter = ValueOutput<GameObject>(nameof(objectReferenceParameter));
}
protected override bool ShouldTrigger(Flow flow, AnimationEvent animationEvent)
{
return CompareNames(flow, name, animationEvent.stringParameter);
}
protected override void AssignArguments(Flow flow, AnimationEvent animationEvent)
{
flow.SetValue(floatParameter, animationEvent.floatParameter);
flow.SetValue(intParameter, animationEvent.intParameter);
flow.SetValue(objectReferenceParameter, animationEvent.objectReferenceParameter);
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called by the Animator Component immediately before it updates its internal IK system.
/// This callback can be used to set the positions of the IK goals and their respective weights.
/// </summary>
[UnitCategory("Events/Animation")]
public sealed class OnAnimatorIK : GameObjectEventUnit<int>
{
public override Type MessageListenerType => typeof(AnimatorMessageListener);
protected override string hookName => EventHooks.OnAnimatorIK;
/// <summary>
/// The index of the layer on which the IK solver is called.
/// </summary>
[DoNotSerialize]
public ValueOutput layerIndex { get; private set; }
protected override void Definition()
{
base.Definition();
layerIndex = ValueOutput<int>(nameof(layerIndex));
}
protected override void AssignArguments(Flow flow, int layerIndex)
{
flow.SetValue(this.layerIndex, layerIndex);
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called at each frame after the state machines and the animations have been evaluated, but before On Animator IK.
/// This callback can be used for processing animation movements for modifying root motion.
/// </summary>
[UnitCategory("Events/Animation")]
public sealed class OnAnimatorMove : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(AnimatorMessageListener);
protected override string hookName => EventHooks.OnAnimatorMove;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the application gains focus.
/// </summary>
[UnitCategory("Events/Application")]
public sealed class OnApplicationFocus : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnApplicationFocus;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the application loses focus.
/// </summary>
[UnitCategory("Events/Application")]
public sealed class OnApplicationLostFocus : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnApplicationLostFocus;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the application pauses.
/// </summary>
[UnitCategory("Events/Application")]
public sealed class OnApplicationPause : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnApplicationPause;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the application quits.
/// </summary>
[UnitCategory("Events/Application")]
public sealed class OnApplicationQuit : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnApplicationQuit;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the application resumes.
/// </summary>
[UnitCategory("Events/Application")]
public sealed class OnApplicationResume : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnApplicationResume;
}
}

View File

@@ -0,0 +1,36 @@
using System.ComponentModel;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a UnityEvent points to TriggerUnityEvent.
/// </summary>
[UnitCategory("Events")]
[UnitTitle("UnityEvent")]
[UnitOrder(2)]
[DisplayName("Visual Scripting Unity Event")]
public sealed class BoltUnityEvent : MachineEventUnit<string>
{
protected override string hookName => EventHooks.UnityEvent;
/// <summary>
/// The name of the event. The event will only trigger if this value
/// is equal to the string parameter passed in the UnityEvent.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueInput name { get; private set; }
protected override void Definition()
{
base.Definition();
name = ValueInput(nameof(name), string.Empty);
}
protected override bool ShouldTrigger(Flow flow, string name)
{
return CompareNames(flow, this.name, name);
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// A special named event with any amount of parameters called manually with the 'Trigger Custom Event' unit.
/// </summary>
[UnitCategory("Events")]
[UnitOrder(0)]
public sealed class CustomEvent : GameObjectEventUnit<CustomEventArgs>
{
public override Type MessageListenerType => null;
protected override string hookName => EventHooks.Custom;
[SerializeAs(nameof(argumentCount))]
private int _argumentCount;
[DoNotSerialize]
[Inspectable, UnitHeaderInspectable("Arguments")]
public int argumentCount
{
get => _argumentCount;
set => _argumentCount = Mathf.Clamp(value, 0, 10);
}
/// <summary>
/// The name of the event.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueInput name { get; private set; }
[DoNotSerialize]
public List<ValueOutput> argumentPorts { get; } = new List<ValueOutput>();
protected override void Definition()
{
base.Definition();
name = ValueInput(nameof(name), string.Empty);
argumentPorts.Clear();
for (var i = 0; i < argumentCount; i++)
{
argumentPorts.Add(ValueOutput<object>("argument_" + i));
}
}
protected override bool ShouldTrigger(Flow flow, CustomEventArgs args)
{
return CompareNames(flow, name, args.name);
}
protected override void AssignArguments(Flow flow, CustomEventArgs args)
{
for (var i = 0; i < argumentCount; i++)
{
flow.SetValue(argumentPorts[i], args.arguments[i]);
}
}
public static void Trigger(GameObject target, string name, params object[] args)
{
EventBus.Trigger(EventHooks.Custom, target, new CustomEventArgs(name, args));
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Unity.VisualScripting
{
public struct CustomEventArgs
{
public readonly string name;
public readonly object[] arguments;
public CustomEventArgs(string name, params object[] arguments)
{
this.name = name;
this.arguments = arguments;
}
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Use to draw gizmos that are always drawn in the editor.
/// </summary>
[UnitCategory("Events/Editor")]
public sealed class OnDrawGizmos : ManualEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnDrawGizmos;
}
}

View File

@@ -0,0 +1,11 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Use to draw gizmos that are drawn in the editor when the object is selected.
/// </summary>
[UnitCategory("Events/Editor")]
public sealed class OnDrawGizmosSelected : ManualEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnDrawGizmosSelected;
}
}

View File

@@ -0,0 +1,193 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Unity.VisualScripting
{
[SerializationVersion("A")]
[SpecialUnit]
public abstract class EventUnit<TArgs> : Unit, IEventUnit, IGraphElementWithData, IGraphEventHandler<TArgs>
{
public class Data : IGraphElementData
{
public EventHook hook;
public Delegate handler;
public bool isListening;
public HashSet<Flow> activeCoroutines = new HashSet<Flow>();
}
public virtual IGraphElementData CreateData()
{
return new Data();
}
/// <summary>
/// Run this event in a coroutine, enabling asynchronous flow like wait units.
/// </summary>
[Serialize]
[Inspectable]
[InspectorExpandTooltip]
public bool coroutine { get; set; } = false;
[DoNotSerialize]
[PortLabelHidden]
public ControlOutput trigger { get; private set; }
[DoNotSerialize]
protected abstract bool register { get; }
protected override void Definition()
{
isControlRoot = true;
trigger = ControlOutput(nameof(trigger));
}
public virtual EventHook GetHook(GraphReference reference)
{
throw new InvalidImplementationException($"Missing event hook for '{this}'.");
}
public virtual void StartListening(GraphStack stack)
{
var data = stack.GetElementData<Data>(this);
if (data.isListening)
{
return;
}
if (register)
{
var reference = stack.ToReference();
var hook = GetHook(reference);
Action<TArgs> handler = args => Trigger(reference, args);
EventBus.Register(hook, handler);
data.hook = hook;
data.handler = handler;
}
data.isListening = true;
}
public virtual void StopListening(GraphStack stack)
{
var data = stack.GetElementData<Data>(this);
if (!data.isListening)
{
return;
}
// The coroutine's flow will dispose at the next frame, letting us
// keep the current flow for clean up operations if needed
foreach (var activeCoroutine in data.activeCoroutines)
{
activeCoroutine.StopCoroutine(false);
}
if (register)
{
EventBus.Unregister(data.hook, data.handler);
data.handler = null;
}
data.isListening = false;
}
public override void Uninstantiate(GraphReference instance)
{
// Here, we're relying on the fact that OnDestroy calls Uninstantiate.
// We need to force-dispose any remaining coroutine to avoid
// memory leaks, because OnDestroy on the runner will not keep
// executing MoveNext() until our soft-destroy call at the end of Flow.Coroutine
// or even dispose the coroutine's enumerator (!).
var data = instance.GetElementData<Data>(this);
var coroutines = data.activeCoroutines.ToHashSetPooled();
#if UNITY_EDITOR
new FrameDelayedCallback(() => StopAllCoroutines(coroutines), 1);
#else
StopAllCoroutines(coroutines);
#endif
base.Uninstantiate(instance);
}
static void StopAllCoroutines(HashSet<Flow> activeCoroutines)
{
// The coroutine's flow will dispose instantly, thus modifying
// the activeCoroutines registry while we enumerate over it
foreach (var activeCoroutine in activeCoroutines)
{
activeCoroutine.StopCoroutineImmediate();
}
activeCoroutines.Free();
}
public bool IsListening(GraphPointer pointer)
{
if (!pointer.hasData)
{
return false;
}
return pointer.GetElementData<Data>(this).isListening;
}
public void Trigger(GraphReference reference, TArgs args)
{
var flow = Flow.New(reference);
if (!ShouldTrigger(flow, args))
{
flow.Dispose();
return;
}
AssignArguments(flow, args);
Run(flow);
}
protected virtual bool ShouldTrigger(Flow flow, TArgs args)
{
return true;
}
protected virtual void AssignArguments(Flow flow, TArgs args)
{
}
private void Run(Flow flow)
{
if (flow.enableDebug)
{
var editorData = flow.stack.GetElementDebugData<IUnitDebugData>(this);
editorData.lastInvokeFrame = EditorTimeBinding.frame;
editorData.lastInvokeTime = EditorTimeBinding.time;
}
if (coroutine)
{
flow.StartCoroutine(trigger, flow.stack.GetElementData<Data>(this).activeCoroutines);
}
else
{
flow.Run(trigger);
}
}
protected static bool CompareNames(Flow flow, ValueInput namePort, string calledName)
{
Ensure.That(nameof(calledName)).IsNotNull(calledName);
return calledName.Trim().Equals(flow.GetValue<string>(namePort)?.Trim(), StringComparison.OrdinalIgnoreCase);
}
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine.EventSystems;
namespace Unity.VisualScripting
{
public abstract class GenericGuiEventUnit : GameObjectEventUnit<BaseEventData>
{
/// <summary>
/// The event data.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
data = ValueOutput<BaseEventData>(nameof(data));
}
protected override void AssignArguments(Flow flow, BaseEventData data)
{
flow.SetValue(this.data, data);
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called on the drag object when dragging is about to begin.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(OnDrag))]
[UnitOrder(16)]
public sealed class OnBeginDrag : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnBeginDragMessageListener);
protected override string hookName => EventHooks.OnBeginDrag;
}
}

View File

@@ -0,0 +1,17 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a user clicks the button and releases it.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(Button))]
[UnitOrder(1)]
public sealed class OnButtonClick : GameObjectEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnButtonClick;
public override Type MessageListenerType => typeof(UnityOnButtonClickMessageListener);
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the cancel button is pressed.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(25)]
public sealed class OnCancel : GenericGuiEventUnit
{
public override Type MessageListenerType => typeof(UnityOnCancelMessageListener);
protected override string hookName => EventHooks.OnCancel;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer deselects the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(23)]
public sealed class OnDeselect : GenericGuiEventUnit
{
public override Type MessageListenerType => typeof(UnityOnDeselectMessageListener);
protected override string hookName => EventHooks.OnDeselect;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// When draging is occuring this will be called every time the cursor is moved.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(17)]
public sealed class OnDrag : PointerEventUnit
{
protected override string hookName => EventHooks.OnDrag;
public override Type MessageListenerType => typeof(UnityOnDragMessageListener);
}
}

View File

@@ -0,0 +1,16 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called on a target that can accept a drop.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(OnDrag))]
[UnitOrder(19)]
public sealed class OnDrop : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnDropMessageListener);
protected override string hookName => EventHooks.OnDrop;
}
}

View File

@@ -0,0 +1,43 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the current value of the dropdown has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(Dropdown))]
[UnitOrder(4)]
public sealed class OnDropdownValueChanged : GameObjectEventUnit<int>
{
public override Type MessageListenerType => typeof(UnityOnDropdownValueChangedMessageListener);
protected override string hookName => EventHooks.OnDropdownValueChanged;
/// <summary>
/// The index of the newly selected option.
/// </summary>
[DoNotSerialize]
public ValueOutput index { get; private set; }
/// <summary>
/// The text of the newly selected option.
/// </summary>
[DoNotSerialize]
public ValueOutput text { get; private set; }
protected override void Definition()
{
base.Definition();
index = ValueOutput<int>(nameof(index));
text = ValueOutput<string>(nameof(text));
}
protected override void AssignArguments(Flow flow, int index)
{
flow.SetValue(this.index, index);
flow.SetValue(text, flow.GetValue<Dropdown>(target).options[index].text);
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called on the drag object when a drag finishes.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(OnDrag))]
[UnitOrder(18)]
public sealed class OnEndDrag : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnEndDragMessageListener);
protected override string hookName => EventHooks.OnEndDrag;
}
}

View File

@@ -0,0 +1,15 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Use to draw immediate mode GUI components.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(GUI))]
[UnitOrder(0)]
public sealed class OnGUI : GlobalEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnGUI;
}
}

View File

@@ -0,0 +1,37 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the user finishes editing the text content either by submitting or by clicking somewhere that removes the
/// focus from the input field.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(InputField))]
[UnitOrder(3)]
public sealed class OnInputFieldEndEdit : GameObjectEventUnit<string>
{
public override Type MessageListenerType => typeof(UnityOnInputFieldEndEditMessageListener);
protected override string hookName => EventHooks.OnInputFieldEndEdit;
/// <summary>
/// The new text content of the input field.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<string>(nameof(value));
}
protected override void AssignArguments(Flow flow, string value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the text content of the input field has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(InputField))]
[UnitOrder(2)]
public sealed class OnInputFieldValueChanged : GameObjectEventUnit<string>
{
public override Type MessageListenerType => typeof(UnityOnInputFieldValueChangedMessageListener);
protected override string hookName => EventHooks.OnInputFieldValueChanged;
/// <summary>
/// The new text content of the input field.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<string>(nameof(value));
}
protected override void AssignArguments(Flow flow, string value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using UnityEngine.EventSystems;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a move event occurs.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(21)]
public sealed class OnMove : GameObjectEventUnit<AxisEventData>
{
public override Type MessageListenerType => typeof(UnityOnMoveMessageListener);
protected override string hookName => EventHooks.OnMove;
/// <summary>
/// The axis event data.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
data = ValueOutput<AxisEventData>(nameof(data));
}
protected override void AssignArguments(Flow flow, AxisEventData data)
{
flow.SetValue(this.data, data);
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer clicks the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(11)]
public sealed class OnPointerClick : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnPointerClickMessageListener);
protected override string hookName => EventHooks.OnPointerClick;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer presses the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(12)]
public sealed class OnPointerDown : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnPointerDownMessageListener);
protected override string hookName => EventHooks.OnPointerDown;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer enters the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(14)]
public sealed class OnPointerEnter : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnPointerEnterMessageListener);
protected override string hookName => EventHooks.OnPointerEnter;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer exits the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(15)]
public sealed class OnPointerExit : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnPointerExitMessageListener);
protected override string hookName => EventHooks.OnPointerExit;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer releases the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(13)]
public sealed class OnPointerUp : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnPointerUpMessageListener);
protected override string hookName => EventHooks.OnPointerUp;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a mouse wheel scrolls.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(20)]
public sealed class OnScroll : PointerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnScrollMessageListener);
protected override string hookName => EventHooks.OnScroll;
}
}

View File

@@ -0,0 +1,37 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the current value of the scrollbar has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(ScrollRect))]
[UnitOrder(7)]
public sealed class OnScrollRectValueChanged : GameObjectEventUnit<Vector2>
{
public override Type MessageListenerType => typeof(UnityOnScrollRectValueChangedMessageListener);
protected override string hookName => EventHooks.OnScrollRectValueChanged;
/// <summary>
/// The new scroll position of the scroll rect.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<Vector2>(nameof(value));
}
protected override void AssignArguments(Flow flow, Vector2 value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the current value of the scrollbar has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(Scrollbar))]
[UnitOrder(6)]
public sealed class OnScrollbarValueChanged : GameObjectEventUnit<float>
{
public override Type MessageListenerType => typeof(UnityOnScrollbarValueChangedMessageListener);
protected override string hookName => EventHooks.OnScrollbarValueChanged;
/// <summary>
/// The new position value of the scrollbar.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<float>(nameof(value));
}
protected override void AssignArguments(Flow flow, float value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the pointer selects the GUI element.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(22)]
public sealed class OnSelect : GenericGuiEventUnit
{
public override Type MessageListenerType => typeof(UnityOnSelectMessageListener);
protected override string hookName => EventHooks.OnSelect;
}
}

View File

@@ -0,0 +1,36 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the current value of the slider has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(Slider))]
[UnitOrder(8)]
public sealed class OnSliderValueChanged : GameObjectEventUnit<float>
{
public override Type MessageListenerType => typeof(UnityOnSliderValueChangedMessageListener);
protected override string hookName => EventHooks.OnSliderValueChanged;
/// <summary>
/// The new numeric value of the slider.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<float>(nameof(value));
}
protected override void AssignArguments(Flow flow, float value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the submit button is pressed.
/// </summary>
[UnitCategory("Events/GUI")]
[UnitOrder(24)]
public sealed class OnSubmit : GenericGuiEventUnit
{
public override Type MessageListenerType => typeof(UnityOnSubmitMessageListener);
protected override string hookName => EventHooks.OnSubmit;
}
}

View File

@@ -0,0 +1,36 @@
using System;
using UnityEngine.UI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the current value of the toggle has changed.
/// </summary>
[UnitCategory("Events/GUI")]
[TypeIcon(typeof(Toggle))]
[UnitOrder(5)]
public sealed class OnToggleValueChanged : GameObjectEventUnit<bool>
{
public override Type MessageListenerType => typeof(UnityOnToggleValueChangedMessageListener);
protected override string hookName => EventHooks.OnToggleValueChanged;
/// <summary>
/// The new boolean value of the toggle.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput value { get; private set; }
protected override void Definition()
{
base.Definition();
value = ValueOutput<bool>(nameof(value));
}
protected override void AssignArguments(Flow flow, bool value)
{
flow.SetValue(this.value, value);
}
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine.EventSystems;
namespace Unity.VisualScripting
{
public abstract class PointerEventUnit : GameObjectEventUnit<PointerEventData>
{
/// <summary>
/// The pointer event data.
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
data = ValueOutput<PointerEventData>(nameof(data));
}
protected override void AssignArguments(Flow flow, PointerEventData data)
{
flow.SetValue(this.data, data);
}
}
}

View File

@@ -0,0 +1,104 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
public abstract class GameObjectEventUnit<TArgs> : EventUnit<TArgs>, IGameObjectEventUnit
{
protected sealed override bool register => true;
public abstract Type MessageListenerType { get; }
public new class Data : EventUnit<TArgs>.Data
{
public GameObject target;
}
public override IGraphElementData CreateData()
{
return new Data();
}
/// <summary>
/// The game object that listens for the event.
/// </summary>
[DoNotSerialize]
[NullMeansSelf]
[PortLabel("Target")]
[PortLabelHidden]
public ValueInput target { get; private set; }
protected override void Definition()
{
base.Definition();
target = ValueInput<GameObject>(nameof(target), null).NullMeansSelf();
}
public override EventHook GetHook(GraphReference reference)
{
if (!reference.hasData)
{
return hookName;
}
var data = reference.GetElementData<Data>(this);
return new EventHook(hookName, data.target);
}
protected virtual string hookName => throw new InvalidImplementationException($"Missing event hook for '{this}'.");
private void UpdateTarget(GraphStack stack)
{
var data = stack.GetElementData<Data>(this);
var wasListening = data.isListening;
var newTarget = Flow.FetchValue<GameObject>(target, stack.ToReference());
if (newTarget != data.target)
{
if (wasListening)
{
StopListening(stack);
}
data.target = newTarget;
if (wasListening)
{
StartListening(stack, false);
}
}
}
protected void StartListening(GraphStack stack, bool updateTarget)
{
if (updateTarget)
{
UpdateTarget(stack);
}
var data = stack.GetElementData<Data>(this);
if (data.target == null)
{
return;
}
if (UnityThread.allowsAPI)
{
if (MessageListenerType != null) // can be null. CustomEvent doesn't need a message listener
MessageListener.AddTo(MessageListenerType, data.target);
}
base.StartListening(stack);
}
public override void StartListening(GraphStack stack)
{
StartListening(stack, true);
}
}
}

View File

@@ -0,0 +1,14 @@
namespace Unity.VisualScripting
{
public abstract class GlobalEventUnit<TArgs> : EventUnit<TArgs>
{
protected override bool register => true;
protected virtual string hookName => throw new InvalidImplementationException();
public override EventHook GetHook(GraphReference reference)
{
return hookName;
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the list of children of the transform of the game object has changed.
/// </summary>
[UnitCategory("Events/Hierarchy")]
public sealed class OnTransformChildrenChanged : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnTransformChildrenChangedMessageListener);
protected override string hookName => EventHooks.OnTransformChildrenChanged;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the parent property of the transform of the game object has changed.
/// </summary>
[UnitCategory("Events/Hierarchy")]
public sealed class OnTransformParentChanged : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnTransformParentChangedMessageListener);
protected override string hookName => EventHooks.OnTransformParentChanged;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
public interface IEventUnit : IUnit, IGraphEventListener
{
bool coroutine { get; }
}
public interface IGameObjectEventUnit : IEventUnit
{
Type MessageListenerType { get; }
}
}

View File

@@ -0,0 +1,187 @@
using System;
using Unity.VisualScripting;
using UnityEngine;
#if PACKAGE_INPUT_SYSTEM_EXISTS
using UnityEngine.InputSystem;
namespace Unity.VisualScripting.InputSystem
{
public enum InputActionChangeOption
{
OnPressed,
OnHold,
OnReleased,
}
public enum OutputType
{
Button,
Float,
Vector2
}
/// <summary>
/// A configurable event to handle input system events.
/// </summary>
[UnitCategory("Events/Input")]
public abstract class OnInputSystemEvent : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName =>
UnityEngine.InputSystem.InputSystem.settings.updateMode ==
InputSettings.UpdateMode.ProcessEventsInDynamicUpdate
? EventHooks.Update
: EventHooks.FixedUpdate;
protected abstract OutputType OutputType { get; }
[Serialize, Inspectable, UnitHeaderInspectable]
public InputActionChangeOption InputActionChangeType;
/// <summary>
/// The InputAction. If it's selected from the dropdown populated from the target PlayerInput, the widget will
/// serialize a fake PlayerAction with the right ID/name, but it will need to be compared to the actual action
/// fetched at runtime from the playerInput's action asset as the fake one won't trigger any event
/// </summary>
[DoNotSerialize]
public ValueInput InputAction { get; private set; }
/// <summary>
/// The target PlayerInput component. if null, the InputAction is expected to be fetched dynamically
/// </summary>
[DoNotSerialize]
[PortLabelHidden]
[NullMeansSelf]
public ValueInput Target { get; private set; }
[PortLabelHidden]
public ValueOutput FloatValue { get; private set; }
[PortLabelHidden]
public ValueOutput Vector2Value { get; private set; }
private InputAction m_Action;
private bool m_WasRunning;
/// <summary>
/// Stores the last value, and returned by the output port. This intermediary value is there to enable
/// "fetching" from the value port, which happens when the value is read, but the node has never been executed.
/// Typical use case is "on some other event, read the value of the axis/joystick even if it was never used"
/// </summary>
private Vector2 m_Value;
protected override void Definition()
{
base.Definition();
Target = ValueInput(typeof(PlayerInput), nameof(Target));
Target.SetDefaultValue(null);
Target.NullMeansSelf();
InputAction = ValueInput(typeof(InputAction), nameof(InputAction));
InputAction.SetDefaultValue(default(InputAction));
switch (OutputType)
{
case OutputType.Button:
break;
case OutputType.Float:
// the getValue delegate is what enables fetching
FloatValue = ValueOutput<float>(nameof(FloatValue), _ => m_Value.x);
break;
case OutputType.Vector2:
// the getValue delegate is what enables fetching
Vector2Value = ValueOutput<Vector2>(nameof(Vector2Value), _ => m_Value);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void StartListening(GraphStack stack)
{
base.StartListening(stack);
var graphReference = stack.ToReference();
var pi = Flow.FetchValue<PlayerInput>(Target, graphReference);
var inputAction = Flow.FetchValue<InputAction>(InputAction, graphReference);
if (inputAction == null)
return;
m_Action = pi
? pi.actions.FindAction(inputAction.id)
: inputAction.actionMap != null ? inputAction : null;
}
public override void StopListening(GraphStack stack)
{
base.StopListening(stack);
m_Action = null;
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
if (m_Action == null)
return false;
bool shouldtrigger;
// "Started" is true while the button is held, triggered is true only one frame. hence what looks like a bug but isn't
switch (InputActionChangeType)
{
case InputActionChangeOption.OnPressed:
shouldtrigger = m_Action.triggered; // started is true too long
break;
case InputActionChangeOption.OnHold:
shouldtrigger = m_Action.phase == InputActionPhase.Started; // triggered is only true one frame
break;
case InputActionChangeOption.OnReleased:
shouldtrigger = m_WasRunning && m_Action.phase != InputActionPhase.Started; // never equal to InputActionPhase.Cancelled when polling
break;
default:
throw new ArgumentOutOfRangeException();
}
DoAssignArguments(flow);
// Hack - can't make sense of the action phase when polled (always Started or Waiting). Fallback on "== Started"
m_WasRunning = m_Action.phase == InputActionPhase.Started;
return shouldtrigger;
}
private void DoAssignArguments(Flow flow)
{
switch (OutputType)
{
case OutputType.Button:
break;
case OutputType.Float:
var readValue = m_Action.ReadValue<float>();
m_Value.Set(readValue, 0);
flow.SetValue(FloatValue, readValue);
break;
case OutputType.Vector2:
var vector2 = m_Action.ReadValue<Vector2>();
m_Value = vector2;
flow.SetValue(Vector2Value, vector2);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public class OnInputSystemEventButton : OnInputSystemEvent
{
protected override OutputType OutputType => OutputType.Button;
}
public class OnInputSystemEventFloat : OnInputSystemEvent
{
protected override OutputType OutputType => OutputType.Float;
}
public class OnInputSystemEventVector2 : OnInputSystemEvent
{
protected override OutputType OutputType => OutputType.Vector2;
}
}
#endif

View File

@@ -0,0 +1,48 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// A configurable event to handle global button input.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnButtonInput : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Update;
/// <summary>
/// The name of the button that received input.
/// </summary>
[DoNotSerialize]
[PortLabel("Name")]
public ValueInput buttonName { get; private set; }
/// <summary>
/// The type of input.
/// </summary>
[DoNotSerialize]
public ValueInput action { get; private set; }
protected override void Definition()
{
base.Definition();
buttonName = ValueInput(nameof(buttonName), string.Empty);
action = ValueInput(nameof(action), PressState.Down);
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var buttonName = flow.GetValue<string>(this.buttonName);
var action = flow.GetValue<PressState>(this.action);
switch (action)
{
case PressState.Down: return Input.GetButtonDown(buttonName);
case PressState.Up: return Input.GetButtonUp(buttonName);
case PressState.Hold: return Input.GetButton(buttonName);
default: throw new UnexpectedEnumValueException<PressState>(action);
}
}
}
}

View File

@@ -0,0 +1,47 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// A configurable event to handle global keyboard input.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnKeyboardInput : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Update;
/// <summary>
/// The key that received input.
/// </summary>
[DoNotSerialize]
public ValueInput key { get; private set; }
/// <summary>
/// The type of input.
/// </summary>
[DoNotSerialize]
public ValueInput action { get; private set; }
protected override void Definition()
{
base.Definition();
key = ValueInput(nameof(key), KeyCode.Space);
action = ValueInput(nameof(action), PressState.Down);
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var key = flow.GetValue<KeyCode>(this.key);
var action = flow.GetValue<PressState>(this.action);
switch (action)
{
case PressState.Down: return Input.GetKeyDown(key);
case PressState.Up: return Input.GetKeyUp(key);
case PressState.Hold: return Input.GetKey(key);
default: throw new UnexpectedEnumValueException<PressState>(action);
}
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the user has pressed the mouse button while over the GUI element or collider.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseDown : GameObjectEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnMouseDown;
public override Type MessageListenerType => typeof(UnityOnMouseDownMessageListener);
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the user has clicked on the GUI element or collider and is still holding down the mouse.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseDrag : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseDragMessageListener);
protected override string hookName => EventHooks.OnMouseDrag;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the mouse enters the GUI element or collider.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseEnter : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseEnterMessageListener);
protected override string hookName => EventHooks.OnMouseEnter;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the mouse is not any longer over the GUI element or collider.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseExit : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseExitMessageListener);
protected override string hookName => EventHooks.OnMouseExit;
}
}

View File

@@ -0,0 +1,47 @@
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// A configurable event to handle global mouse input.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseInput : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Update;
/// <summary>
/// The mouse button that received input.
/// </summary>
[DoNotSerialize]
public ValueInput button { get; private set; }
/// <summary>
/// The type of input.
/// </summary>
[DoNotSerialize]
public ValueInput action { get; private set; }
protected override void Definition()
{
base.Definition();
button = ValueInput(nameof(button), MouseButton.Left);
action = ValueInput(nameof(action), PressState.Down);
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var button = (int)flow.GetValue<MouseButton>(this.button);
var action = flow.GetValue<PressState>(this.action);
switch (action)
{
case PressState.Down: return Input.GetMouseButtonDown(button);
case PressState.Up: return Input.GetMouseButtonUp(button);
case PressState.Hold: return Input.GetMouseButton(button);
default: throw new UnexpectedEnumValueException<PressState>(action);
}
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called every frame while the mouse is over the GUI element or collider.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseOver : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseOverMessageListener);
protected override string hookName => EventHooks.OnMouseOver;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the user has released the mouse button.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseUp : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseUpMessageListener);
protected override string hookName => EventHooks.OnMouseUp;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the mouse is released over the same GUI element or collider as it was pressed.
/// </summary>
[UnitCategory("Events/Input")]
public sealed class OnMouseUpAsButton : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnMouseUpAsButtonMessageListener);
protected override string hookName => EventHooks.OnMouseUpAsButton;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called every fixed framerate frame.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(4)]
public sealed class FixedUpdate : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.FixedUpdate;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called every frame after all update functions have been called.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(5)]
public sealed class LateUpdate : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.LateUpdate;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called before the machine is destroyed.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(7)]
public sealed class OnDestroy : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnDestroy;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the machine becomes disabled or inactive.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(6)]
public sealed class OnDisable : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnDisable;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the machine becomes enabled and active.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(1)]
public sealed class OnEnable : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.OnEnable;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called the first time a machine is enabled before any update method.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(2)]
public sealed class Start : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Start;
}
}

View File

@@ -0,0 +1,12 @@
namespace Unity.VisualScripting
{
/// <summary>
/// Called every frame.
/// </summary>
[UnitCategory("Events/Lifecycle")]
[UnitOrder(3)]
public sealed class Update : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Update;
}
}

View File

@@ -0,0 +1,14 @@
namespace Unity.VisualScripting
{
public abstract class MachineEventUnit<TArgs> : EventUnit<TArgs>
{
protected sealed override bool register => true;
public override EventHook GetHook(GraphReference reference)
{
return new EventHook(hookName, reference.machine);
}
protected virtual string hookName => throw new InvalidImplementationException($"Missing event hook for '{this}'.");
}
}

View File

@@ -0,0 +1,14 @@
namespace Unity.VisualScripting
{
public abstract class ManualEventUnit<TArgs> : EventUnit<TArgs>
{
protected sealed override bool register => false;
protected abstract string hookName { get; }
public sealed override EventHook GetHook(GraphReference reference)
{
return hookName;
}
}
}

View File

@@ -0,0 +1,42 @@
using UnityEngine.AI;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the nav mesh agent comes within a certain threshold of its destination.
/// </summary>
[UnitCategory("Events/Navigation")]
public sealed class OnDestinationReached : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => EventHooks.Update;
/// <summary>
/// The threshold for the remaining distance.
/// </summary>
[DoNotSerialize]
public ValueInput threshold { get; private set; }
/// <summary>
/// Whether the event should only trigger when the path is not partial or invalid.
/// </summary>
[DoNotSerialize]
public ValueInput requireSuccess { get; private set; }
protected override void Definition()
{
base.Definition();
threshold = ValueInput(nameof(threshold), 0.05f);
requireSuccess = ValueInput(nameof(requireSuccess), true);
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var navMeshAgent = flow.stack.gameObject.GetComponent<NavMeshAgent>();
return navMeshAgent != null &&
navMeshAgent.remainingDistance <= flow.GetValue<float>(threshold) &&
(navMeshAgent.pathStatus == NavMeshPathStatus.PathComplete || !flow.GetValue<bool>(requireSuccess));
}
}
}

View File

@@ -0,0 +1,58 @@
using UnityEngine;
namespace Unity.VisualScripting
{
[UnitCategory("Events/Physics")]
public abstract class CollisionEventUnit : GameObjectEventUnit<Collision>
{
/// <summary>
/// The collider we hit.
/// </summary>
[DoNotSerialize]
public ValueOutput collider { get; private set; }
/// <summary>
/// The contact points generated by the physics engine.
/// </summary>
[DoNotSerialize]
public ValueOutput contacts { get; private set; }
/// <summary>
/// The total impulse applied to this contact pair to resolve the collision.
/// </summary>
[DoNotSerialize]
public ValueOutput impulse { get; private set; }
/// <summary>
/// The relative linear velocity of the two colliding objects.
/// </summary>
[DoNotSerialize]
public ValueOutput relativeVelocity { get; private set; }
/// <summary>
/// The complete collision data object.
/// </summary>
[DoNotSerialize]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
collider = ValueOutput<Collider>(nameof(collider));
contacts = ValueOutput<ContactPoint[]>(nameof(contacts));
impulse = ValueOutput<Vector3>(nameof(impulse));
relativeVelocity = ValueOutput<Vector3>(nameof(relativeVelocity));
data = ValueOutput<Collision>(nameof(data));
}
protected override void AssignArguments(Flow flow, Collision collision)
{
flow.SetValue(collider, collision.collider);
flow.SetValue(contacts, collision.contacts);
flow.SetValue(impulse, collision.impulse);
flow.SetValue(relativeVelocity, collision.relativeVelocity);
flow.SetValue(data, collision);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when this collider / rigidbody has begun touching another rigidbody / collider.
/// </summary>
public sealed class OnCollisionEnter : CollisionEventUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionEnterMessageListener);
protected override string hookName => EventHooks.OnCollisionEnter;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when this collider / rigidbody has stopped touching another rigidbody / collider.
/// </summary>
public sealed class OnCollisionExit : CollisionEventUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionExitMessageListener);
protected override string hookName => EventHooks.OnCollisionExit;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called once per frame for every collider / rigidbody that is touching rigidbody / collider.
/// </summary>
public sealed class OnCollisionStay : CollisionEventUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionStayMessageListener);
protected override string hookName => EventHooks.OnCollisionStay;
}
}

View File

@@ -0,0 +1,82 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the controller hits a collider while performing a move.
/// </summary>
[UnitCategory("Events/Physics")]
[TypeIcon(typeof(CharacterController))]
public sealed class OnControllerColliderHit : GameObjectEventUnit<ControllerColliderHit>
{
public override Type MessageListenerType => typeof(UnityOnControllerColliderHitMessageListener);
protected override string hookName => EventHooks.OnControllerColliderHit;
/// <summary>
/// The collider that was hit by the controller.
/// </summary>
[DoNotSerialize]
public ValueOutput collider { get; private set; }
/// <summary>
/// The controller that hit the collider.
/// </summary>
[DoNotSerialize]
public ValueOutput controller { get; private set; }
/// <summary>
/// The direction the CharacterController was moving in when the collision occured.
/// </summary>
[DoNotSerialize]
public ValueOutput moveDirection { get; private set; }
/// <summary>
/// How far the character has travelled until it hit the collider.
/// </summary>
[DoNotSerialize]
public ValueOutput moveLength { get; private set; }
/// <summary>
/// The normal of the surface we collided with in world space.
/// </summary>
[DoNotSerialize]
public ValueOutput normal { get; private set; }
/// <summary>
/// The impact point in world space.
/// </summary>
[DoNotSerialize]
public ValueOutput point { get; private set; }
/// <summary>
/// The impact point in world space.
/// </summary>
[DoNotSerialize]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
collider = ValueOutput<Collider>(nameof(collider));
controller = ValueOutput<CharacterController>(nameof(controller));
moveDirection = ValueOutput<Vector3>(nameof(moveDirection));
moveLength = ValueOutput<float>(nameof(moveLength));
normal = ValueOutput<Vector3>(nameof(normal));
point = ValueOutput<Vector3>(nameof(point));
data = ValueOutput<ControllerColliderHit>(nameof(data));
}
protected override void AssignArguments(Flow flow, ControllerColliderHit hitData)
{
flow.SetValue(collider, hitData.collider);
flow.SetValue(controller, hitData.controller);
flow.SetValue(moveDirection, hitData.moveDirection);
flow.SetValue(moveLength, hitData.moveLength);
flow.SetValue(normal, hitData.normal);
flow.SetValue(point, hitData.point);
flow.SetValue(data, hitData);
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a joint attached to the same game object broke.
/// </summary>
[UnitCategory("Events/Physics")]
public sealed class OnJointBreak : GameObjectEventUnit<float>
{
public override Type MessageListenerType => typeof(UnityOnJointBreakMessageListener);
protected override string hookName => EventHooks.OnJointBreak;
/// <summary>
/// The force that was applied for this joint to break.
/// </summary>
[DoNotSerialize]
public ValueOutput breakForce { get; private set; }
protected override void Definition()
{
base.Definition();
breakForce = ValueOutput<float>(nameof(breakForce));
}
protected override void AssignArguments(Flow flow, float breakForce)
{
flow.SetValue(this.breakForce, breakForce);
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a particle hits a collider.
/// </summary>
[UnitCategory("Events/Physics")]
public sealed class OnParticleCollision : GameObjectEventUnit<GameObject>
{
public override Type MessageListenerType => typeof(UnityOnParticleCollisionMessageListener);
protected override string hookName => EventHooks.OnParticleCollision;
/// <summary>
/// A game object with an attached collider struck by the particle system.
/// </summary>
[DoNotSerialize]
public ValueOutput other { get; private set; }
/// <summary>
/// The particle collision events.
/// </summary>
[DoNotSerialize]
public ValueOutput collisionEvents { get; private set; }
protected override void Definition()
{
base.Definition();
other = ValueOutput<GameObject>(nameof(other));
collisionEvents = ValueOutput<List<ParticleCollisionEvent>>(nameof(collisionEvents));
}
protected override void AssignArguments(Flow flow, GameObject other)
{
flow.SetValue(this.other, other);
var collisionEvents = new List<ParticleCollisionEvent>();
var data = flow.stack.GetElementData<Data>(this);
data.target.GetComponent<ParticleSystem>().GetCollisionEvents(other, collisionEvents);
flow.SetValue(this.collisionEvents, collisionEvents);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a collider enters the trigger.
/// </summary>
public sealed class OnTriggerEnter : TriggerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerEnterMessageListener);
protected override string hookName => EventHooks.OnTriggerEnter;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a collider exits the trigger.
/// </summary>
public sealed class OnTriggerExit : TriggerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerExitMessageListener);
protected override string hookName => EventHooks.OnTriggerExit;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called once per frame for every collider that is touching the trigger.
/// </summary>
public sealed class OnTriggerStay : TriggerEventUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerStayMessageListener);
protected override string hookName => EventHooks.OnTriggerStay;
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine;
namespace Unity.VisualScripting
{
[UnitCategory("Events/Physics")]
public abstract class TriggerEventUnit : GameObjectEventUnit<Collider>
{
/// <summary>
/// The other collider involved in the collision.
/// </summary>
[DoNotSerialize]
public ValueOutput collider { get; private set; }
protected override void Definition()
{
base.Definition();
collider = ValueOutput<Collider>(nameof(collider));
}
protected override void AssignArguments(Flow flow, Collider other)
{
flow.SetValue(collider, other);
}
}
}

View File

@@ -0,0 +1,57 @@
using UnityEngine;
namespace Unity.VisualScripting
{
[UnitCategory("Events/Physics 2D")]
public abstract class CollisionEvent2DUnit : GameObjectEventUnit<Collision2D>
{
/// <summary>
/// The collider we hit.
/// </summary>
[DoNotSerialize]
public ValueOutput collider { get; private set; }
/// <summary>
/// The contact points generated by the physics engine.
/// </summary>
[DoNotSerialize]
public ValueOutput contacts { get; private set; }
/// <summary>
/// The relative linear velocity of the two colliding objects.
/// </summary>
[DoNotSerialize]
public ValueOutput relativeVelocity { get; private set; }
/// <summary>
/// Whether the collision was enabled or not.
/// </summary>
[DoNotSerialize]
public ValueOutput enabled { get; private set; }
/// <summary>
/// The complete collision data object.
/// </summary>
[DoNotSerialize]
public ValueOutput data { get; private set; }
protected override void Definition()
{
base.Definition();
collider = ValueOutput<Collider2D>(nameof(collider));
contacts = ValueOutput<ContactPoint2D[]>(nameof(contacts));
relativeVelocity = ValueOutput<Vector2>(nameof(relativeVelocity));
enabled = ValueOutput<bool>(nameof(enabled));
data = ValueOutput<Collision2D>(nameof(data));
}
protected override void AssignArguments(Flow flow, Collision2D collisionData)
{
flow.SetValue(collider, collisionData.collider);
flow.SetValue(contacts, collisionData.contacts);
flow.SetValue(relativeVelocity, collisionData.relativeVelocity);
flow.SetValue(enabled, collisionData.enabled);
flow.SetValue(data, collisionData);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when an incoming collider makes contact with this object's collider.
/// </summary>
public sealed class OnCollisionEnter2D : CollisionEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionEnter2DMessageListener);
protected override string hookName => EventHooks.OnCollisionEnter2D;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a collider on another object stops touching this object's collider.
/// </summary>
public sealed class OnCollisionExit2D : CollisionEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionExit2DMessageListener);
protected override string hookName => EventHooks.OnCollisionExit2D;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called each frame where a collider on another object is touching this object's collider.
/// </summary>
public sealed class OnCollisionStay2D : CollisionEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnCollisionStay2DMessageListener);
protected override string hookName => EventHooks.OnCollisionStay2D;
}
}

View File

@@ -0,0 +1,74 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a joint attached to the same game object broke.
/// </summary>
[UnitCategory("Events/Physics 2D")]
public sealed class OnJointBreak2D : GameObjectEventUnit<Joint2D>
{
public override Type MessageListenerType => typeof(UnityOnJointBreak2DMessageListener);
protected override string hookName => EventHooks.OnJointBreak2D;
/// <summary>
/// The force that needs to be applied for the joint that broke to break.
/// </summary>
[DoNotSerialize]
public ValueOutput breakForce { get; private set; }
/// <summary>
/// The torque that needs to be applied for the joint that broke to break.
/// </summary>
[DoNotSerialize]
public ValueOutput breakTorque { get; private set; }
/// <summary>
/// The 2D rigidbody to which the other end of the joint is attached (ie, the object without the joint component).
/// </summary>
[DoNotSerialize]
public ValueOutput connectedBody { get; private set; }
/// <summary>
/// The reaction force of the joint that broke.
/// </summary>
[DoNotSerialize]
public ValueOutput reactionForce { get; private set; }
/// <summary>
/// The reaction torque of the joint that broke.
/// </summary>
[DoNotSerialize]
public ValueOutput reactionTorque { get; private set; }
/// <summary>
/// The joint that broke.
/// </summary>
[DoNotSerialize]
public ValueOutput joint { get; private set; }
protected override void Definition()
{
base.Definition();
breakForce = ValueOutput<float>(nameof(breakForce));
breakTorque = ValueOutput<float>(nameof(breakTorque));
connectedBody = ValueOutput<Rigidbody2D>(nameof(connectedBody));
reactionForce = ValueOutput<Vector2>(nameof(reactionForce));
reactionTorque = ValueOutput<float>(nameof(reactionTorque));
joint = ValueOutput<Joint2D>(nameof(joint));
}
protected override void AssignArguments(Flow flow, Joint2D joint)
{
flow.SetValue(breakForce, joint.breakForce);
flow.SetValue(breakTorque, joint.breakTorque);
flow.SetValue(connectedBody, joint.connectedBody);
flow.SetValue(reactionForce, joint.reactionForce);
flow.SetValue(reactionTorque, joint.reactionTorque);
flow.SetValue(this.joint, joint);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a collider enters the trigger.
/// </summary>
public sealed class OnTriggerEnter2D : TriggerEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerEnter2DMessageListener);
protected override string hookName => EventHooks.OnTriggerEnter2D;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a collider exits the trigger.
/// </summary>
public sealed class OnTriggerExit2D : TriggerEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerExit2DMessageListener);
protected override string hookName => EventHooks.OnTriggerExit2D;
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called once per frame for every collider that is touching the trigger.
/// </summary>
public sealed class OnTriggerStay2D : TriggerEvent2DUnit
{
public override Type MessageListenerType => typeof(UnityOnTriggerStay2DMessageListener);
protected override string hookName => EventHooks.OnTriggerStay2D;
}
}

View File

@@ -0,0 +1,26 @@
using UnityEngine;
namespace Unity.VisualScripting
{
[UnitCategory("Events/Physics 2D")]
public abstract class TriggerEvent2DUnit : GameObjectEventUnit<Collider2D>
{
/// <summary>
/// The other collider involved in the collision.
/// </summary>
[DoNotSerialize]
public ValueOutput collider { get; private set; }
protected override void Definition()
{
base.Definition();
collider = ValueOutput<Collider2D>(nameof(collider));
}
protected override void AssignArguments(Flow flow, Collider2D other)
{
flow.SetValue(collider, other);
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the renderer is no longer visible by any camera.
/// </summary>
[UnitCategory("Events/Rendering")]
public sealed class OnBecameInvisible : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnBecameInvisibleMessageListener);
protected override string hookName => EventHooks.OnBecameInvisible;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when the renderer became visible by any camera.
/// </summary>
[UnitCategory("Events/Rendering")]
public sealed class OnBecameVisible : GameObjectEventUnit<EmptyEventArgs>
{
public override Type MessageListenerType => typeof(UnityOnBecameVisibleMessageListener);
protected override string hookName => EventHooks.OnBecameVisible;
}
}

View File

@@ -0,0 +1,82 @@
using System;
using UnityEngine;
namespace Unity.VisualScripting
{
/// <summary>
/// Called when a specified number of seconds has elapsed.
/// </summary>
[UnitCategory("Events/Time")]
[Obsolete("Use Wait For Seconds or Timer instead.")]
public sealed class OnTimerElapsed : MachineEventUnit<EmptyEventArgs>
{
public new class Data : EventUnit<EmptyEventArgs>.Data
{
public float time;
public bool triggered;
}
public override IGraphElementData CreateData()
{
return new Data();
}
protected override string hookName => EventHooks.Update;
/// <summary>
/// The number of seconds to await.
/// </summary>
[DoNotSerialize]
[PortLabel("Delay")]
public ValueInput seconds { get; private set; }
/// <summary>
/// Whether to ignore the time scale.
/// </summary>
[DoNotSerialize]
[PortLabel("Unscaled")]
public ValueInput unscaledTime { get; private set; }
protected override void Definition()
{
base.Definition();
seconds = ValueInput(nameof(seconds), 0f);
unscaledTime = ValueInput(nameof(unscaledTime), false);
}
public override void StartListening(GraphStack stack)
{
base.StartListening(stack);
var data = stack.GetElementData<Data>(this);
data.triggered = false;
data.time = 0;
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var data = flow.stack.GetElementData<Data>(this);
if (data.triggered)
{
return false;
}
var increment = flow.GetValue<bool>(unscaledTime) ? Time.unscaledDeltaTime : Time.deltaTime;
var threshold = flow.GetValue<float>(seconds);
data.time += increment;
if (data.time >= threshold)
{
data.triggered = true;
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,95 @@
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;
}
}
}