docs.unity3d.com
  • Manual
  • Scripting API
  • Changelog
  • License
    Show / Hide Table of Contents
    • About Visual Scripting
      • Configure project settings
        • Add or remove available nodes
        • Add or remove types
        • Create or restore a backup
      • Choose a control scheme
      • Configure your preferences
      • Update Visual Scripting
      • Version control systems
      • Use Visual Scripting with Unity Cloud Build
    • Basic concepts in Visual Scripting
      • The interface
      • Nodes
      • Graphs
        • Subgraphs and State Units
        • Transitions
      • Script Machines and State Machines
      • Object types
        • Custom types
      • Variables
    • Develop application logic with Script Graphs
      • Create a new graph file
        • Create a new blank graph with the Project window
        • Create a new unassigned graph with the empty graph creation flow
        • Create and assign a graph to an existing Game​Object
        • Create and assign a graph to a new Game​Object
        • Create a graph on a Script Machine or State Machine
      • Attach a graph file to a Script Machine or State Machine
      • Open a graph file
        • Add a node to a Script Graph
        • Connect nodes in a Script Graph
        • Create and add a variable to a Script Graph
        • Create node groups
      • Add a Subgraph to a Script Graph
        • Add a Trigger or Data port to a Script Graph
      • Add a State Unit to a Script Graph
      • Custom Events
        • Add a Custom Event node
        • Add a Trigger Custom Event node
      • Capture user input in an application
        • Capture input using the Input Manager
        • Add and configure a Player Input component
        • Capture input using the Input System package
      • Use relations to debug
        • Predictive and live debugging
        • Working with debug messages
      • Live edit
        • Live edit during runtime
    • Develop logic transitions with state graphs
      • Create a new state
      • Create a transition between states
    • Advanced customization and development
      • Refactor a C# script with Visual Scripting
        • Add the Renamed​From attribute to a C# script
      • Custom C# nodes
        • Create a new simple Custom C# node
        • Add ports to your Custom C# node
        • Add logic to your Custom C# node
        • Add relations to your Custom C# node
        • Add documentation to your Custom C# node
        • Custom C# node attributes reference
      • Create a Custom Scripting Event node
        • Create a Custom Scripting Event Sender node
        • Trigger a Custom Scripting Event from a C# script
        • Listen to a Custom Scripting Event from a C# script
      • Use a custom type
        • Add the Inspectable attribute to a custom type
        • Create a custom Property​Drawer for a custom type
    • Node reference
      • This node
      • Control node
      • Time node
      • Events
        • Event nodes
        • Input Event nodes
          • On Input System Event Button
          • On Input System Event Float
          • On Input System Event Vector 2
          • On Button Input
          • On Keyboard Input
          • On Mouse Down
          • On Mouse Drag
          • On Mouse Enter
          • On Mouse Exit
          • On Mouse Input
          • On Mouse Over
          • On Mouse Up As Button
          • On Mouse Up
      • Variable node
      • Nulls node
      • Formula node
      • Nesting
        • Input node
        • Output node
        • State Unit node
        • Subgraph node
      • Script graph nodes
      • State graph nodes
    • Developer's guide
    • Manual
    • Advanced customization and development
    • Create a Custom Scripting Event node
    • Create a Custom Scripting Event Sender node

    Create a Custom Scripting Event Sender node

    Note

    Before you create a Custom Scripting Event Sender node, you must create a Custom Scripting Event node. The examples below are based on the previous example to create a Custom Scripting Event node. For more information, see Create a Custom Scripting Event node.

    After you create a Custom Scripting Event node, you can create a Custom Scripting Event Sender node to trigger the Event from any other Script Graph in the same scene, or the same Script Graph.

    You can also choose to create a separate script to trigger the Event from code. For more information, see Trigger a Custom Scripting Event from a C# script.

    Create a node and add it to the fuzzy finder

    To create a Custom Scripting Event Sender node and add it to the fuzzy finder:

    1. Go to Window > General > Project, or press Ctrl+5 (macOS: Cmd+5) to open the Project window.

    2. Right-click a folder in the Project window's folder list or anywhere in the Project window's preview pane.

    3. Go to Create > C# Script.

    4. Enter a name, such as SendMyEventNode, for the new script file.

    5. Press Enter.

    6. Double-click the new C# file. Unity opens the file in the program you specified in your preferences, under External Script Editor.

      Note

      For more information on script editors in Unity, see the Integrated development environment (IDE) support in the Unity User Manual.

    7. In your external editor, copy and paste the following code into the C# script:

      using Unity.VisualScripting;
      using UnityEngine;
      
      //Custom node to send the Event
      [UnitTitle("Send My Custom Event")]
      [UnitCategory("Events\\MyEvents")]//Setting the path to find the node in the fuzzy finder as Events > My Events.
      public class SendMyEvent : Unit
      {
        [DoNotSerialize]// Mandatory attribute, to make sure we don’t serialize data that should never be serialized.
        [PortLabelHidden]// Hide the port label, as we normally hide the label for default Input and Output triggers.
        public ControlInput inputTrigger { get; private set; }
        [DoNotSerialize]
        public ValueInput myValue;
        [DoNotSerialize]
        [PortLabelHidden]// Hide the port label, as we normally hide the label for default Input and Output triggers.
        public ControlOutput outputTrigger { get; private set; }
      
        protected override void Definition()
        {
      
            inputTrigger = ControlInput(nameof(inputTrigger), Trigger);
            myValue = ValueInput<int>(nameof(myValue),1);
            outputTrigger = ControlOutput(nameof(outputTrigger));
            Succession(inputTrigger, outputTrigger);
        }
      
        //Send the Event MyCustomEvent with the integer value from the ValueInput port myValueA.
        private ControlOutput Trigger(Flow flow)
        {
            EventBus.Trigger(EventNames.MyCustomEvent, flow.GetValue<int>(myValue));
            return outputTrigger;
        }
      }
      
    8. Save your script file.

    9. Return to the Unity Editor.

    10. Follow the process described in Configure project settings to regenerate your Node Library.

    After you regenerate your Node Library, the Custom Scripting Event Sender node appears in the fuzzy finder. If you didn't change the [UnitCategory] or [UnitTitle] from the sample code, then the fuzzy finder displays the node under Events > MyEvents, as the Send My Custom Event node. For more information on the fuzzy finder, see The interface.

    Trigger your Custom Scripting Event node

    You might use your Send My Custom Event node to trigger your Event based on keyboard input:

    1. Open a Script Graph where you want to add the new node. This can be the same or a different Script Graph from the one that contains your Custom Scripting Event node.

    2. Right-click anywhere in the Graph Editor to open the fuzzy finder..

    3. Go to Events > Input.

    4. Select the On Keyboard Input node to add it to the graph.

    5. Right-click again in the Graph Editor to open the fuzzy finder.

    6. Go to Events > My Events.

    7. Select your Send My Custom Event node to add it to the graph.

    8. Connect the On Keyboard Input node's Trigger output port to the Send My Custom Event node's Input Trigger input port, as shown in the following image.

      An image of the Graph Editor. An On Keyboard Input node with its Key set to Space and its Action set to Up connects to the Send My Custom Event node.

    9. Select Play from the Unity Editor's Toolbar to enter Play mode.

    10. Press and release the Spacebar in the Game view.

    The Custom Scripting Event Sender node triggers the Custom Scripting Event in your graph and sends the Event the value from My Value A.

    Next steps

    After you create a Custom Scripting Event Sender node, you can create a script to trigger your Event from code or create a script to listen to your Event.

    In This Article
    • Create a node and add it to the fuzzy finder
    • Trigger your Custom Scripting Event node
    • Next steps
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023