Skip to content

Commit

Permalink
feat: Add HTML, Vue2, and Vue3 examples for UnityWebgl (#38)
Browse files Browse the repository at this point in the history
* docs: Update demo links in READMEs

* feat: Add HTML, Vue2, and Vue3 examples for UnityWebgl
  • Loading branch information
Marinerer authored Dec 20, 2024
1 parent 2aca339 commit e27906f
Show file tree
Hide file tree
Showing 21 changed files with 4,766 additions and 2,365 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ npm install unity-webgl

## Quick Start

- [Live Demo]()
- [vue2 Demo](https://stackblitz.com/edit/unity-webgl-vue2-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-vue3-demo)
- [Live Demo](https://stackblitz.com/edit/unity-webgl-v4-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-v4-vue3-demo)

> 🚨 **Important:**
> Communication and interaction with the web application are only possible after the Unity instance is successfully created (when the `mounted` event is triggered).
Expand Down
5 changes: 2 additions & 3 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ https://cdn.jsdelivr.net/npm/unity-webgl/dist/index.min.js

## Quick Start

- [Live Demo]()
- [vue2 Demo](https://stackblitz.com/edit/unity-webgl-vue2-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-vue3-demo)
- [Live Demo](https://stackblitz.com/edit/unity-webgl-v4-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-v4-vue3-demo)

> 🚨 提醒:
> 仅在 `Unity` 实例成功创建后(触发 `mounted` 事件时)才能进行 Web 应用程序的通信和交互。
Expand Down
24 changes: 24 additions & 0 deletions examples/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telphone=no" />
<title><title>unity-webgl demo</title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>
<div>
<canvas id="canvas" style="width: 800px; height: 600px"></canvas>

<div>
<button onclick="sendMessage()">postMessage</button>
<button onclick="onFullscreen()">Fullscreen</button>
</div>
</div>
</body>
<script src="main.js" type="module"></script>
</html>
30 changes: 30 additions & 0 deletions examples/html/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import UnityWebgl from 'unity-webgl'

const unityContext = new UnityWebgl('#canvas', {
loaderUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.loader.js',
dataUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.data',
frameworkUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.framework.js',
codeUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.wasm',
})

unityContext
.on('progress', (p) => console.log('loading :', p))
.on('mounted', () => console.log('unity mounted ...'))
.on('debug', (msg) => console.log('unity debug', msg))

unityContext.addUnityListener('gameStart', (msg) => {
alert(msg)
console.log('gameStart : ', msg)
})

window.sendMessage = function sendMessage() {
unityContext.sendMessage('GameUI', 'ReceiveRole', 'Tanya')
}

window.onFullscreen = function () {
unityContext.setFullscreen(true)
}
15 changes: 15 additions & 0 deletions examples/html/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "html",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite"
},
"devDependencies": {
"vite": "^6.0.5"
},
"dependencies": {
"unity-webgl": "workspace:^"
}
}
7 changes: 7 additions & 0 deletions examples/html/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'

export default defineConfig({
server: {
port: 3010,
},
})
55 changes: 55 additions & 0 deletions examples/vue2/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<script>
import UnityWebgl from 'unity-webgl'
import VueUnity from 'unity-webgl/vue'
const unityContext = new UnityWebgl({
loaderUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.loader.js',
dataUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.data',
frameworkUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.framework.js',
codeUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.wasm',
});
unityContext
.on('progress', (p) => console.log('loading :', p))
.on('mounted', () => console.log('unity mounted ...'))
.on('debug', (msg) => console.log('unity debug', msg));
unityContext.addUnityListener('gameStart', (msg) => {
alert(msg);
console.log('gameStart : ', msg);
});
export default {
name: 'App',
components: {
VueUnity
},
data() {
return {
unityContext
}
},
methods: {
onSendMessage() {
unityContext.sendMessage('GameUI', 'ReceiveRole', 'Tanya')
},
onFullscreen() {
unityContext.setFullscreen(true)
}
}
}
</script>

<template>
<div>
<VueUnity :unity="unityContext" width="800px" height="600px" tabindex="0" />
<div>
<button @click="onSendMessage">SendMessage</button>
<button @click="onFullscreen">Fullscreen</button>
</div>
</div>
</template>
17 changes: 17 additions & 0 deletions examples/vue2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telphone=no" />
<title>unity-webgl vue2 demo</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>
<div id="app"></div>
<script src="/main.js" type="module"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions examples/vue2/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Vue from 'vue'
import App from './App.vue'

new Vue({
render: (h) => h(App),
}).$mount('#app')
18 changes: 18 additions & 0 deletions examples/vue2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "vue2",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite"
},
"devDependencies": {
"@vitejs/plugin-legacy": "^6.0.0",
"@vitejs/plugin-vue2": "^2.3.3",
"vite": "^6.0.5"
},
"dependencies": {
"unity-webgl": "^4.0.1",
"vue": "2.7.16"
}
}
10 changes: 10 additions & 0 deletions examples/vue2/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'
import legacy from '@vitejs/plugin-legacy'
import vue from '@vitejs/plugin-vue2'

export default defineConfig({
plugins: [vue(), legacy()],
server: {
port: 3020,
},
})
41 changes: 41 additions & 0 deletions examples/vue3/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script setup>
import { ref, onMounted } from 'vue'
import UnityWebgl from 'unity-webgl'
import VueUnity from 'unity-webgl/vue'
const unityContext = new UnityWebgl({
loaderUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.loader.js',
dataUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.data',
frameworkUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.framework.js',
codeUrl:
'https://static-huariot-com.oss-cn-hangzhou.aliyuncs.com/uploads/17343457791501843/demo.wasm',
});
unityContext
.on('progress', (p) => console.log('loading :', p))
.on('mounted', () => console.log('unity mounted ...'))
.on('debug', (msg) => console.log('unity debug', msg));
unityContext.addUnityListener('gameStart', (msg) => {
alert(msg);
console.log('gameStart : ', msg);
});
function onSendMessage() {
unityContext.sendMessage('GameUI', 'ReceiveRole', 'Tanya')
}
function onFullscreen() {
unityContext.setFullscreen(true)
}
</script>

<template>
<VueUnity :unity="unityContext" width="800px" height="600px" tabindex="0" />
<div>
<button @click="onSendMessage">SendMessage</button>
<button @click="onFullscreen">Fullscreen</button>
</div>
</template>
17 changes: 17 additions & 0 deletions examples/vue3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telphone=no" />
<title>unity-webgl vue3 demo</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>
<div id="app"></div>
</body>
<script src="/main.js" type="module"></script>
</html>
6 changes: 6 additions & 0 deletions examples/vue3/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Vue from 'vue'
import App from './App.vue'

new Vue({
render: (h) => h(App),
}).$mount('#app')
17 changes: 17 additions & 0 deletions examples/vue3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "vue3",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "vite"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.1",
"vite": "^6.0.5"
},
"dependencies": {
"unity-webgl": "workspace:^",
"vue": "3"
}
}
9 changes: 9 additions & 0 deletions examples/vue3/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
server: {
port: 3021,
},
})
5 changes: 2 additions & 3 deletions lib/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ npm install unity-webgl

