You are here:

Most of data sharing is done in LightAct GameObject

In the object’s properties on the right, you’ll see 3 properties:

  • Memory Writing Handle is the name of the shared memory handle Unity is writing to. You need to type the exact same text into JSON memory reader node in LightAct’s Devices window.
  • Memory Reading Handle is the name of the shared memory handle Unity is reading from. Please note, you need to type in the exact same text as the one you typed into JSON memory writer node in LightAct’s Devices window.
  • Memory Size is the amount of memory space (in bytes) Unity reserves for itself (for writing). 1024 should be, for most intents and purposes, more than enough.

Receiving Data from LightAct in Unity

As you probably noticed by now (and as we mentioned before), the floating yellow text above the ‘screen plane’ shows text that is sent from LightAct. Also, the red cube is rotating based on a float value coming from LightAct.

In order to explain that we’ll need to go through the C# LightactPlugin script. We’ll go just through the most essential aspects, for all the detailed information please refer to the script itself where we’ve put some additional explanatory comments.

    //values coming from LightAct and then shared with GameObjects
    public static float valRotation;
    public static string valText;

In the beginning of the script around line 15, we declare 2 public variables.

  • valRotation is a float variable determining the rotation of the red cube
  • valText is a string variable determining the content of the yellow text object.
 //Reading from shared memory
        if(BuildMap(m_memoryReadingHandle, m_memorySize))
        {
            Debug.Log("Reading from shared memory (handle name: "+ m_memoryReadingHandle+") succesful");
        }
        else
            Debug.Log("Reading from shared memory (handle name: " + m_memoryReadingHandle + ") failed");

In the Update function, around line 136 we call BuildMap function. It is a function in the native LightAct plugin, which you are free to use & modify (source code is available as part of LightAct4Unity plugin).

That function sends the data from the shared memory back to BuildMapData function in our C# script.

 void BuildMapData(string keys, string values)
    {
        Debug.Log("Incoming Keys: " + keys);
        Debug.Log("Incoming Values: " + values);

        // let's do some string splitting
        String[] keysArray = keys.Split(','); // comma is our delimiter.
        String[] valuesArray = values.Split(',');
        // you can then transform these values into proper formats (ints, floats etc.) and use them elsewhere in your project.

        float v = 0;

        if (valuesArray.Length > 0 && float.TryParse(valuesArray[0], out v))
        {
            valRotation = v;
        }
        if(valuesArray.Length > 1)
        {
            valText = valuesArray[1];
        }

    }

As you can see the data comes as a string of comma-delimited keys and a string of comma-delimited values.

It is up to you then, to parse these 2 comma-delimited strings the way you want and to write the results into the correct variables or push them to other GameObjects in your project.

In our case, we’ve just saved the values to valRotation and valText public variables.

These variables are then read by Text and Red cube GameObjects with their respective scripts.

Sending data from Unity to LightAct

//values to be sent to LightAct
    public static float float2LA=0.0f;
    public static string string2LA="";

Around line 20 we declare 2 public variables: float2LA and string2LA. Both are set every frame by Blue sphere GameObject and its respective script SphereScript.

void Update()
    {
        transform.position = new Vector3(transform.position.x,Mathf.PingPong(Time.time * 2, max - min) + min, transform.position.z);
        LightactPlugin.float2LA = transform.position.y;
        LightactPlugin.string2LA = "Hello from that blue sphere in Unity";
    }

Update function in SphereScript script. It writes the height of the blue sphere to float2LA variable and some arbitrary text to string2LA.

Let’s have a look now how this is then sent to LightAct. For that we have to go back to LightactPlugin script.

if (createMemHandle(m_memoryWritingHandle, m_memorySize))
        {
            Debug.Log("Opened Shared Memory Write Handle");
        }
        else
        {
            Debug.Log("Error in opening Memory write Handle");
        }

First, we open the shared memory handle in the Start function (around line 104).

//Writing to shared memory. You can change the handle name and size in Unity editor
        if (writeSharedMemory("sphereHeight,sphereText", float2LA.ToString("F", CultureInfo.InvariantCulture) + "," + string2LA, m_memoryWritingHandle, m_memorySize))
        {
        }
        else
        {
            Debug.Log("Shared memory Writing failed");
        }

Then, in the Update function, we call writeSharedMemory function where, in the function’s arguments, we simply concatenate all the keys and values into 2 comma-delimited strings together with shared memory handle and shared memory size.

Actions OnDestroy()

void OnDestroy()
    {
        if(closeMemHandle(m_memoryWritingHandle))
        {
            Debug.Log("Closed Shared Memory Write Handle");
        }
        else
        {
            Debug.Log("Error in Closing Memory Write Handle");
        }

        if (closeMemHandle(m_memoryReadingHandle))
        {
            Debug.Log("Closed Shared Memory Read Handle");
        }
        else
        {
            Debug.Log("Error in Closing Memory Read Handle");
        }
        Debug.Log("Destroy");
    }

It is very important, that we close both memory handles (especially the writing one) when we exit the game. Memory Reading Handle (from Unity’s perspective) should be destroyed by LightAct when it exits, but you shouldn’t forget to call closeMemHandle on the Memory Writing Handle (again, from Unity’s perspective).