sendDataToUnity method

void sendDataToUnity({
  1. required String gameObject,
  2. required String method,
  3. required String data,
})

Post message to unity from flutter. This method takes in a string data. The gameObject must match the name of an actual unity game object in a scene at runtime, and the methodName, must exist in a MonoDevelop class and also exposed as a method. message is an parameter taken by the method

sendDataToUnity("GameManager", "openScene", "ThirdScene")

Implementation

void sendDataToUnity({
  required String gameObject,
  required String method,
  required String data,
}) {
  _flutter2js = html.MessageEvent(
    'flutter2js',
    data: json.encode({
      "gameObject": gameObject,
      "method": method,
      "data": data,
    }),
  );
  html.window.dispatchEvent(_flutter2js);
  refreshUnityView();
}