tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.13k stars 2.53k forks source link

[bug] Loading unity in the app #7135

Open mad-jose opened 1 year ago

mad-jose commented 1 year ago

Describe the bug

I exported a unity app as webgl, added it to the public folder loaded it using iframe

In the browser, it works but in tauri app it does not work. doing build for windows

In tauri app it displays: Unable to parse Build/UnityWebGlTest.framework.js.br! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: br" present. Check browser Console and Devtools Network tab to debug.

Reproduction

exported a unity app as webgl, added it to the public folder loaded it using iframe

https://github.com/maddevjose/tauri-unity-bug yarn run bld

Expected behavior

unity loads

Platform and versions

Windows: 11 22H2
@tauri-apps/cli: 1.3.1

Stack trace

No response

Additional context

in tauri dev it works build is the issue

amrbashir commented 1 year ago

Please provide a minimal reproduction. Thanks.

Why reproduction is required

mad-jose commented 1 year ago

Please provide a minimal reproduction. Thanks.

Why reproduction is required

here is a sample code https://github.com/maddevjose/tauri-unity-bug

dklassic commented 1 year ago

@maddevjose can you try the following and see if it works?

In Unity under Project Settings -> Player -> Publishing Settings

image

afaik this is potentially a limitation of Unity itself

mad-jose commented 1 year ago

@maddevjose can you try the following and see if it works?

In Unity under Project Settings -> Player -> Publishing Settings

  • Try different Compression Format
  • Try enable Decompression Fallback

image

afaik this is potentially a limitation of Unity itself

it still does not work build. It works in yarn tauri dev

mad-jose commented 1 year ago

@maddevjose can you try the following and see if it works? In Unity under Project Settings -> Player -> Publishing Settings

  • Try different Compression Format
  • Try enable Decompression Fallback

image afaik this is potentially a limitation of Unity itself

it still does not work build. It works in yarn tauri dev

i added .setup(move |app| { let resource_path = app.path_resolver().resolve_resource("UnityWebGlTest/").expect("failed to resolve resource"); let resource_path_string = resource_path.to_str().unwrap().to_owned(); let server = thread::spawn( move || { let sys = actixrt::System::new(); let = HttpServer::new(move || { App::new().service(fs::Files::new("/UnityWebGlTest", resource_path_string.clone()).show_fileslisting()) }) .bind("127.0.0.1:8000") .unwrap() .run(); let = sys.run(); });

        Ok(())
    })

in main.rs

moved UnityWebGlTest into src-tauri

and added "resources": [ "UnityWebGlTest/*" ], in tauri.conf.json. hoping to load it from the localhost:8000 but it is not getting hosted there

amrbashir commented 1 year ago

yikes, I wasted an hour trying to debug and fix this only to discover you're using an iframe which is the root cause of the problem. So it is duplicate of #7114, basically requests from within the iframe doesn't reach our internal server. We are waiting for microsoft to release the new API in stable WebView2.

mad-jose commented 1 year ago

yikes, I wasted an hour trying to debug and fix this only to discover you're using an iframe which is the root cause of the problem. So it is duplicate of #7114, basically requests from within the iframe doesn't reach our internal server. We are waiting for microsoft to release the new API in stable WebView2.

so i could potentially use actix_web and add resources in config and self host it and call it via iframe right? that is what i tried before,

amrbashir commented 1 year ago

usually I won't recommend using a localhost server but for this use-case there is no other workarounds at the moment

ezewu commented 1 year ago

How to achieve this function, can you guide me

amrbashir commented 1 year ago

you could try using tauri-plugin-localhost or bundle the unity files as resources and use any rust server to serve them.

DerAndereJohannes commented 3 months ago

Are you hosting the build files on your device?

I just got the unity web app working in tauri (release build) by simply putting all the build files in the root static folder (all webeditor files are in the /static/ folder) and setting my config like this:

const config = {
        dataUrl: '/webeditor.data',
        frameworkUrl: '/webeditor.framework.js',
        codeUrl: '/webeditor.wasm',
        StreamingAssetsUrl: null,
        companyName: "com.somecompany.test",
        productName: "0.1.0",
        showBanner: null
    }

and applying it like this:

canvasElement = document.querySelector("#unity-canvas");
    const unityScript = document.createElement('script');
    unityScript.src = "/webeditor.loader.js";
    unityScript.onload = () => {
        console.log("loading unity...");
        createUnityInstance(canvasElement, config, (progress) => {
            console.log(progress);
        }).then((instance) => {
                unityInstance = instance;
            }).catch((msg) => {
                console.log(msg);
            });
    }
    document.body.appendChild(unityScript);
  }

This is all without a localhost, using the plugin or iframes. I am not sure if this helps you for a workaround, but id be happy to assist if there is further trouble.