docs.unity3d.com
    Show / Hide Table of Contents

    Add logic to a Custom C# node

    Note

    To add logic to a node, you must create a Custom C# node and add ports. The examples below are based on the previous examples for a Custom C# node. For more information, see Create a new simple Custom C# node.

    After you create a Custom C# node and add ports, you can add logic to a node. Add logic to tell Visual Scripting what the node does with any data it receives from its ports.

    To add logic to a node:

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

    2. Double-click the 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.

    3. In your external editor, add any logic for the node within the lambda expression that handles the assignment of the inputTrigger. For example, you can take the values of the two string input ports added in the previous example and concatenate them, as shown in the following code:

      using System;
      using Unity.VisualScripting;
      using UnityEngine;
      
      public class MyNode : Unit
      {
      [DoNotSerialize]
      public ControlInput inputTrigger;
      
      [DoNotSerialize]
      public ControlOutput outputTrigger;
      
      [DoNotSerialize]
      public ValueInput myValueA;
      
      [DoNotSerialize]
      public ValueInput myValueB;
      
      [DoNotSerialize]
      public ValueOutput result;
      
      private string resultValue;
      protected override void Definition()
      {
          //The lambda to execute our node action when the inputTrigger port is triggered.
          inputTrigger = ControlInput("inputTrigger", (flow) =>
          {
              //Making the resultValue equal to the input value from myValueA concatenating it with myValueB.
              resultValue = flow.GetValue<string>(myValueA) + flow.GetValue<string>(myValueB) + "!!!";
              return outputTrigger;
          });
          outputTrigger = ControlOutput("outputTrigger");
      
          myValueA = ValueInput<string>("myValueA", "Hello ");
          myValueB = ValueInput<string>("myValueB", String.Empty);
          result = ValueOutput<string>("result", (flow) => resultValue);
      }
      }
      
    4. Save your script file.

    5. Return to the Unity Editor.

    6. Do one of the following:

      • Open a Script Graph where you've already added your node..
      • Right-click anywhere in the Graph Editor to open the fuzzy finder. Then, select your node in the fuzzy finder to add it to your graph.

    Next steps

    After you add logic to a node, add relations to ensure that the node displays correctly in Visual Scripting.

    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