Skip to content

Commit 4246a79

Browse files
author
Philipp Molitor
committed
add missing config options and bump version
1 parent 7eaa202 commit 4246a79

File tree

4 files changed

+89
-21
lines changed

4 files changed

+89
-21
lines changed

README.md

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# React Unity Renderer
22

3+
<p align="center">
4+
5+
<img src="https://img.shields.io/npm/l/react-unity-renderer?style=flat-square">
6+
<img src="https://img.shields.io/npm/dw/react-unity-renderer?style=flat-square">
7+
<img src="https://img.shields.io/github/stars/PhilippMolitor/react-unity-renderer?style=flat-square">
8+
<img src="https://img.shields.io/npm/v/react-unity-renderer?style=flat-square">
9+
10+
</p>
11+
312
> This project is heavily inspired by [react-unity-webgl](https://github.com/elraccoone/react-unity-webgl) made by Jeffrey Lanters.
413
> This is a modernized, `FunctionComponent` based interpretation of his ideas.
514
@@ -37,11 +46,13 @@ import {
3746
// you *could* put a JSON in your WebGL template containing this information
3847
// and load that with fetch or axios to assemble your config.
3948
const config: UnityLoaderConfig = {
40-
loaderUrl: '',
41-
frameworkUrl: '',
42-
codeUrl: '',
43-
dataUrl: '',
49+
loaderUrl: '...',
50+
frameworkUrl: '...',
51+
codeUrl: '...',
52+
dataUrl: '...',
4453
// everything from here on is optional
54+
memoryUrl: '',
55+
symbolsUrl: '',
4556
streamingAssetsUrl: '',
4657
companyName: '',
4758
productName: '',
@@ -64,3 +75,70 @@ export const UnityGameComponent: VFC = (): JSX.Element => {
6475
);
6576
};
6677
```
78+
79+
## Creating a fetchable config from a Unity WebGL template
80+
81+
In order to create a fetchable build config that contains all required keys for `UnityLoaderConfig`, you could add the following to a Unity WebGL template and upload it to a `CORS`-enabled web host (for example Amazon AWS S3).
82+
83+
`build.json`
84+
85+
```json
86+
{
87+
"loaderUrl": "Build/{{{ LOADER_FILENAME }}}",
88+
"frameworkUrl": "Build/{{{ FRAMEWORK_FILENAME }}}",
89+
"codeUrl": "Build/{{{ CODE_FILENAME }}}",
90+
#if MEMORY_FILENAME
91+
"memoryUrl": "Build/{{{ MEMORY_FILENAME }}}",
92+
#endif
93+
#if SYMBOLS_FILENAME
94+
"symbolsUrl": "Build/{{{ SYMBOLS_FILENAME }}}",
95+
#endif
96+
"dataUrl": "Build/{{{ DATA_FILENAME }}}",
97+
"streamingAssetsUrl": "StreamingAssets",
98+
"companyName": "{{{ COMPANY_NAME }}}",
99+
"productName": "{{{ PRODUCT_NAME }}}",
100+
"productVersion": "{{{ PRODUCT_VERSION }}}"
101+
}
102+
103+
```
104+
105+
Take the following example using `axios`:
106+
107+
`unity-api.ts`
108+
109+
```ts
110+
import axios, { AxiosResponse } from 'axios';
111+
import { UnityLoaderConfig } from 'react-unity-renderer';
112+
113+
function fetchLoaderConfig(baseUrl: string): Promise<UnityLoaderConfig> {
114+
let result: AxiosResponse<UnityLoaderConfig>;
115+
116+
try {
117+
const url = `${baseUrl}/build.json?t=${new Date().getTime()}`;
118+
result = await axios.get<UnityLoaderConfig>(url);
119+
} catch (ex) {
120+
// network or request error
121+
throw new Error('unable to load build info');
122+
}
123+
124+
const { status, data } = result;
125+
126+
// invalid response
127+
if (status < 200 || status >= 400) {
128+
throw new Error('unable to load build info');
129+
}
130+
131+
return {
132+
loaderUrl: `${baseUrl}/${data.loaderUrl}`,
133+
frameworkUrl: `${baseUrl}/${data.frameworkUrl}`,
134+
codeUrl: `${baseUrl}/${data.codeUrl}`,
135+
dataUrl: `${baseUrl}/${data.dataUrl}`,
136+
memoryUrl: `${baseUrl}/${data.memoryUrl}`,
137+
symbolsUrl: `${baseUrl}/${data.symbolsUrl}`,
138+
streamingAssetsUrl: `${baseUrl}/${data.streamingAssetsUrl}`,
139+
companyName: `${data.companyName}`,
140+
productName: `${data.productName}`,
141+
productVersion: `${data.productVersion}`,
142+
};
143+
}
144+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-unity-renderer",
3-
"version": "1.2020.0",
3+
"version": "1.2020.1",
44
"description": "React Unity Renderer allows to interactively embed Unity WebGL builds into a React powered project.",
55
"main": "./dist/index.js",
66
"author": "Philipp Molitor <philipp@molitor.cloud>",

src/global.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ interface Window {
77
createUnityInstance(
88
element: HTMLCanvasElement,
99
parameters: {
10-
dataUrl: string;
11-
1210
frameworkUrl: string;
13-
1411
codeUrl: string;
15-
12+
dataUrl: string;
13+
memoryUrl?: string;
14+
symbolsUrl?: string;
1615
streamingAssetsUrl?: string;
17-
1816
companyName?: string;
19-
2017
productName?: string;
21-
2218
productVersion?: string;
23-
2419
modules?: Object;
2520
},
2621
onProgress?: (progression: number) => void

src/interfaces/config.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
export type UnityInstanceConfig = {
2-
dataUrl: string;
3-
42
frameworkUrl: string;
5-
63
codeUrl: string;
7-
4+
dataUrl: string;
5+
memoryUrl?: string;
6+
symbolsUrl?: string;
87
streamingAssetsUrl?: string;
9-
108
companyName?: string;
11-
129
productName?: string;
13-
1410
productVersion?: string;
15-
1611
modules?: Object;
1712
};
1813

0 commit comments

Comments
 (0)