11import './global' ;
22import { UnityLoaderConfig } from './interfaces/config' ;
33
4+ export type UnityEventCallback = ( ...params : any ) => void ;
5+
6+ /**
7+ * Defines a Unity WebGL context.
8+ *
9+ * The context is responsible for the initial startup parameters of the
10+ * `UnityRenderer`, as well as receiving events and emitting RPCs to Unity.
11+ *
12+ */
413export class UnityContext {
14+ private config : UnityLoaderConfig ;
15+
516 private instance ?: UnityInstance ;
617
7- private config : UnityLoaderConfig ;
18+ private eventCallbacks : { [ name : string ] : UnityEventCallback } = { } ;
819
20+ /**
21+ * Creates a new `UnityContext` and registers the global event callback.
22+ *
23+ * @param {UnityLoaderConfig } config A loader configuration object.
24+ */
925 constructor ( config : UnityLoaderConfig ) {
1026 this . config = config ;
11- if ( ! window . UnityBridge ) window . UnityBridge = { } ;
27+
28+ // callback is needed so it can access the actual eventCallbacks object
29+ if ( ! window . UnityBridge )
30+ window . UnityBridge = ( name : string ) => this . bridgeCallback ( name ) ;
1231 }
1332
33+ /**
34+ * Retrieves the currently activte loader configuration.
35+ *
36+ * @returns {UnityLoaderConfig } The current loader configuration object.
37+ */
1438 public getConfig ( ) : UnityLoaderConfig {
1539 return this . config ;
1640 }
1741
18- public setInstance ( unityInstance : UnityInstance ) : void {
19- this . instance = unityInstance ;
42+ /**
43+ * Sets the Unity instance this `UnityContext` is responsible for.
44+ *
45+ * @param {UnityInstance } instance The running Unity instance
46+ */
47+ public setInstance ( instance : UnityInstance ) : void {
48+ this . instance = instance ;
2049 }
2150
51+ /**
52+ * Shuts down the running Unity instance, then unregisters the existing
53+ * event handlers.
54+ *
55+ * @param {() => void } onShutdownFinished Callback to execute when the
56+ * shutdown has been completed.
57+ *
58+ * @returns {void } void
59+ */
2260 public shutdown ( onShutdownFinished ?: ( ) => void ) : void {
2361 if ( ! this . instance ) return ;
2462
@@ -36,34 +74,69 @@ export class UnityContext {
3674 ) ;
3775 }
3876
39- public emitUnityEvent (
77+ /**
78+ * Emits a remote procedure call towards the running Unity instance.
79+ *
80+ * @param {string } objectName The `GameObject` on which to call the method.
81+ * @param {string } methodName The name of the method which should be invoked.
82+ * @param {(string | number) } value The value to pass to the method
83+ * as the first parameter.
84+ * @returns {void } void
85+ */
86+ public emit (
4087 objectName : string ,
4188 methodName : string ,
42- value ?: string | number | boolean
89+ value ?: string | number
4390 ) : void {
4491 if ( ! this . instance ) return ;
4592
4693 this . instance . SendMessage ( objectName , methodName , value ) ;
4794 }
4895
49- public on < T extends ( ...params : any ) => void = ( ) => void > (
50- name : string ,
51- callback : T
52- ) : void {
53- window . UnityBridge [ name ] = callback ;
54- }
55-
56- public off ( name : string ) {
57- if ( name in window . UnityBridge ) window . UnityBridge [ name ] = ( ) => undefined ;
96+ /**
97+ * Delegates an event handler to handle an event (from Unity) by using a
98+ * callback function.
99+ *
100+ * @param {string } name The (unique) name of the event.
101+ * @param {UnityEventCallback } callback The callback which should be invoked
102+ * upon the occurence of this event.
103+ */
104+ public on < T extends UnityEventCallback > ( name : string , callback : T ) : void {
105+ this . eventCallbacks [ name ] = callback ;
58106 }
59107
108+ /**
109+ * Enables or disables fullscreen mode.
110+ *
111+ * @param {booolean } enabled Whether to enable or disable fullscreen.
112+ * @returns {void } void
113+ */
60114 public setFullscreen ( enabled : boolean ) : void {
61115 if ( ! this . instance ) return ;
62116 this . instance . SetFullscreen ( enabled ? 1 : 0 ) ;
63117 }
64118
119+ /**
120+ * The internal handler for any incoming event.
121+ * Logs a warning for events with names that are not registered.
122+ *
123+ * @param {string } name The name of the event.
124+ * @returns {UnityEventCallback } The callback which should
125+ * handle the event.
126+ */
127+ private bridgeCallback ( name : string ) : UnityEventCallback {
128+ if ( this . eventCallbacks && this . eventCallbacks [ name ] )
129+ return this . eventCallbacks [ name ] ;
130+
131+ // eslint-disable-next-line no-console
132+ console . warn ( `called event "${ name } " which currently is not registered` ) ;
133+ return ( ) => undefined ;
134+ }
135+
136+ /**
137+ * Unregisters all event handlers.
138+ */
65139 private unregisterAllEventHandlers ( ) {
66- if ( ! window . UnityBridge || typeof window . UnityBridge !== 'object' ) return ;
67- Object . keys ( window . UnityBridge ) . forEach ( ( k ) => this . off ( k ) ) ;
140+ this . eventCallbacks = { } ;
68141 }
69142}
0 commit comments