{"id":13550955,"url":"https://github.com/Samuel310/flutter_unity_widget_web","last_synced_at":"2025-04-03T01:30:43.537Z","repository":{"id":56830430,"uuid":"469630433","full_name":"Samuel310/flutter_unity_widget_web","owner":"Samuel310","description":"Flutter unity widget for embedding unity in flutter web apps.","archived":false,"fork":false,"pushed_at":"2022-05-25T06:32:22.000Z","size":488,"stargazers_count":6,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T12:11:17.382Z","etag":null,"topics":["flutter","flutterweb","unity","unity3d","web","widget"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Samuel310.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-14T07:49:55.000Z","updated_at":"2024-05-19T06:56:45.000Z","dependencies_parsed_at":"2022-09-02T03:51:39.268Z","dependency_job_id":null,"html_url":"https://github.com/Samuel310/flutter_unity_widget_web","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Samuel310%2Fflutter_unity_widget_web","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Samuel310%2Fflutter_unity_widget_web/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Samuel310%2Fflutter_unity_widget_web/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Samuel310%2Fflutter_unity_widget_web/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Samuel310","download_url":"https://codeload.github.com/Samuel310/flutter_unity_widget_web/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246921957,"owners_count":20855339,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flutter","flutterweb","unity","unity3d","web","widget"],"created_at":"2024-08-01T12:01:40.265Z","updated_at":"2025-04-03T01:30:42.673Z","avatar_url":"https://github.com/Samuel310.png","language":"Dart","readme":"\u003c!--\nThis README describes the package. If you publish this package to pub.dev,\nthis README's contents appear on the landing page for your package.\n\nFor information about how to write a good package README, see the guide for\n[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).\n\nFor general information about developing packages, see the Dart guide for\n[creating packages](https://dart.dev/guides/libraries/create-library-packages)\nand the Flutter guide for\n[developing packages and plugins](https://flutter.dev/developing-packages).\n--\u003e\n\nFlutter 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.\n\n## Setup\n\n1. Place the exported unity webGL build folder inside web folder.\n\n![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\")\n\n2. Modify the index.html file inside unity webGL build folder.\n\n   - Add following script in index.html.\n\n     ```javascript\n     let globalUnityInstance;\n\n     window[\"receiveMessageFromUnity\"] = function (params) {\n       window.parent.postMessage(params, \"*\");\n     };\n\n     window.parent.addEventListener(\"flutter2js\", function (params) {\n       const obj = JSON.parse(params.data);\n       globalUnityInstance.SendMessage(obj.gameObject, obj.method, obj.data);\n     });\n     ```\n\n     index.html file after adding the above script.\n\n     ![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\")\n\n   - Add following script in index.html.\n\n     ```javascript\n     window.parent.postMessage(\"unity_loaded\", \"*\");\n     globalUnityInstance = unityInstance;\n     ```\n\n     index.html file after adding the above script.\n\n     ![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\")\n\n## Usage\n\n```dart\nimport 'package:flutter/material.dart';\nimport 'package:flutter_unity_widget_web/flutter_unity_widget_web.dart';\n\nclass UnityScreen extends StatefulWidget {\n  const UnityScreen({Key? key}) : super(key: key);\n\n  @override\n  State\u003cUnityScreen\u003e createState() =\u003e _UnityScreenState();\n}\n\nclass _UnityScreenState extends State\u003cUnityScreen\u003e {\n  late UnityWebController _unityWebController;\n\n  @override\n  Widget build(BuildContext context) {\n    return UnityWebWidget(\n      url: 'http://localhost:${Uri.base.port}/unity/index.html',\n      listenMessageFromUnity: _listenMessageFromUnity,\n      onUnityLoaded: _onUnityLoaded,\n    );\n  }\n\n  @override\n  void dispose() {\n    _unityWebController.dispose();\n    super.dispose();\n  }\n\n  void _listenMessageFromUnity(String data) {\n    if (data == 'load_next_scene') { // any message emitted from unty.\n      _unityWebController.sendDataToUnity(\n        gameObject: 'GameWindow',\n        method: 'LoadNextScene',\n        data: '0', // data sent to unity from flutter web.\n      );\n    }\n  }\n\n  void _onUnityLoaded(UnityWebController controller) {\n    _unityWebController = controller;\n    setState(() {});\n  }\n}\n\n```\n\n## API\n\n- `onUnityLoaded(UnityWebController controller)` (Unity to flutter web binding and listener when unity is loaded)\n- `listenMessageFromUnity(String data)` (Listen for message sent from unity to flutter web)\n- `sendDataToUnity(String gameObject, String method, String data)` (Allows you to send date from flutter web to untiy)\n\n## Features and bugs\n\nPlease file feature requests and bugs at the [issue tracker][tracker].\n\n[tracker]: https://github.com/Samuel310/flutter_unity_widget_web/issues\n","funding_links":[],"categories":["Dart"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSamuel310%2Fflutter_unity_widget_web","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSamuel310%2Fflutter_unity_widget_web","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSamuel310%2Fflutter_unity_widget_web/lists"}