Git Product home page Git Product logo

andreibosco / unitywebgl.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meqn/unitywebgl.js

0.0 1.0 0.0 346 KB

🏣 An easy solution for embedding Unity WebGL builds in webApp or Vue.js project, with two-way communication between your webApp and Unity. 🏩 在webApp 或 Vue.js 项目中嵌入 Unity WebGL,并支持通过API在 webApp 和 Unity 之间进行双向通信。🏰

License: MIT License

JavaScript 96.15% Vue 0.66% HTML 0.86% TypeScript 2.33%

unitywebgl.js's Introduction

unity-webgl

Unity WebGL provides an easy solution for embedding Unity WebGL builds in your webApp or Vue.js project, with two-way communication between your webApp and Unity application with advanced API's.

UnityWebgl.js 提供了一种简单的解决方案,用于在 webApp 或 Vue.js 项目中嵌入 Unity WebGL,并通过API在 webApp 和 Unity 之间进行双向通信。

based on react-unity-webgl

features

  • 💊 Simple and flexible to use
  • 📮 two-way communication (webApp, Unity)
  • 🛠 Built-in event handler
  • 🧬 Available for Vue.js

API

Unity Config

  • loaderUrl: string The url to the build json file generated by Unity
  • dataUrl: string : The url to the build data file generated by Unity
  • frameworkUrl: string : The url to the framework file generated by Unity
  • codeUrl: string : The url to the unity code file generated by Unity
  • streamingAssetsUrl?: string : The url where the streaming assets can be found
  • companyName?: string : The applications company name
  • productName?: string : The applications product name
  • productVersion?: string : The applications product version
  • webglContextAttributes?: IWebGLContextAttributes : This object allow you to configure WebGLRenderingContext creation options. see MDN@WebGLRenderingContext
  • devicePixelRatio?: number : Uncomment this to override low DPI rendering on high DPI displays. see MDN@devicePixelRatio
  • matchWebGLToCanvasSize?: boolean : Uncomment this to separately control WebGL canvas render size and DOM element size. see unity3d@matchWebGLToCanvasSize

Unity Instance

Methods:

  • on(eventName: string, eventListener: function)
  • once(eventName: string, eventListener: function)
  • off(eventName: string)
  • clear()
  • emit(eventName: string)

  • create(canvasElement: HTMLCanvasElement | string)
  • send(objectName: string, methodName: string, params: any)
  • setFullscreen()
  • destroy()

Events:

  • progress(value: number) : loading progress.
  • loaded() : loading completed.
  • created() : Unity instance is created.
  • destroyed() : Quits the Unity Instance and clears it from memory.

vue component

props

  • unity: UnityWebgl
  • width?: string|number , default: 100%
  • height?: string|number , default: 100%

Install

npm install unity-webgl

browser

https://cdn.jsdelivr.net/npm/unity-webgl/dist/UnityWebgl.min.js

Usage

in example.html:

<canvas id="canvas" style="width: 100%; height: 100%"></canvas>

<button onclick="onDestroy()">Destroy</button>
<button onclick="onLoad()">Reload</button>
<button onclick="onFullscreen()">Fullscreen</button>

<script>
var Unity = new UnityWebgl.default('#canvas', {
  loaderUrl: 'Build/OUT_BIM.loader.js',
  dataUrl: "Build/OUT_BIM.data",
  frameworkUrl: "Build/OUT_BIM.framework.js",
  codeUrl: "Build/OUT_BIM.wasm"
})

Unity
  .on('progress', percent => console.log('Unity Loaded: ', percent))
  .on('created', () => console.log('Unity Instance: created.'))
  .on('destroyed', () => console.log('Unity Instance: Destroyed.'))

function postMessage() {
  Unity.send('objectName', 'methodName', {
    id: 'B0001',
    name: 'Building#1',
    location: [150, 75]
  })
}

function onDestroy() {
  Unity.destroy()
}

function onLoad() {
  Unity.create('#canvas')
}

function onFullscreen() {
  Unity.setFullscreen(true)
}
</script>

You can also:

var Unity = new UnityWebgl.default({
  loaderUrl: 'Build/OUT_BIM.loader.js',
  dataUrl: "Build/OUT_BIM.data",
  frameworkUrl: "Build/OUT_BIM.framework.js",
  codeUrl: "Build/OUT_BIM.wasm",
  streamingAssetsUrl: "StreamingAssets",
  companyName: "DefaultCompany",
  productName: "Unity",
  productVersion: "0.1",
})

Unity.create(document.querySelector('#canvas'))

Vue

in example.vue

<template>
  <Unity :unity="unityContext" width="800px" height="600px" />
</template>

<script>
import UnityWebgl, { VueUnity } from 'unity-webgl'

const Unity = new UnityWebgl({
  loaderUrl: 'Build/OUT_BIM.loader.js',
  dataUrl: "Build/OUT_BIM.data",
  frameworkUrl: "Build/OUT_BIM.framework.js",
  codeUrl: "Build/OUT_BIM.wasm"
})

export default {
  name: 'Unity',
  component: {
    Unity: VueUnity
  },
  data() {
    return {
      unityContext: Unity
    }
  }
}
</script>

Communication

  1. In Unity call js functions.
    在Unity中调用js方法。
// # in Unity
// Call the `showDialog` method in unity.
__UnityLib__.showDialog(data)

// 📢 `__UnityLib__` is a global function collection.
// # in webApp

const Unity = new UnityWebgl.default()

// Register functions
Unity.on('showDialog', (data: any) => {
  console.log(data)
  $('#dialog').show()
})

// you also can call function.
Unity.emit('showDialog', data)
// or
window[Unity.global_name].showDialog(data) // 📢 Unity.global_name = __UnityLib__
  1. JS call Unity public methods.
    在web页面内调用 Unity public方法。
/**
 * Sends a message to the UnityInstance to invoke a public method.
 * @param {string} objectName Unity scene name.
 * @param {string} methodName public method name.
 * @param {any} params an optional method parameter.
 */
Unity.send(objectName, methodName, params)

// e.g. Initialize Building#001 data
Unity.send('mainScene', 'init', {
  id: 'b001',
  name: 'building#001',
  length: 95,
  width: 27,
  height: 120
})

unitywebgl.js's People

Contributors

meqn avatar

Watchers

James Cloos 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.