Flutter unity widget for embedding unity in flutter web apps. Now you can render unity webGL build in a Flutter web app. Also enables two way communication between flutter web and unity.
- Place the exported unity webGL build folder inside web folder.
-
Modify the index.html file inside unity webGL build folder.
-
Add following script in index.html.
let globalUnityInstance; window["receiveMessageFromUnity"] = function (params) { window.parent.postMessage(params, "*"); }; window.parent.addEventListener("flutter2js", function (params) { const obj = JSON.parse(params.data); globalUnityInstance.SendMessage(obj.gameObject, obj.method, obj.data); });
index.html file after adding the above script.
-
Add following script in index.html.
window.parent.postMessage("unity_loaded", "*"); globalUnityInstance = unityInstance;
index.html file after adding the above script.
-
import 'package:flutter/material.dart';
import 'package:flutter_unity_widget_web/flutter_unity_widget_web.dart';
class UnityScreen extends StatefulWidget {
const UnityScreen({Key? key}) : super(key: key);
@override
State<UnityScreen> createState() => _UnityScreenState();
}
class _UnityScreenState extends State<UnityScreen> {
late UnityWebController _unityWebController;
@override
Widget build(BuildContext context) {
return UnityWebWidget(
url: 'http://localhost:${Uri.base.port}/unity/index.html',
listenMessageFromUnity: _listenMessageFromUnity,
onUnityLoaded: _onUnityLoaded,
);
}
@override
void dispose() {
_unityWebController.dispose();
super.dispose();
}
void _listenMessageFromUnity(String data) {
if (data == 'load_next_scene') { // any message emitted from unty.
_unityWebController.sendDataToUnity(
gameObject: 'GameWindow',
method: 'LoadNextScene',
data: '0', // data sent to unity from flutter web.
);
}
}
void _onUnityLoaded(UnityWebController controller) {
_unityWebController = controller;
setState(() {});
}
}
onUnityLoaded(UnityWebController controller)
(Unity to flutter web binding and listener when unity is loaded)listenMessageFromUnity(String data)
(Listen for message sent from unity to flutter web)sendDataToUnity(String gameObject, String method, String data)
(Allows you to send date from flutter web to untiy)
Please file feature requests and bugs at the issue tracker.