Giter Site home page Giter Site logo

Comments (9)

dtanquary avatar dtanquary commented on June 27, 2024 5

Here is my solution for loading Unity webGL games in vue3 as I wasn't able to get this module to work either. My solution was tested on Unity 2020.3.19f1 but probably works on all 2020.x versions and above.

First, put a copy of your webGL build folder in your vue public directory:

Screen Shot 2021-10-25 at 9 07 26 PM

Then copy and paste the code below into a new component and update const file = 'BUILD_FILE'; with your game name. There are further improvements that can be made here but this should be enough to get you started:

<template>
  <canvas
    id="unity-canvas"
    width=900
    height=600
    style="width: 900px; height: 600px; background: #808080">
  </canvas>
</template>

<script setup>
import { onMounted } from 'vue';

const sendMessage = (object, method, param) => {
  window.gameInstance.SendMessage(object, method, param);
};

onMounted(() => {
  const file = 'BUILD_FILE';
  const script = document.createElement('script');
  script.onload = () => {
    createUnityInstance(document.querySelector('#unity-canvas'), {
      dataUrl: `Build/${file}.data`,
      frameworkUrl: `Build/${file}.framework.js`,
      codeUrl: `Build/${file}.wasm`,
      streamingAssetsUrl: 'StreamingAssets',
      companyName: 'YOUR_COMPANY_NAME',
      productName: 'YOUR_PRODUCT_NAME',
      productVersion: 'YOUR_VERSION_NUMBER',
      // matchWebGLToCanvasSize: false,
      // Uncomment above to separately control WebGL canvas render size and DOM element size.
      // devicePixelRatio: 1,
      // Uncomment above to override low DPI rendering on high DPI displays.
    }).then((unityInstance) => {
      // setting this allows the usage of "window.gameInstance" in jslib plugins inside Unity
      // it also sets up a simple shortcut we can use to provide a path into Unity from vue
      window.gameInstance = unityInstance;
    });
  };
  script.async = true;
  script.src = `Build/${file}.loader.js`;
  document.head.appendChild(script);
});

</script>

Some notes:

  • I used [email protected] and the composition api but this can just as easily be done with the options api if that is what you prefer
  • If you run into any issues try copying and pasting the values directly from your built webgl game's index.html into the script.onload section.
  • createUnityInstance is a promise now and so if you want to be able to call SendMessage you need to .then or await the call and then map the unityInstance back to some variable. I use window.gameInstance because window is accessible in jslib plugins inside Unity.

from vue-unity-webgl.

88er avatar 88er commented on June 27, 2024

same errors with me,i think it should be update to vue3.

from vue-unity-webgl.

wcai49 avatar wcai49 commented on June 27, 2024

same here, is there any solution for vue3?

from vue-unity-webgl.

scalemaildev avatar scalemaildev commented on June 27, 2024

For anyone else reading this. For Nuxt3 rc4 I had to specify the .br in my build files to get it to load.

EDIT: I also moved the unity instance from the window to it's own constant and it's working fine. Personal preference.

from vue-unity-webgl.

akbarism avatar akbarism commented on June 27, 2024

Here is my solution for loading Unity webGL games in vue3 as I wasn't able to get this module to work either. My solution was tested on Unity 2020.3.19f1 but probably works on all 2020.x versions and above.

First, put a copy of your webGL build folder in your vue public directory:

Screen Shot 2021-10-25 at 9 07 26 PM

Then copy and paste the code below into a new component and update const file = 'BUILD_FILE'; with your game name. There are further improvements that can be made here but this should be enough to get you started:

<template>
  <canvas
    id="unity-canvas"
    width=900
    height=600
    style="width: 900px; height: 600px; background: #808080">
  </canvas>
</template>

<script setup>
import { onMounted } from 'vue';

const sendMessage = (object, method, param) => {
  window.gameInstance.SendMessage(object, method, param);
};

onMounted(() => {
  const file = 'BUILD_FILE';
  const script = document.createElement('script');
  script.onload = () => {
    createUnityInstance(document.querySelector('#unity-canvas'), {
      dataUrl: `Build/${file}.data`,
      frameworkUrl: `Build/${file}.framework.js`,
      codeUrl: `Build/${file}.wasm`,
      streamingAssetsUrl: 'StreamingAssets',
      companyName: 'YOUR_COMPANY_NAME',
      productName: 'YOUR_PRODUCT_NAME',
      productVersion: 'YOUR_VERSION_NUMBER',
      // matchWebGLToCanvasSize: false,
      // Uncomment above to separately control WebGL canvas render size and DOM element size.
      // devicePixelRatio: 1,
      // Uncomment above to override low DPI rendering on high DPI displays.
    }).then((unityInstance) => {
      // setting this allows the usage of "window.gameInstance" in jslib plugins inside Unity
      // it also sets up a simple shortcut we can use to provide a path into Unity from vue
      window.gameInstance = unityInstance;
    });
  };
  script.async = true;
  script.src = `Build/${file}.loader.js`;
  document.head.appendChild(script);
});

