NEP+ Docs
  • NEP+ middleware documentation
  • Developer Tools
    • NEP CLI
  • User interfaces
    • Install NEP+ App (Windows)
    • Install NEP+ App (macOS)
    • NEP+ App (0.0.4) - Deprecated
  • Concepts for developers
    • Message Format
    • Tutorials for developers
      • Connecting Multiple Scripts on the Same Computer using NEP+ Tools: A Step-by-Step Tutorial
      • Connecting 2 scripts in a local network (e.g., two computers on the same Wifi network)
      • Connecting ROS to non-ROS nodes using NEP+ tools
  • Programming enviroments
    • Python
    • C# (Unity)
    • C# (Visual Studio)
    • Javascript (Node.js)
    • Java/Kotlin (Android Studio)
    • Java (Netbeans)
    • C++ (Visual Studio)
  • Other tutorials
    • Pepper & NAO robots
      • Using Pepper without Pepper in Choregraphe
      • Install Pepper/NAO SDK
  • Code examples
    • Sending images from Python and Opencv
Powered by GitBook
On this page
  • Installation:
  • Sample code for sending and reading cube 3D position
  • Solving issues with Newtonsoft.Json
  1. Programming enviroments

C# (Unity)

How to use NEP in Unity?

Version
Link
Nep master port

0.0.0.8

7000

0.0.0.9 (recomended)

50000

Installation:

  1. Download the folder containing the NepUnity library from the links above.

  2. Unzip the folder to extract its contents.

  3. Open your Unity project.

  4. Locate the "Assets" folder in your project's directory in the Unity Editor.

  5. Copy and paste the downloaded folder, including all its contents, into your Unity project's "Assets" folder.

  6. Unity will automatically recognize the .dll files within the folder and include them in your project.

After following these steps, you can use the Nep library in your Unity project. You can now reference the library's classes and functions in your C# scripts.

Sample code for sending and reading cube 3D position

using UnityEngine;
using Nep;
using NepGeometryMsgs;
using Newtonsoft.Json;

public class move : MonoBehaviour
{
    private NepPublisher pub;
    private NepNode node;
    private Transform cubeTransform;
    private NepVector3 vector;
    NepVector3 result;


    /// <summary>
    /// Represents the message structure for publishing the cube position.
    /// </summary>
    private class Msg
    {
        public double message { get; set; }
    }

    private NepCallback nepCallback;

    /// <summary>
    /// Callback method to handle incoming messages.
    /// </summary>
    /// <param name="message">The received message.</param>
    private void PrintDataCallback(string message)
    {
        // Log the X, Y, and Z components of the NepVector3 object
        result = JsonConvert.DeserializeObject<NepVector3>(message);
        Debug.Log("X:" + result.x.ToString() + 
        ", Y:" + result.y.ToString() + ", Z:" + result.z.ToString());
    }

    // Start is called before the first frame update
    void Start()
    {
        cubeTransform = GetComponent<Transform>();
        node = new NepNode("basic_pub_cs");
        vector = new NepVector3();
        result = new NepVector3();

        // Define the NepCallback/Subscriber for receiving messages
        nepCallback = node.NewCallback("test", PrintDataCallback);
        nepCallback.Start();

        // Define the Publisher for sending messages
        pub = node.NewPub("test", "json");
    }

    // Update is called once per frame
    void Update()
    {
        // Get the position of the cube
        Vector3 cubePosition = cubeTransform.position;

        // Fill message to send
        vector.x = cubePosition.x;
        vector.y = cubePosition.y;
        vector.z = cubePosition.z;

        // Send the message
        pub.Publish(vector);

        // Update the NepCallback to process received messages
        nepCallback.UpdateMessage();
    }

    private void OnDestroy()
    {
        // Dispose the Publisher and close the Node
        pub.Dispose();
        node.Close();
        
        // Stop the NepCallback
        nepCallback.Stop();
    }
}

Solving issues with Newtonsoft.Json

Some Unity versions will require the installation of Newtonsoft.Json

Select Window -> Package Manager -> Add package from git URL... and then write:

com.unity.nuget.newtonsoft-json

A better approach

using UnityEngine;
using NepGeometryMsgs;
using Nep;


public class NepGlobal : MonoBehaviour
{
    public static NepNode node;    
    public static NepPublisher pubCube;

    private void Awake()
    {
        node = new NepNode("unity_test");
        NepConfiguration conf = node.Hybrid("192.168.1.12");
        pubCube = node.NewPub("cube/Vector3", "json", conf);
    }

    private void Update()
    {
            // Update Callbacks
            node.Update();
    }

    private void OnDestroy()
    {
            node.Close();
            node.Dispose();
    }
}

Attached to a cube

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Nep;
using NepGeometryMsgs;
using Newtonsoft.Json;

public class Example : MonoBehaviour
{
   
    // The game object
    public GameObject cube;
    NepVector3 result;
    NepVector3 vector;

    private Transform cubeTransform;
    private NepCallback nepCallback;
   
    void Start()
    {
        cubeTransform = GetComponent<Transform>();
        nepCallback = NepGlobal.node.NewCallback("test2", PrintDataCallback, "192.168.1.12");
        nepCallback.Start();
        vector = new NepVector3();
    }

    private void PrintDataCallback(string message)
    {
        // Log the X, Y, and Z components of the NepVector3 object
        result = JsonConvert.DeserializeObject<NepVector3>(message);
        Debug.Log("X:" + result.x.ToString() + 
        ", YY:" + result.y.ToString() + ", Z:" + result.z.ToString());
    }

    private void Update()
    {
            Vector3 cubePosition = cubeTransform.position;
            vector.x = cubePosition.x;
            vector.y = cubePosition.y;
            vector.z = cubePosition.z;
            NepGlobal.pubCube.Publish(vector);       
    }
}

Last updated 1 year ago

Download
Download