-
-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathUnityContent.ts
More file actions
214 lines (193 loc) · 6.22 KB
/
UnityContent.ts
File metadata and controls
214 lines (193 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import IUnityConfig from "./interfaces/IUnityConfig";
import UnityComponent from "./components/Unity";
import UnityEvents from "./UnityEvents";
import "./declarations/UnityLoader";
import "./declarations/UnityInstance";
import "./declarations/ReactUnityWebGL";
import { loggingService } from "./services/LoggingService";
// event names on which this.triggerUnityEvent() is called
const InstanceEventNames: string[] = [
'error',
'loaded',
'progress',
'quitted',
'resized',
];
export default class UnityContent {
/**
* the relative path to the build json file generated by Unity.
* @type {string}
* @private
*/
public buildJsonPath: string;
/**
* the relative path to the unity loader javascript file.
* @type {string}
* @public
*/
public unityLoaderJsPath: string;
/**
* The Unity component binded to this content.
* @type {UnityComponent}
* @private
*/
private unityComponent?: UnityComponent;
/**
* The Unity instance binded to this content.
* @type {UnityInstance}
* @private
*/
private unityInstance?: UnityInstance;
/**
* the Unity configuration that will be used to start the player.
* @type {IUnityConfig}
* @public
*/
public unityConfig: IUnityConfig;
/**
* The registered instance Unity Events.
* @type {UnityEvents}
* @private
*/
private unityInstanceEvents: UnityEvents;
/**
* The registered global Unity Events associated with ReactUnityWebGL object.
* @type {UnityEvents}
* @private
*/
static unityGlobalEvents: UnityEvents = new UnityEvents();
/**
* The unique ID helps seperating multiple
* Unity player instances in your react
* application.
* @type {number}
* @public
*/
public uniqueID: number;
/**
* the statis unique ID keeps track of the
* unique ID's made by other instances.
* @type {number}
* @static
* @public
*/
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.
* @param {string} unityLoaderJsPath the relative path to the unity loader javascript file.
* @param {IUnityConfig} unityConfig the Unity configuration that will be used to start the player.
*/
constructor(
buildJsonPath: string,
unityLoaderJsPath: string,
unityConfig?: IUnityConfig
) {
const _unityConfig = unityConfig || ({} as IUnityConfig);
this.buildJsonPath = buildJsonPath;
this.unityLoaderJsPath = unityLoaderJsPath;
this.uniqueID = ++UnityContent.uniqueID;
this.unityInstanceEvents = new UnityEvents();
this.unityConfig = {
modules: _unityConfig.modules || {},
unityVersion: _unityConfig.unityVersion || "undefined",
adjustOnWindowResize: _unityConfig.adjustOnWindowResize,
id: _unityConfig.id || "nill"
} as IUnityConfig;
if (typeof (window as any).ReactUnityWebGL === "undefined")
(window as any).ReactUnityWebGL = {};
}
/**
* Binds a unity component to this content.
* @param unityComponentInstance the unity component that will be binded to this content.
* @public
*/
public setComponentInstance(unityComponentInstance: UnityComponent): void {
this.unityComponent = unityComponentInstance;
}
/**
* Binds a unity player to this content.
* @param unityPlayerInstance the unity component that will be binded to this content.
* @public
*/
public setUnityInstance(unityInstance: UnityInstance): void {
this.unityInstance = unityInstance;
}
/**
* Sets the unity players fullscreen mode.
* @param {boolean} fullscreen
* @public
*/
public setFullscreen(fullscreen: boolean): void {
if (this.unityInstance != null) {
this.unityInstance.SetFullscreen(fullscreen === true ? 1 : 0);
}
}
/**
* Quits the Unity Instance and removes it from memory.
*/
public remove(): void {
if (
typeof this.unityInstance !== "undefined" &&
typeof this.unityInstance.Quit === "function"
)
return this.unityInstance.Quit(() => {
this.triggerUnityEvent("quitted");
this.unityInstance = undefined;
});
return loggingService.warnUnityContentRemoveNotAvailable();
}
/**
* Sends an event to the Unity player that will trigger a function.
* @param {string} gameObjectName the name of the game object in your Unity scene.
* @param {string} methodName the name of the public method on the game object.
* @param {any} parameter an optional parameter to pass along to the method.
* @public
*/
public send(
gameObjectName: string,
methodName: string,
parameter?: any
): void {
if (this.unityInstance != null) {
if (typeof parameter === "undefined") {
this.unityInstance.SendMessage(gameObjectName, methodName);
} else {
this.unityInstance.SendMessage(gameObjectName, methodName, parameter);
}
}
}
/**
* Registers an event listener for the Unity player. These can be
* system events like when the player is initialized or loader and
* your custom events from Unity.
* @param {string} eventName the event name
* @param {Function} eventCallback the event function
* @returns {any} The Function
* @public
*/
public on(eventName: string, eventCallback: Function): any {
if (InstanceEventNames.find(name => name === eventName)) {
// instance event - add to instance events
this.unityInstanceEvents.AddEventListener(eventName, eventCallback);
} else {
// ReactUnityWebGL event - add to class events
UnityContent.unityGlobalEvents.AddEventListener(eventName, eventCallback);
// install global event handler (if necessary)
if (typeof (window as any).ReactUnityWebGL[eventName] === 'undefined') {
(window as any).ReactUnityWebGL[eventName] = (parameter: any) =>
UnityContent.unityGlobalEvents.DispatchEvent(eventName, parameter);
}
}
}
/**
* Triggers an event that has been registered by the on
* function.
* @param {string} eventName the event name
* @param {Function} eventValue the event value
* @public
*/
public triggerUnityEvent(eventName: string, eventValue?: any): void {
this.unityInstanceEvents.DispatchEvent(eventName, eventValue);
}
}