@reactunity/scripts 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/bin/react-unity-scripts.js +0 -0
- package/config/babel-preset-dependencies.js +26 -0
- package/config/babel-preset-extended.js +53 -15
- package/config/babel-webpack-overrides.js +25 -0
- package/config/env.js +0 -2
- package/config/getHttpsConfig.js +0 -2
- package/config/jest/babelTransform.js +0 -2
- package/config/jest/cssTransform.js +0 -2
- package/config/jest/fileTransform.js +0 -2
- package/config/modules.js +60 -12
- package/config/paths.js +20 -7
- package/config/public/index.html +151 -62
- package/config/public/info.html +159 -0
- package/config/unityMiddleware.js +19 -9
- package/config/webpack/persistentCache/createEnvironmentHash.js +0 -1
- package/config/webpack.config.js +26 -60
- package/config/webpackDevServer.config.js +7 -16
- package/lib/types/base.d.ts +12 -1
- package/package.json +26 -22
- package/scripts/build.js +9 -10
- package/scripts/init.js +9 -5
- package/scripts/start.js +10 -1
- package/scripts/utils/createJestConfig.js +1 -1
- package/tsconfig.json +1 -1
- package/scripts/utils/verifyTypeScriptSetup.js +0 -263
package/README.md
CHANGED
|
@@ -42,6 +42,8 @@ In addition to `src/index` file, there can be other entry points. Every file und
|
|
|
42
42
|
|
|
43
43
|
## WebGL Inspector
|
|
44
44
|
|
|
45
|
-
When dev server is started with the `start` script, a web server is launched at the selected port (e.g. http://localhost:3000). This server serves the javascript and asset files that are used by ReactUnity.
|
|
45
|
+
When dev server is started with the `start` script, a web server is launched at the selected port (e.g. http://localhost:3000). This server serves the javascript and asset files that are used by ReactUnity. Visiting it in a browser opens the previewer, which runs your UI in a Unity WebGL player without launching the editor, and reacts to code changes via HMR. `/info.html` on the same server explains how to point Unity's `DevServer` property at it.
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
Nothing needs installing for this — the previewer's Unity build is hosted on `reactunity.github.io` rather than shipped as a dependency, so the first load fetches it over the network and the browser caches it thereafter. It used to be an optional `@reactunity/previewer` package, which meant a ~107 MB install for a page many projects never opened. That package is no longer used.
|
|
48
|
+
|
|
49
|
+
Note that the previewer has very limited capabilities and ReactUnity is not optimised for WebGL, so performance is well below a real player. It may not work for all cases, but it is useful when starting a new project. To preview your own scene instead — or to work offline — build a scene in Unity with a `ReactRendererUGUI` component, its `Type` set to `Url` and `Source Path` set to `/index.js`, and copy the output into a `previewer` folder under your React project so that `previewer/index.html` exists. The dev server serves that in preference to the built-in previewer, and your project's own `public` folder takes precedence over both.
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The preset applied to JS coming out of node_modules, replacing
|
|
2
|
+
// babel-preset-react-app/dependencies (see babel-preset-extended.js for why that package
|
|
3
|
+
// had to go). Dependencies get standard ES features lowered and nothing else -- no JSX, no
|
|
4
|
+
// TypeScript, no macros -- since they are already compiled.
|
|
5
|
+
//
|
|
6
|
+
// This is also what keeps published ES2015+ packages usable on Unity's older engines: it is
|
|
7
|
+
// the pass that lowers @reactunity/renderer's own dist on the way into an app bundle.
|
|
8
|
+
module.exports = (_api, opts = {}) => ({
|
|
9
|
+
presets: [
|
|
10
|
+
[
|
|
11
|
+
require.resolve('@babel/preset-env'),
|
|
12
|
+
{
|
|
13
|
+
// Leave modules alone so webpack can tree-shake.
|
|
14
|
+
modules: false,
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
],
|
|
18
|
+
plugins: [
|
|
19
|
+
// Babel 8 removed the `helpers` option; including the plugin at all is what enables
|
|
20
|
+
// helper imports.
|
|
21
|
+
opts.helpers !== false && [
|
|
22
|
+
require.resolve('@babel/plugin-transform-runtime'),
|
|
23
|
+
{ version: require('@babel/runtime/package.json').version },
|
|
24
|
+
],
|
|
25
|
+
].filter(Boolean),
|
|
26
|
+
});
|
|
@@ -1,19 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// The preset that compiles application code.
|
|
2
|
+
//
|
|
3
|
+
// It used to wrap babel-preset-react-app, extending it only to honour JSX_IMPORT_SOURCE.
|
|
4
|
+
// That package is unmaintained and hard-asserts Babel 7 (`Requires Babel "^7.0.0-0", but was
|
|
5
|
+
// loaded with "8.0.1"`), which pinned @babel/core at 7 for the whole workspace. What follows
|
|
6
|
+
// is the same composition written against Babel 8.
|
|
7
|
+
//
|
|
8
|
+
// Two things it listed are absent rather than ported: the class-properties /
|
|
9
|
+
// private-methods / private-property-in-object transforms are part of preset-env in Babel 8,
|
|
10
|
+
// and transform-runtime's `regenerator` option was removed there. preset-env's
|
|
11
|
+
// `useBuiltIns`/`corejs` pair is gone too (Babel 8 points at babel-plugin-polyfill-corejs3),
|
|
12
|
+
// so no polyfills are injected automatically -- apps that need them import react-app-polyfill,
|
|
13
|
+
// which is what the scaffold does.
|
|
14
|
+
module.exports = (api, opts = {}) => {
|
|
15
|
+
const env = api.env();
|
|
16
|
+
const isEnvDevelopment = env === 'development';
|
|
17
|
+
const isEnvTest = env === 'test';
|
|
3
18
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
19
|
+
const isTypeScriptEnabled = opts.typescript !== false;
|
|
20
|
+
const areHelpersEnabled = opts.helpers !== false;
|
|
21
|
+
// Set per app (see packages/scripts/README.md) so projects using @emotion/react and
|
|
22
|
+
// friends can redirect the automatic JSX runtime.
|
|
8
23
|
const jsxImportSource = process.env.JSX_IMPORT_SOURCE;
|
|
9
24
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
return {
|
|
26
|
+
presets: [
|
|
27
|
+
isEnvTest
|
|
28
|
+
? [require.resolve('@babel/preset-env'), { targets: { node: 'current' } }]
|
|
29
|
+
: [
|
|
30
|
+
require.resolve('@babel/preset-env'),
|
|
31
|
+
{
|
|
32
|
+
// Leave modules alone so webpack can tree-shake.
|
|
33
|
+
modules: false,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
require.resolve('@babel/preset-react'),
|
|
38
|
+
{
|
|
39
|
+
development: isEnvDevelopment || isEnvTest,
|
|
40
|
+
runtime: opts.runtime === 'classic' ? 'classic' : 'automatic',
|
|
41
|
+
...(jsxImportSource ? { importSource: jsxImportSource } : {}),
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
// Babel strips the types; tsc only checks them (`pnpm typecheck`).
|
|
45
|
+
isTypeScriptEnabled && [require.resolve('@babel/preset-typescript')],
|
|
46
|
+
].filter(Boolean),
|
|
47
|
+
plugins: [
|
|
48
|
+
require.resolve('babel-plugin-macros'),
|
|
49
|
+
// In Babel 8 the plugin's presence *is* the switch: `helpers` (and `version`) were
|
|
50
|
+
// removed, so opting out means leaving it out.
|
|
51
|
+
areHelpersEnabled && [
|
|
52
|
+
require.resolve('@babel/plugin-transform-runtime'),
|
|
53
|
+
{ version: require('@babel/runtime/package.json').version },
|
|
54
|
+
],
|
|
55
|
+
].filter(Boolean),
|
|
56
|
+
};
|
|
19
57
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// babel-loader's `customize` hook, vendored from babel-preset-react-app/webpack-overrides
|
|
2
|
+
// when that package was dropped (see babel-preset-extended.js). Unchanged in behaviour: it
|
|
3
|
+
// only exists to defeat caching for files that use Babel macros.
|
|
4
|
+
const crypto = require('node:crypto');
|
|
5
|
+
|
|
6
|
+
const macroCheck = /[./]macro/;
|
|
7
|
+
|
|
8
|
+
module.exports = () => ({
|
|
9
|
+
// Transforms the Babel configuration per file.
|
|
10
|
+
config(config, { source }) {
|
|
11
|
+
// Macros are effectively uncacheable (babel/babel#8497). Detect them by package suffix
|
|
12
|
+
// and add a random token to `caller` -- a valid Babel option -- so the file gets a
|
|
13
|
+
// one-time cache identifier. Loader options cannot be varied per file.
|
|
14
|
+
if (macroCheck.test(source)) {
|
|
15
|
+
return {
|
|
16
|
+
...config.options,
|
|
17
|
+
caller: {
|
|
18
|
+
...config.options.caller,
|
|
19
|
+
craInvalidationToken: crypto.randomBytes(32).toString('hex'),
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return config.options;
|
|
24
|
+
},
|
|
25
|
+
});
|
package/config/env.js
CHANGED
package/config/getHttpsConfig.js
CHANGED
package/config/modules.js
CHANGED
|
@@ -1,10 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
const fs = require('node:fs');
|
|
4
2
|
const path = require('node:path');
|
|
5
3
|
const paths = require('./paths');
|
|
6
4
|
const chalk = require('react-dev-utils/chalk');
|
|
7
|
-
const
|
|
5
|
+
const { parse: parseJsoncText, printParseErrorCode } = require('jsonc-parser');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Parse a tsconfig.json. It is JSONC -- comments and trailing commas are allowed and
|
|
9
|
+
* common -- so JSON.parse is not enough, which is why this used to go through
|
|
10
|
+
* TypeScript's `readConfigFile`.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} text
|
|
13
|
+
* @param {string} filePath only used to point at the file when it does not parse
|
|
14
|
+
*/
|
|
15
|
+
function parseJsonc(text, filePath) {
|
|
16
|
+
const errors = [];
|
|
17
|
+
const value = parseJsoncText(text, errors, { allowTrailingComma: true });
|
|
18
|
+
|
|
19
|
+
if (errors.length > 0) {
|
|
20
|
+
const [{ error, offset }] = errors;
|
|
21
|
+
const line = text.slice(0, offset).split('\n').length;
|
|
22
|
+
throw new Error(`Failed to parse ${filePath} (line ${line}): ${printParseErrorCode(error)}.`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The `baseUrl` a compilerOptions object asks for, including the spelling TypeScript 7
|
|
30
|
+
* requires.
|
|
31
|
+
*
|
|
32
|
+
* 7 removed `baseUrl` outright -- `tsc` fails with TS5102 -- and points you at
|
|
33
|
+
* `"paths": { "*": ["./*"] }` instead. The three functions below only ever read `baseUrl`,
|
|
34
|
+
* so a project that follows that advice silently loses its `src` alias and its absolute
|
|
35
|
+
* imports stop resolving in webpack. A single `*` mapping to one `<prefix>/*` target is
|
|
36
|
+
* exactly what `baseUrl: <prefix>` expressed, so translate it back rather than making every
|
|
37
|
+
* consumer choose between type checking and resolution.
|
|
38
|
+
*
|
|
39
|
+
* @param {Object} options
|
|
40
|
+
* @returns {string | undefined}
|
|
41
|
+
*/
|
|
42
|
+
function getBaseUrl(options = {}) {
|
|
43
|
+
if (options.baseUrl) {
|
|
44
|
+
return options.baseUrl;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const starTargets = options.paths?.['*'];
|
|
48
|
+
if (!Array.isArray(starTargets)) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const target = starTargets.find((entry) => typeof entry === 'string' && entry.endsWith('/*'));
|
|
53
|
+
// './*' -> './' (the project root), './src/*' -> './src'
|
|
54
|
+
return target ? target.slice(0, -2) || './' : undefined;
|
|
55
|
+
}
|
|
8
56
|
|
|
9
57
|
/**
|
|
10
58
|
* Get additional module paths based on the baseUrl of a compilerOptions object.
|
|
@@ -12,7 +60,7 @@ const resolve = require('resolve');
|
|
|
12
60
|
* @param {Object} options
|
|
13
61
|
*/
|
|
14
62
|
function getAdditionalModulePaths(options = {}) {
|
|
15
|
-
const baseUrl = options
|
|
63
|
+
const baseUrl = getBaseUrl(options);
|
|
16
64
|
|
|
17
65
|
if (!baseUrl) {
|
|
18
66
|
return '';
|
|
@@ -55,7 +103,7 @@ function getAdditionalModulePaths(options = {}) {
|
|
|
55
103
|
* @param {*} options
|
|
56
104
|
*/
|
|
57
105
|
function getWebpackAliases(options = {}) {
|
|
58
|
-
const baseUrl = options
|
|
106
|
+
const baseUrl = getBaseUrl(options);
|
|
59
107
|
|
|
60
108
|
if (!baseUrl) {
|
|
61
109
|
return {};
|
|
@@ -76,7 +124,7 @@ function getWebpackAliases(options = {}) {
|
|
|
76
124
|
* @param {*} options
|
|
77
125
|
*/
|
|
78
126
|
function getJestAliases(options = {}) {
|
|
79
|
-
const baseUrl = options
|
|
127
|
+
const baseUrl = getBaseUrl(options);
|
|
80
128
|
|
|
81
129
|
if (!baseUrl) {
|
|
82
130
|
return {};
|
|
@@ -108,12 +156,12 @@ function getModules() {
|
|
|
108
156
|
// TypeScript project and set up the config
|
|
109
157
|
// based on tsconfig.json
|
|
110
158
|
if (hasTsConfig) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
config =
|
|
159
|
+
// This used to load the app's TypeScript and call `ts.readConfigFile`, which is only a
|
|
160
|
+
// JSONC parse -- the result is read for `compilerOptions.baseUrl` a few lines down and
|
|
161
|
+
// nothing else. Doing it without the compiler is what lets an app run TypeScript 7,
|
|
162
|
+
// whose export map ships no `require`-able entry. `readConfigFile` does not follow
|
|
163
|
+
// `extends` either, so parsing the file directly sees exactly what it saw.
|
|
164
|
+
config = parseJsonc(fs.readFileSync(paths.appTsConfig, 'utf8'), paths.appTsConfig);
|
|
117
165
|
// Otherwise we'll check if there is jsconfig.json
|
|
118
166
|
// for non TS projects.
|
|
119
167
|
} else if (hasJsConfig) {
|
package/config/paths.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
const path = require('node:path');
|
|
4
2
|
const fs = require('node:fs');
|
|
5
3
|
const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
|
|
@@ -38,13 +36,24 @@ const resolveModule = (resolveFn, filePath) => {
|
|
|
38
36
|
};
|
|
39
37
|
|
|
40
38
|
// config after eject: we're in ./config/
|
|
39
|
+
// A CommonJS webpack config inside a "type": "module" package must be named
|
|
40
|
+
// .cjs. Prefer that, falling back to .js so existing consumers keep working.
|
|
41
|
+
const resolveWebpackSetup = (resolver, prefix = '') => {
|
|
42
|
+
const cjs = resolver(`${prefix}webpack.config.cjs`);
|
|
43
|
+
return fs.existsSync(cjs) ? cjs : resolver(`${prefix}webpack.config.js`);
|
|
44
|
+
};
|
|
45
|
+
|
|
41
46
|
module.exports = {
|
|
42
47
|
dotenv: resolveApp('.env'),
|
|
43
48
|
appPath: resolveApp('.'),
|
|
44
49
|
appBuild: resolveApp(buildPath),
|
|
45
50
|
appManifest: resolveApp(manifestPath),
|
|
46
51
|
appPublic: resolveApp('public'),
|
|
47
|
-
appPublicAlt
|
|
52
|
+
// Read as `paths.appPreviewer` by webpackDevServer.config.js. It was named appPublicAlt
|
|
53
|
+
// in both of these branches and appPreviewer only in the ejected branch below, so for
|
|
54
|
+
// every published install the lookup returned undefined and the documented `previewer`
|
|
55
|
+
// override silently did nothing.
|
|
56
|
+
appPreviewer: resolveApp('previewer'),
|
|
48
57
|
appHtml: resolveApp('public/index.html'),
|
|
49
58
|
appIndexJs: resolveModule(resolveApp, 'src/index'),
|
|
50
59
|
appPackageJson: resolveApp('package.json'),
|
|
@@ -54,7 +63,7 @@ module.exports = {
|
|
|
54
63
|
yarnLockFile: resolveApp('yarn.lock'),
|
|
55
64
|
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
|
|
56
65
|
proxySetup: resolveApp('src/setupProxy.js'),
|
|
57
|
-
webpackSetup: resolveApp
|
|
66
|
+
webpackSetup: resolveWebpackSetup(resolveApp),
|
|
58
67
|
appNodeModules: resolveApp('node_modules'),
|
|
59
68
|
appWebpackCache: resolveApp('node_modules/.cache'),
|
|
60
69
|
appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
|
|
@@ -72,7 +81,11 @@ module.exports = {
|
|
|
72
81
|
appBuild: resolveApp(buildPath),
|
|
73
82
|
appManifest: resolveApp(manifestPath),
|
|
74
83
|
appPublic: resolveApp('public'),
|
|
75
|
-
appPublicAlt
|
|
84
|
+
// Read as `paths.appPreviewer` by webpackDevServer.config.js. It was named appPublicAlt
|
|
85
|
+
// in both of these branches and appPreviewer only in the ejected branch below, so for
|
|
86
|
+
// every published install the lookup returned undefined and the documented `previewer`
|
|
87
|
+
// override silently did nothing.
|
|
88
|
+
appPreviewer: resolveApp('previewer'),
|
|
76
89
|
appHtml: resolveApp('public/index.html'),
|
|
77
90
|
appIndexJs: resolveModule(resolveApp, 'src/index'),
|
|
78
91
|
appPackageJson: resolveApp('package.json'),
|
|
@@ -82,7 +95,7 @@ module.exports = {
|
|
|
82
95
|
yarnLockFile: resolveApp('yarn.lock'),
|
|
83
96
|
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
|
|
84
97
|
proxySetup: resolveApp('src/setupProxy.js'),
|
|
85
|
-
webpackSetup: resolveApp
|
|
98
|
+
webpackSetup: resolveWebpackSetup(resolveApp),
|
|
86
99
|
appNodeModules: resolveApp('node_modules'),
|
|
87
100
|
appWebpackCache: resolveApp('node_modules/.cache'),
|
|
88
101
|
appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
|
|
@@ -118,7 +131,7 @@ if (!reactScriptsLinked && __dirname.indexOf(path.join('packages', 'react-unity-
|
|
|
118
131
|
yarnLockFile: resolveOwn(`${templatePath}/yarn.lock`),
|
|
119
132
|
testsSetup: resolveModule(resolveOwn, `${templatePath}/src/setupTests`),
|
|
120
133
|
proxySetup: resolveOwn(`${templatePath}/src/setupProxy.js`),
|
|
121
|
-
webpackSetup: resolveApp
|
|
134
|
+
webpackSetup: resolveWebpackSetup(resolveApp, `${templatePath}/`),
|
|
122
135
|
appNodeModules: resolveOwn('node_modules'),
|
|
123
136
|
appWebpackCache: resolveOwn('node_modules/.cache'),
|
|
124
137
|
appTsBuildInfoFile: resolveOwn('node_modules/.cache/tsconfig.tsbuildinfo'),
|
package/config/public/index.html
CHANGED
|
@@ -6,73 +6,162 @@
|
|
|
6
6
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
7
7
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
8
8
|
|
|
9
|
-
<title>ReactUnity</title>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
<title>ReactUnity Previewer</title>
|
|
10
|
+
<link rel="icon" href="./favicon.ico">
|
|
11
|
+
|
|
12
|
+
<style>
|
|
13
|
+
html,
|
|
14
|
+
body {
|
|
15
|
+
font-size: 0;
|
|
16
|
+
margin: 0;
|
|
17
|
+
height: 100%;
|
|
18
|
+
overflow: hidden;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#unity-canvas {
|
|
22
|
+
height: 100%;
|
|
23
|
+
width: 100%;
|
|
24
|
+
background: #231F20;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.loader-center {
|
|
28
|
+
position: absolute;
|
|
29
|
+
left: 0;
|
|
30
|
+
right: 0;
|
|
31
|
+
top: 50%;
|
|
32
|
+
height: 0;
|
|
33
|
+
overflow: visible;
|
|
34
|
+
display: block;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
user-select: none;
|
|
37
|
+
-webkit-user-select: none;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.loader {
|
|
41
|
+
position: absolute;
|
|
42
|
+
height: 50px;
|
|
43
|
+
top: -25px;
|
|
44
|
+
left: 50px;
|
|
45
|
+
right: 50px;
|
|
46
|
+
background: #c5cae9;
|
|
47
|
+
border-radius: 10px;
|
|
48
|
+
overflow: hidden;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.loader-bar {
|
|
52
|
+
height: 100%;
|
|
53
|
+
background: #3f51b5;
|
|
54
|
+
width: 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#error {
|
|
58
|
+
display: none;
|
|
59
|
+
position: absolute;
|
|
60
|
+
inset: 0;
|
|
61
|
+
overflow: auto;
|
|
62
|
+
padding: 3rem 1.5rem;
|
|
63
|
+
background: #fff;
|
|
64
|
+
color: #231F20;
|
|
65
|
+
font: 16px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#error>div {
|
|
69
|
+
max-width: 40rem;
|
|
70
|
+
margin: 0 auto;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
#error h1 {
|
|
74
|
+
font-size: 1.5rem;
|
|
75
|
+
margin: 0 0 1rem;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#error code {
|
|
79
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
80
|
+
background: #f1f1f4;
|
|
81
|
+
padding: .1em .3em;
|
|
82
|
+
border-radius: 3px;
|
|
83
|
+
}
|
|
84
|
+
</style>
|
|
13
85
|
</head>
|
|
14
86
|
|
|
15
87
|
<body>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
<
|
|
26
|
-
<
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
</p>
|
|
31
|
-
|
|
32
|
-
<p class="fs-5 col-md-8">
|
|
33
|
-
If your UI is simple, you can also test your UI inside this page with a WebGL template. There are 2 ways you can
|
|
34
|
-
do that.
|
|
88
|
+
<canvas id="unity-canvas"></canvas>
|
|
89
|
+
|
|
90
|
+
<div class="loader-center" id="loader-root">
|
|
91
|
+
<div class="loader">
|
|
92
|
+
<div class="loader-bar" id="loader-bar"></div>
|
|
93
|
+
</div>
|
|
94
|
+
</div>
|
|
95
|
+
|
|
96
|
+
<div id="error">
|
|
97
|
+
<div>
|
|
98
|
+
<h1>Couldn't load the previewer</h1>
|
|
99
|
+
<p>
|
|
100
|
+
The previewer's Unity WebGL build is hosted rather than bundled, so the first load needs
|
|
101
|
+
network access. It is cached by the browser afterwards.
|
|
35
102
|
</p>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
<path
|
|
42
|
-
d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z" />
|
|
43
|
-
</svg>
|
|
44
|
-
<div class="fs-5">
|
|
45
|
-
Note that as ReactUnity is not optimized for WebGL, you will have a low performance by testing in WebGL.
|
|
46
|
-
</div>
|
|
47
|
-
</div>
|
|
48
|
-
|
|
49
|
-
<h4>1 - Install the default previewer</h4>
|
|
50
|
-
|
|
51
|
-
<p class="fs-5 col-md-8">
|
|
52
|
-
You can install the <code>@reactunity/previewer</code> package for this.
|
|
103
|
+
<p>
|
|
104
|
+
To work offline, or to preview your own Unity scene instead, build a scene with a
|
|
105
|
+
<code>ReactRendererUGUI</code> component and copy the output into a <code>previewer</code>
|
|
106
|
+
folder in your React project, so that <code>previewer/index.html</code> exists. The dev
|
|
107
|
+
server prefers that over this page.
|
|
53
108
|
</p>
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
109
|
+
<p><a href="./info.html">More about the previewer and the dev server</a></p>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
<script>
|
|
114
|
+
/*
|
|
115
|
+
* The Unity build lives on reactunity.github.io rather than in an npm package: it is
|
|
116
|
+
* ~107 MB, and shipping it as a dependency meant every install paid for it. GitHub
|
|
117
|
+
* Pages serves it with `Content-Type: application/wasm` and `Access-Control-Allow-Origin: *`,
|
|
118
|
+
* so streaming compilation works cross-origin from this dev server.
|
|
119
|
+
*
|
|
120
|
+
* The version is part of the path on purpose. Unity's loader, framework and wasm are
|
|
121
|
+
* built as a set and are not interchangeable, so each build gets its own directory and
|
|
122
|
+
* nothing is ever overwritten. Publishing a new previewer build means adding a directory
|
|
123
|
+
* on that branch and bumping the version here -- those two have to move together.
|
|
124
|
+
*/
|
|
125
|
+
var PREVIEWER_VERSION = '0.20.0';
|
|
126
|
+
var PREVIEWER_BASE = 'https://reactunity.github.io/Unity/previewer/' + PREVIEWER_VERSION + '/Build/';
|
|
127
|
+
|
|
128
|
+
function fail() {
|
|
129
|
+
document.getElementById('loader-root').style.display = 'none';
|
|
130
|
+
document.getElementById('error').style.display = 'block';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function onProgress(progress) {
|
|
134
|
+
var bar = document.getElementById('loader-bar');
|
|
135
|
+
if (bar) bar.style.width = (progress * 100) + '%';
|
|
136
|
+
|
|
137
|
+
var loader = document.getElementById('loader-root');
|
|
138
|
+
if (loader) loader.style.display = progress < 1 ? 'block' : 'none';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* Unity's loader is a plain script defining a global `createUnityInstance`, so it has to
|
|
143
|
+
* be appended rather than imported.
|
|
144
|
+
*/
|
|
145
|
+
var loader = document.createElement('script');
|
|
146
|
+
loader.src = PREVIEWER_BASE + 'Previewer.loader.js';
|
|
147
|
+
loader.onerror = fail;
|
|
148
|
+
loader.onload = () => {
|
|
149
|
+
if (typeof createUnityInstance !== 'function') return fail();
|
|
150
|
+
|
|
151
|
+
createUnityInstance(document.getElementById('unity-canvas'), {
|
|
152
|
+
dataUrl: PREVIEWER_BASE + 'Previewer.data',
|
|
153
|
+
frameworkUrl: PREVIEWER_BASE + 'Previewer.framework.js',
|
|
154
|
+
codeUrl: PREVIEWER_BASE + 'Previewer.wasm',
|
|
155
|
+
// Absolute like the rest. Left relative it would resolve against this dev server,
|
|
156
|
+
// which has no StreamingAssets to serve.
|
|
157
|
+
streamingAssetsUrl: PREVIEWER_BASE + '../StreamingAssets',
|
|
158
|
+
companyName: 'reactunity',
|
|
159
|
+
productName: 'dev-sample',
|
|
160
|
+
productVersion: PREVIEWER_VERSION,
|
|
161
|
+
}, onProgress).catch(fail);
|
|
162
|
+
};
|
|
163
|
+
document.head.appendChild(loader);
|
|
164
|
+
</script>
|
|
76
165
|
</body>
|
|
77
166
|
|
|
78
167
|
</html>
|