Skip to content

Commit 7be81d4

Browse files
author
Philipp Molitor
authored
Merge pull request #13 from PhilippMolitor/dev
release 2020.1.0
2 parents 3150bff + 6ad1847 commit 7be81d4

File tree

9 files changed

+292
-34
lines changed

9 files changed

+292
-34
lines changed

.github/workflows/ci-dev.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Build/CI
2+
3+
on:
4+
push:
5+
branches: ['dev']
6+
pull_request:
7+
branches: ['dev']
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node-version: [14.x]
15+
steps:
16+
- name: checkout
17+
uses: actions/checkout@v2
18+
19+
- name: install node ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
24+
- name: cache node_modules
25+
uses: actions/cache@v2
26+
with:
27+
path: '**/node_modules'
28+
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
29+
30+
- run: yarn --frozen-lockfile
31+
- run: yarn test
32+
- run: yarn build

.github/workflows/release-npmjs.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types:
6+
- released
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
node-version: [14.x]
14+
steps:
15+
- name: checkout
16+
uses: actions/checkout@v2
17+
18+
- name: install node ${{ matrix.node-version }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
23+
- name: cache node_modules
24+
uses: actions/cache@v2
25+
with:
26+
path: '**/node_modules'
27+
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
28+
29+
- run: yarn --frozen-lockfile
30+
- run: yarn test
31+
- run: yarn build
32+
- run: yarn publish --access public
33+
env:
34+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# IDEs
22
.vscode/
3+
.github/
34

45
# node
56
node_modules/

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
<p align="center">
44

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-
<img src="https://img.shields.io/bundlephobia/minzip/react-unity-renderer?style=flat-square">
5+
<img src="https://github.com/PhilippMolitor/react-unity-renderer/actions/workflows/ci-dev.yaml/badge.svg?branch=dev">
6+
<img src="https://github.com/PhilippMolitor/react-unity-renderer/actions/workflows/release-npmjs.yaml/badge.svg?event=release">
7+
<img src="https://img.shields.io/npm/l/react-unity-renderer">
8+
<img src="https://img.shields.io/npm/dw/react-unity-renderer">
9+
<img src="https://img.shields.io/github/stars/PhilippMolitor/react-unity-renderer">
10+
<img src="https://img.shields.io/npm/v/react-unity-renderer">
11+
<img src="https://img.shields.io/bundlephobia/minzip/react-unity-renderer">
1012

1113
</p>
1214

@@ -75,6 +77,11 @@ export const UnityGameComponent: VFC = (): JSX.Element => {
7577
const [progress, setProgress] = useState<number>(0);
7678
const [ready, setReady] = useState<boolean>(false);
7779

80+
// Attach some event handlers to the context
81+
useEffect(() => {
82+
ctx.on('message', (m: string) => console.log(message));
83+
}, []);
84+
7885
return (
7986
<UnityRenderer
8087
context={ctx}
@@ -182,6 +189,24 @@ export async function fetchLoaderConfig(
182189

183190
You can then use it to construct a `UnityContext` and pass this context to your `UnityRenderer` via the `context` prop.
184191

192+
## Sending events from Unity
193+
194+
In order to send events from Unity to the react application, use the global method for that in your `*.jslib` mapping file:
195+
196+
```javascript
197+
mergeInto(LibraryManager.library, {
198+
RunSomeActionInJavaScript: function (message, number) {
199+
// surround with try/catch to make unity not crash in case the method is
200+
// not defined in the global scope yet
201+
try {
202+
window.UnityBridge('event-name')(Pointer_stringify(message), number);
203+
} catch (e) {}
204+
},
205+
});
206+
```
207+
208+
If the event name has no registered event handlers, the `UnityBridge(event: string)` function will log a warning via `console.warn(...)`.
209+
185210
## Module augmentation
186211

187212
Take the following example:

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": "2020.0.3",
3+
"version": "2020.1.0",
44
"description": "React Unity Renderer allows to interactively embed Unity WebGL builds into a React powered project.",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/components/__tests__/UnityRenderer.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { mount, ReactWrapper } from 'enzyme';
22

3-
import { UnityContext } from '../../lib/context';
43
import { UnityRenderer } from '../UnityRenderer';
54

65
describe('<UnityRenderer /> (unconfigured)', () => {

src/lib/__tests__/context.test.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path="../../../typings/unity.d.ts" />
2+
13
import { UnityContext, UnityLoaderConfig } from '../context';
24

35
describe('UnityContext', () => {
@@ -15,9 +17,95 @@ describe('UnityContext', () => {
1517
productVersion: 'k',
1618
};
1719

20+
beforeEach(async () => {
21+
// @ts-ignore
22+
delete window.__UnityBridgeRegistry__;
23+
// @ts-ignore
24+
delete window.UnityBridge;
25+
});
26+
27+
afterEach(() => {
28+
jest.resetAllMocks();
29+
});
30+
1831
it('stores and retrieves the loader configuration', async () => {
1932
const ctx = new UnityContext(cfg);
2033

21-
expect(ctx.getConfig()).toEqual(cfg);
34+
expect(ctx.getConfig()).toStrictEqual(cfg);
35+
});
36+
37+
it('creates the global event registry', async () => {
38+
expect(window.__UnityBridgeRegistry__).toBe(undefined);
39+
40+
new UnityContext(cfg);
41+
42+
expect(window.__UnityBridgeRegistry__).toStrictEqual({});
43+
});
44+
45+
it('creates the global event lookup handler', async () => {
46+
expect(window.UnityBridge).toBe(undefined);
47+
48+
new UnityContext(cfg);
49+
50+
expect(typeof window.UnityBridge).toBe('function');
51+
});
52+
53+
it('registers an event handler to the global registry', async () => {
54+
const ctx = new UnityContext(cfg);
55+
const handler = jest.fn();
56+
57+
ctx.on('test', handler);
58+
expect(window.__UnityBridgeRegistry__).toStrictEqual({ test: [handler] });
59+
expect(handler).not.toHaveBeenCalled();
60+
61+
window.UnityBridge('test')('string', 42);
62+
expect(handler).toHaveBeenCalledWith('string', 42);
63+
});
64+
65+
it('logs a warning when calling an unknown event', async () => {
66+
new UnityContext(cfg);
67+
68+
let message: string = '';
69+
const consoleWarn = jest
70+
.spyOn(console, 'warn')
71+
.mockImplementation((m: string) => (message = m));
72+
73+
expect(consoleWarn).not.toHaveBeenCalled();
74+
window.UnityBridge('test')(42, test);
75+
76+
expect(consoleWarn).toHaveBeenCalled();
77+
expect(message).toMatch(/\"test\"/);
78+
});
79+
80+
it('unregisters a previously registered event', async () => {
81+
const ctx = new UnityContext(cfg);
82+
expect(window.__UnityBridgeRegistry__).toStrictEqual({});
83+
84+
// add handler
85+
const handler = jest.fn();
86+
ctx.on('test', handler);
87+
expect(window.__UnityBridgeRegistry__).toStrictEqual({ test: [handler] });
88+
89+
// remove it again
90+
ctx.off('test');
91+
expect(window.__UnityBridgeRegistry__).toStrictEqual({ test: [] });
92+
});
93+
94+
it('registers events with the same name for to contexts', async () => {
95+
const ctxA = new UnityContext(cfg);
96+
const ctxB = new UnityContext(cfg);
97+
98+
const handlerA = jest.fn();
99+
const handlerB = jest.fn();
100+
ctxA.on('test-a-b', handlerA);
101+
ctxB.on('test-a-b', handlerB);
102+
103+
window.UnityBridge('test-a-b')('abtest', 42);
104+
expect(handlerA).toHaveBeenCalledWith('abtest', 42);
105+
expect(handlerB).toHaveBeenCalledWith('abtest', 42);
106+
107+
expect(window.__UnityBridgeRegistry__).toStrictEqual({
108+
'test-a-b': [handlerA, handlerB], // order matters here!
109+
});
22110
});
23111
});

0 commit comments

Comments
 (0)