Download the folder containing the NepUnity library from the links above.
Unzip the folder to extract its contents.
Open your Unity project.
Locate the "Assets" folder in your project's directory in the Unity Editor.
Copy and paste the downloaded folder, including all its contents, into your Unity project's "Assets" folder.
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);
}
}