## Quick Start

- [Live Demo]()
- [vue2 Demo](https://stackblitz.com/edit/unity-webgl-vue2-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-vue3-demo)
- [Live Demo](https://stackblitz.com/edit/unity-webgl-v4-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-v4-vue3-demo)

> 🚨 **Important:**
> Communication and interaction with the web application are only possible after the Unity instance is successfully created (when the `mounted` event is triggered).
Expand Down
5 changes: 2 additions & 3 deletions lib/core/README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ https://cdn.jsdelivr.net/npm/unity-webgl/dist/index.min.js

## Quick Start

- [Live Demo]()
- [vue2 Demo](https://stackblitz.com/edit/unity-webgl-vue2-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-vue3-demo)
- [Live Demo](https://stackblitz.com/edit/unity-webgl-v4-demo)
- [vue3 Demo](https://stackblitz.com/edit/unity-webgl-v4-vue3-demo)

> 🚨 提醒:
> 仅在 `Unity` 实例成功创建后(触发 `mounted` 事件时)才能进行 Web 应用程序的通信和交互。
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"build:all": "pnpm -r --filter=./lib/* run build",
"build": "pnpm run build:core && pnpm run build:components",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"demo": "pnpm --filter html run dev",
"demo:vue2": "pnpm --filter vue2 run dev",
"demo:vue3": "pnpm --filter vue3 run dev",
"bump": "bumpp lib/core/package.json",
"publish": "pnpm -r --filter unity-webgl publish --no-git-checks",
"preinstall": "npx only-allow pnpm",
Expand Down
Loading

0 comments on commit e27906f

Please sign in to comment.