Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samuel310/flutter_unity_widget_web
Flutter unity widget for embedding unity in flutter web apps.
https://github.com/samuel310/flutter_unity_widget_web
flutter flutterweb unity unity3d web widget
Last synced: about 1 month ago
JSON representation
Flutter unity widget for embedding unity in flutter web apps.
- Host: GitHub
- URL: https://github.com/samuel310/flutter_unity_widget_web
- Owner: Samuel310
- License: bsd-3-clause
- Created: 2022-03-14T07:49:55.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-25T06:32:22.000Z (over 2 years ago)
- Last Synced: 2024-09-27T17:01:39.148Z (about 2 months ago)
- Topics: flutter, flutterweb, unity, unity3d, web, widget
- Language: Dart
- Homepage:
- Size: 477 KB
- Stars: 6
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
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.
## Setup
1. Place the exported unity webGL build folder inside web folder.
![Web folder structure](https://github.com/Samuel310/flutter_unity_widget_web/blob/main/assets/fs.png?raw=true "Location to place exported unity web folder")
2. Modify the index.html file inside unity webGL build folder.
- Add following script in index.html.
```javascript
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.
![index.html file image](https://github.com/Samuel310/flutter_unity_widget_web/blob/main/assets/index1.png?raw=true "index.html will look like this after adding the above script")
- Add following script in index.html.
```javascript
window.parent.postMessage("unity_loaded", "*");
globalUnityInstance = unityInstance;
```index.html file after adding the above script.
![index.html file image](https://github.com/Samuel310/flutter_unity_widget_web/blob/main/assets/index2.png?raw=true "index.html will look like this after adding the above script")
## Usage
```dart
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 createState() => _UnityScreenState();
}class _UnityScreenState extends State {
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(() {});
}
}```
## API
- `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)## Features and bugs
Please file feature requests and bugs at the [issue tracker][tracker].
[tracker]: https://github.com/Samuel310/flutter_unity_widget_web/issues