Skip to content

Commit

Permalink
Merge pull request #44 from jeffreylanters/patch/multiple-instance-ids
Browse files Browse the repository at this point in the history
Patch/multiple instance ids
  • Loading branch information
Jeffrey Lanters authored Jul 2, 2018
2 parents 50e2a33 + c716d76 commit e044f38
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "source/index.js",
"scripts": {
"start": "tsc source/index.ts --declaration --declarationMap --sourceMap --module CommonJS --watch",
"compile": "tsc source/index.ts --declaration --sourceMap --module CommonJS"
"compile": "tsc source/index.ts --declaration --declarationMap --sourceMap --module CommonJS",
"prepublish": "npm run compile"
},
"repository": {
"type": "git",
Expand Down
16 changes: 16 additions & 0 deletions source/UnityContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ export default class UnityContent {
*/
private unityEvents: IUnityEvent[];

/**
* The unique ID helps seperating multiple
* Unity player instances in your react
* application.
* @type {string}
* @public
*/
public uniqueID: number;

/**
* the statis unique ID keeps track of the
* unique ID's made by other instances.
*/
public static uniqueID: number = 0;

/**
* Creates a new Unity content object. This object can be used
* @param {string} buildJsonPath the relative path to the build json file generated by Unity.
Expand All @@ -61,6 +76,7 @@ export default class UnityContent {
const _unityConfig = unityConfig || ({} as IUnityConfig);
this.buildJsonPath = buildJsonPath;
this.unityLoaderJsPath = unityLoaderJsPath;
this.uniqueID = ++UnityContent.uniqueID;
this.unityEvents = [];
this.unityConfig = {
modules: _unityConfig.modules || {},
Expand Down
4 changes: 2 additions & 2 deletions source/components/Unity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class Unity extends React.Component<IUnityProps, IUnityState> {
// prettier-ignore
this.unityLoaderService.append(this.props.unityContent.unityLoaderJsPath, () => {
this.unityContent.setUnityInstance(UnityLoader.instantiate(
"__ReactUnityWebGL",
`__ReactUnityWebGL_${this.props.unityContent.uniqueID}__`,
this.props.unityContent.buildJsonPath, {
onProgress: this.onProgress.bind(this),
Module: this.props.unityContent.unityConfig.modules
Expand All @@ -84,7 +84,7 @@ export default class Unity extends React.Component<IUnityProps, IUnityState> {
return React.createElement("div", {
className: this.props.className || "",
ref: ref => (this.htmlElement = ref!),
id: "__ReactUnityWebGL",
id: `__ReactUnityWebGL_${this.props.unityContent.uniqueID}__`,
style: {
width: this.props.width || "800px",
height: this.props.height || "600px"
Expand Down

0 comments on commit e044f38

Please sign in to comment.