React Unity WebGL provides a modern solution for embedding Unity WebGL builds in your React Application while providing advanced APIs for two way communication and interaction between Unity and React.
It appears that this library doesn't work with static Unity functions.
I wanted to call a Unity function from React, and then sends Unity data back to React. With static functions, this doesn't work, and so I had to refactor my Unity class to use regular methods. Not sure if this is expected behavior, but the error message was confusing.
Using the sample code attached to this bug report, the console will log:
SendMessage: object ReactManager does not have receiver for function RetrieveGameObjects!
Action: Maybe update the error message to suggest static methods are not supported.
Reproducible test case
ListNodes.cs
using UnityEngine;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Utility;
namespace DeepLynx
{
public class ListNodes : MonoBehaviour
{
// This script is attached to an empty game object called ReactManager
[DllImport("__Internal")]
private static extern void SendGameObjects(string response);
public static void RetrieveGameObjects()
{
Debug.Log("Retrieving GameObjects");
string response = ListGameObjects();
Debug.Log(response); // "Object1, Object2, Object3"
#if UNITY_WEBGL == true && UNITY_EDITOR == false
SendGameObjects(response);
#endif
}
public static string ListGameObjects()
{
// This is a custom function that returns a list of game objects
List<GameObject> gameObjects = CustomTags.GetGameObjects("SpecificTag");
List<string> nodes = new List<string>();
foreach (GameObject go in gameObjects)
{
nodes.Add(go.name);
}
return string.Join(",", nodes);
}
}
}
Please avoid duplicates
Language and Compiler
Babel and WebPack JavaScript
What environment are you using?
Local Development Server
When does your problem occur?
When the Unity App is running
What does your problem relate to?
The problem seems Unity related
React-Unity-WebGL Version
^9.4.0
React Version
^18.1.0
Unity Version
2021.3.5f1
What happened?
It appears that this library doesn't work with static Unity functions.
I wanted to call a Unity function from React, and then sends Unity data back to React. With static functions, this doesn't work, and so I had to refactor my Unity class to use regular methods. Not sure if this is expected behavior, but the error message was confusing.
Using the sample code attached to this bug report, the console will log:
Action: Maybe update the error message to suggest static methods are not supported.
Reproducible test case
ListNodes.cs
/Plugins/WebGL/React.jslib
Component.js
Would you be interested in contributing a fix?