Code Monkey home page Code Monkey logo

flutter_unity_widget_web's Introduction

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

  1. 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.

      index.html file image

    • Add following script in index.html.

      window.parent.postMessage("unity_loaded", "*");
      globalUnityInstance = unityInstance;

      index.html file after adding the above script.

      index.html file image

Usage

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(() {});
  }
}

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.

flutter_unity_widget_web's People

Contributors

samuel310 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.