The plugin that allows you to embed a UNITY project into the react native as a full-fledged component
ATTENTION!
The plugin now supports the new architecture as well.
For iOS, it is no longer necessary to embed a project created with Unity. Only the built UnityFramework.framework is used. It should be placed in the plugin folder at the path -
node_modules/@azesmway/react-native-unity/ios/
or change the path in the react-native-unity.podspec
file.
s.vendored_frameworks = ["ios/UnityFramework.framework"].
npm install @azesmway/react-native-unity
or
yarn add @azesmway/react-native-unity
- Copy from folder "unity" to <Unity_Project_Name> folder and rebuild unity project.
Add this code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using UnityEngine;
public class NativeAPI {
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
public static extern void sendMessageToMobileApp(string message);
#endif
}
public class ButtonBehavior : MonoBehaviour
{
public void ButtonPressed()
{
if (Application.platform == RuntimePlatform.Android)
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.azesmwayreactnativeunity.ReactNativeUnityViewManager"))
{
jc.CallStatic("sendMessageToMobileApp", "The button has been tapped!");
}
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
#if UNITY_IOS && !UNITY_EDITOR
NativeAPI.sendMessageToMobileApp("The button has been tapped!");
#endif
}
}
}
- Build Unity app
- Open the created project in XCode
- Select Data folder and set a checkbox in the "Target Membership" section to "UnityFramework"
- You need to select the NativeCallProxy.h inside the
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 required - sign the project
UnityFramework.framework
and build a framework - Open the folder with the built framework (by right-clicking) and move it to the plugin folder (
node_modules/@azesmway/react-native-unity/ios/
)
-
Export Unity app to
[project_root]/unity/builds/android
-
Add the following lines to
android/settings.gradle
:include ':unityLibrary' project(':unityLibrary').projectDir=new File('..\\unity\\builds\\android\\unityLibrary')
-
Add into
android/build.gradle
allprojects { repositories { // this flatDir { dirs "${project(':unityLibrary').projectDir}/libs" } // ...
-
Add into
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>
-
Remove
<intent-filter>...</intent-filter>
from<project_name>/unity/builds/android/unityLibrary/src/main/AndroidManifest.xml
at unityLibrary to leave only integrated version.
- Works only on real iOS devices. Android emulators are capable of showing the UnityView.
- On IOS the Unity view is waiting for a parent with dimensions greater than 0 (from RN side). Please take care of this because if it is not the case, your app will crash with the native message
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;
androidKeepPlayerMounted?: 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. FOR ANDROID: has no effect on iOS.fullScreen?: boolean
- defaults to true. If set to false, will not request full screen access. ANDROID ONLYonUnityMessage?: (event: NativeSyntheticEvent)
- receives a message from a Unitystyle: ViewStyle
- styles the UnityView. (Won't show on Android without dimensions. Recommended to give itflex: 1
as in the example)
-
postMessage(gameObject, methodName, message)
- sends a message to the Unity. FOR IOS: The native method of unity is used to send a messagesendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg;
, more details can be found in the documentation -
unloadUnity()
- the Unity is unloaded automatically when the react-native component is unmounted, but if you want to unload the Unity, you can call this method -
pauseUnity?: (pause: boolean)
- pause the Unity -
windowFocusChanged(hasFocus: boolean = false)
- simulate focus change (intended to be used to recover from black screen (not rendering) after remounting Unity view whenresumeUnity
does not work) ANDROID ONLY
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT