The plugin that allows you to embed a Unity project into React Native as a full-fledged component. The plugin now supports the new architecture.
Attention! Added support for Unity 2023 and above
[!IMPORTANT] For iOS, it is no longer necessary to embed a project created with Unity. Only the built
UnityFramework
is used. It should be placed in the plugin folder at the path -<YOUR_RN_PROJECT>/unity/builds/ios
Platform | Supported |
---|---|
iOS Simulator | ❌ |
iOS Device | ✅ |
Android Emulator | ✅ |
Android Device | ✅ |
npm install @azesmway/react-native-unity
or
yarn add @azesmway/react-native-unity
Copy the contents of the folder unity
to the root of your Unity project. This folder contains the necessary scripts and settings for the Unity project. You can find these files in your react-native project under node_modules/@azesmway/react-native-unity/unity
. This is necessary to ensure iOS has access to the NativeCallProxy
class from this library.
(optional) If you're following along with the example, you can add the following code to the ButtonBehavior.cs
script in your Unity project. This allows the button press in Unity to communicate with your react-native app.
After you've moved the files from the unity
folder to your Unity project, you can export the iOS unity project by following these steps:
Unity-iPhone/Libraries/Plugins/iOS
folder of the Unity-iPhone project and change UnityFramework’s target membership from Project to Public. Don’t forget this step! (if you don't see these files in your Xcode project, you didn't copy over the unity
folder to your Unity project correctly in previous steps) UnityFramework.framework
and build a framework <YOUR_RN_PROJECT>/unity/builds/ios
) Pods
cache and lockfile with this command in the root of the main RN project rm -rf ios/Pods && rm -f ios/Podfile.lock && npx pod-install
<YOUR_RN_PROJECT>/unity/builds/android
<intent-filter>...</intent-filter>
from <YOUR_RN_PROJECT>/unity/builds/android/unityLibrary/src/main/AndroidManifest.xml
at unityLibrary to leave only integrated version.If you're using expo, you're done. The built-in expo plugin will handle the rest. If you're not using expo, you'll need to follow the steps below.
android/settings.gradle
:
include ':unityLibrary'
project(':unityLibrary').projectDir=new File('..\\unity\\builds\\android\\unityLibrary')
android/build.gradle
allprojects {
repositories {
// this
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
// ...
}
}
android/gradle.properties
unityStreamingAssets=.unity3d
Add strings to android/app/src/main/res/values/strings.xml
<string name="game_view_content_description">Game view</string>
MTLTextureDescriptor has width of zero
.import React, { useRef, useEffect } from 'react';
import UnityView from '@azesmway/react-native-unity';
import { View } from 'react-native';
interface IMessage {
gameObject: string;
methodName: string;
message: string;
}
const Unity = () => {
const unityRef = useRef<UnityView>(null);
useEffect(() => {
if (unityRef?.current) {
const message: IMessage = {
gameObject: 'gameObject',
methodName: 'methodName',
message: 'message',
};
unityRef.current.postMessage(
message.gameObject,
message.methodName,
message.message
);
}
}, []);
return (
<View style={{ flex: 1 }}>
<UnityView
ref={unityRef}
style={{ flex: 1 }}
onUnityMessage={(result) => {
console.log('onUnityMessage', result.nativeEvent.message);
}}
/>
</View>
);
};
export default Unity;
style: ViewStyle
- styles the UnityView. (Won't show on Android without dimensions. Recommended to give it flex: 1
as in the example)onUnityMessage?: (event: NativeSyntheticEvent)
- receives a message from a UnityandroidKeepPlayerMounted?: boolean
- if set to true, keep the player mounted even when the view that contains it has lost focus. The player will be paused on blur and resumed on focus. ANDROID ONLYfullScreen?: boolean
- defaults to true. If set to false, will not request full screen access. ANDROID ONLYpostMessage(gameObject, methodName, message)
- sends a message to the Unity. FOR IOS: The native method of unity is used to send a message
sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg;
, more details can be found in the documentationunloadUnity()
- the Unity is unloaded automatically when the react-native component is unmounted, but if you want to unload the Unity, you can call this methodpauseUnity?: (pause: boolean)
- pause the UnitywindowFocusChanged(hasFocus: boolean = false)
- simulate focus change (intended to be used to recover from black screen (not rendering) after remounting Unity view when resumeUnity
does not work) ANDROID ONLYSee the contributing guide to learn how to contribute to the repository and the development workflow.
MIT