</script>

Some notes:

  • I used [email protected] and the composition api but this can just as easily be done with the options api if that is what you prefer
  • If you run into any issues try copying and pasting the values directly from your built webgl game's index.html into the script.onload section.
  • createUnityInstance is a promise now and so if you want to be able to call SendMessage you need to .then or await the call and then map the unityInstance back to some variable. I use window.gameInstance because window is accessible in jslib plugins inside Unity.

have you tried it using vite? I have a problem when importing the wasm file

from vue-unity-webgl.

sunjiesama avatar sunjiesama commented on June 27, 2024

Here is my solution for loading Unity webGL games in vue3 as I wasn't able to get this module to work either. My solution was tested on Unity 2020.3.19f1 but probably works on all 2020.x versions and above.

First, put a copy of your webGL build folder in your vue public directory:

Screen Shot 2021-10-25 at 9 07 26 PM

Then copy and paste the code below into a new component and update const file = 'BUILD_FILE'; with your game name. There are further improvements that can be made here but this should be enough to get you started:

<template>
  <canvas
    id="unity-canvas"
    width=900
    height=600
    style="width: 900px; height: 600px; background: #808080">
  </canvas>
</template>

<script setup>
import { onMounted } from 'vue';

const sendMessage = (object, method, param) => {
  window.gameInstance.SendMessage(object, method, param);
};

onMounted(() => {
  const file = 'BUILD_FILE';
  const script = document.createElement('script');
  script.onload = () => {
    createUnityInstance(document.querySelector('#unity-canvas'), {
      dataUrl: `Build/${file}.data`,
      frameworkUrl: `Build/${file}.framework.js`,
      codeUrl: `Build/${file}.wasm`,
      streamingAssetsUrl: 'StreamingAssets',
      companyName: 'YOUR_COMPANY_NAME',
      productName: 'YOUR_PRODUCT_NAME',
      productVersion: 'YOUR_VERSION_NUMBER',
      // matchWebGLToCanvasSize: false,
      // Uncomment above to separately control WebGL canvas render size and DOM element size.
      // devicePixelRatio: 1,
      // Uncomment above to override low DPI rendering on high DPI displays.
    }).then((unityInstance) => {
      // setting this allows the usage of "window.gameInstance" in jslib plugins inside Unity
      // it also sets up a simple shortcut we can use to provide a path into Unity from vue
      window.gameInstance = unityInstance;
    });
  };
  script.async = true;
  script.src = `Build/${file}.loader.js`;
  document.head.appendChild(script);
});

</script>

Some notes:

  • I used [email protected] and the composition api but this can just as easily be done with the options api if that is what you prefer
  • If you run into any issues try copying and pasting the values directly from your built webgl game's index.html into the script.onload section.
  • createUnityInstance is a promise now and so if you want to be able to call SendMessage you need to .then or await the call and then map the unityInstance back to some variable. I use window.gameInstance because window is accessible in jslib plugins inside Unity.

Hi, thank you very much for sharing your code
I have a question that I would like to ask. The files exported by my unity have no .data, no .framework.js, and no wasm. The directory is as follows. How should I solve it?
image

from vue-unity-webgl.

dtanquary avatar dtanquary commented on June 27, 2024

@akbarism

have you tried it using vite? I have a problem when importing the wasm file

Not yet but it is on my product roadmap to move from vue-cli-service to vite as that's the new default. Can you post the exact error you get? I can maybe take a look and see if there is any suggestion for vite config that can help.

from vue-unity-webgl.

dtanquary avatar dtanquary commented on June 27, 2024

@sunjiesama

The files exported by my unity have no .data, no .framework.js, and no wasm.

Can you try doing a build where in your publishing settings you set "Compression Format" to disabled and see if that gets you the correct files?

Screenshot 2023-01-23 at 11 47 27 AM

from vue-unity-webgl.

akbarism avatar akbarism commented on June 27, 2024

@akbarism

have you tried it using vite? I have a problem when importing the wasm file

Not yet but it is on my product roadmap to move from vue-cli-service to vite as that's the new default. Can you post the exact error you get? I can maybe take a look and see if there is any suggestion for vite config that can help.

i solved by moving to unity-webgl

from vue-unity-webgl.

Related Issues (20)

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.