Code Monkey home page Code Monkey logo

flutter_unity_widget_web's Issues

Unity not reacting to Flutter-Messages if you don't tap on UnityView

Hey it's me again ❤️

I created a cross-platform solution in: https://github.com/Glartek/flutter-unity/pull/55

It's working perfectly for all platforms and thank you again for your web-implementation ❤️

But I recognized that sometimes Unity is not reacting if I send messages to Unity.

I solved this with this: https://github.com/Glartek/flutter-unity/pull/55/commits/0237f6f386cf289b19d1777fdb08ad0e4f7d8717

Maybe you can apply this fix also for this package :)

ArgumentException: JSON must represent an object type.

Thank you so much for your package.

I've inserted everything like this:

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

      var container = document.querySelector("#unity-container");
      var canvas = document.querySelector("#unity-canvas");
      var loadingBar = document.querySelector("#unity-loading-bar");
      var progressBarFull = document.querySelector("#unity-progress-bar-full");
      var fullscreenButton = document.querySelector("#unity-fullscreen-button");
      var mobileWarning = document.querySelector("#unity-mobile-warning");

      // By default Unity keeps WebGL canvas render target size matched with
      // the DOM size of the canvas element (scaled by window.devicePixelRatio)
      // Set this to false if you want to decouple this synchronization from
      // happening inside the engine, and you would instead like to size up
      // the canvas DOM size and WebGL render target sizes yourself.
      // config.matchWebGLToCanvasSize = false;

      if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
        container.className = "unity-mobile";
        // Avoid draining fillrate performance on mobile devices,
        // and default/override low DPI mode on mobile browsers.
        config.devicePixelRatio = 1;
        mobileWarning.style.display = "block";
        setTimeout(() => {
          mobileWarning.style.display = "none";
        }, 5000);
      } else {
        canvas.style.width = "960px";
        canvas.style.height = "600px";
      }
      loadingBar.style.display = "block";

      var script = document.createElement("script");
      script.src = loaderUrl;
      script.onload = () => {
        createUnityInstance(canvas, config, (progress) => {
          progressBarFull.style.width = 100 * progress + "%";
        })
          .then((unityInstance) => {
            window.parent.postMessage("unity_loaded", "*");
            globalUnityInstance = unityInstance;
            loadingBar.style.display = "none";
            fullscreenButton.onclick = () => {
              unityInstance.SetFullscreen(1);
            };
          })
          .catch((message) => {
            alert(message);
          });
      };

And this is my dart implementation:

if (kIsWeb) {
      return UnityWebWidget(
        url: 'http://localhost:${Uri.base.port}/unity/index.html',
        listenMessageFromUnity: onUnityMessage,
        onUnityLoaded: (webController) => onUnityCreated(
          unityWebController: webController,
        ),
      );
    }

And this is not working:

if (kIsWeb) {
      webController!.sendDataToUnity(
        gameObject: 'ComposerInterface',
        method: 'ToggleBetweenInsideAndOutside',
        data: "$isViewingFromOutside",
      );
      return;
    }

I get: ArgumentException: JSON must represent an object type.

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.