Skip to content

Commit 529a36e

Browse files
Add sendMessage method to useUnityContext for invoking public methods on Unity instances
1 parent 04ae1f3 commit 529a36e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

module/source/hooks/use-unity-context.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UnityConfig } from "../types/unity-config";
33
import { UnityContext } from "../types/unity-context";
44
import { UnityProvider } from "../types/unity-provider";
55
import { UnityInstance } from "../types/unity-instance";
6+
import { UnityEventParameter } from "../exports";
67

78
/**
89
* Custom hook to create a Unity context.
@@ -58,6 +59,18 @@ const useUnityContext = (unityConfig: UnityConfig): UnityContext => {
5859
unityInstance?.Module.canvas?.requestPointerLock();
5960
}, [unityInstance]);
6061

62+
/**
63+
* Sends a message to the Unity Instance to invoke a public method.
64+
*/
65+
const sendMessage = useCallback(
66+
(
67+
gameObjectName: string,
68+
methodName: string,
69+
parameter?: UnityEventParameter
70+
) => unityInstance?.SendMessage(gameObjectName, methodName, parameter),
71+
[unityInstance]
72+
);
73+
6174
// Initialize the UnityProvider with the provided configuration
6275
// This is where you would typically load the Unity instance
6376
// and set up event listeners, etc.
@@ -68,6 +81,7 @@ const useUnityContext = (unityConfig: UnityConfig): UnityContext => {
6881
initialisationError,
6982
requestFullscreen,
7083
requestPointerLock,
84+
sendMessage,
7185
UNSAFE__unityInstance: unityInstance,
7286
};
7387
};

0 commit comments

Comments
 (0)