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.
3948const 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+ ```
0 commit comments