@azesmway/react-native-unity 这个插件允许您将 Unity 项目嵌入到 React Native 中,作为一款完整的组件。该插件 ...
@azesmway/react-native-unity 这个插件允许您将 Unity 项目嵌入到 React Native 中,作为一款完整的组件。该插件 ...
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
UnityFrameworkis used. It should be placed in the plugin folder at the path -/unity/builds/ios
| Platform | Supported |
|---|---|
| iOS Simulator | ⚠️ optional (see iOS Simulator support) |
| iOS Device | ✅ |
| Android Emulator | ✅ |
| Android Device | ✅ |
The iOS Simulator works only with the optional XCFramework export described below; the default device-only
UnityFramework.frameworkdoes not link on the simulator.
npm install @azesmway/react-native-unity
or
yarn add @azesmway/react-native-unityCopy 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.
ButtonBehavior.cs
…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 /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/unity/builds/android... from /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.
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=.unity3dAdd strings to android/app/src/main/res/values/strings.xml
Game viewIf your Unity project uses AR — AR Foundation / ARCore — add this permission to android/app/src/main/AndroidManifest.xml. This applies to expo users too: the config plugin does not add it, so declare it through android.permissions in app.json.
Since Android 12 (API 31), an app targeting API 31+ needs this permission to read sensors faster than 200 Hz. ARCore requests the IMU at its maximum rate — 500 Hz on many devices — so without it the sensor registration is rejected, the ARCore session never starts and the camera stays black. In logcat it looks like this:
Failed to register sensor to queue 0
...
ArPresto::Moving from ArPrestoStatus 102 to 200
operator(): width ` can hide the system navigation bar for the whole app (immersive sticky mode — the bar reappears on swipe, then hides again). The `fullScreen` prop only controls the status bar flag; change `Fullscreen Mode` / `Status Bar Hidden` in Unity's Player Settings, or set `Screen.fullScreen = false` in your scene.
# Usage
## Sample code
```jsx
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(null);
useEffect(() => {
if (unityRef?.current) {
const message: IMessage = {
gameObject: 'gameObject',
methodName: 'methodName',
message: 'message',
};
unityRef.current.postMessage(
message.gameObject,
message.methodName,
message.message
);
}
}, []);
return (
{
console.log('onUnityMessage', result.nativeEvent.message);
}}
/>
);
};
export default Unity;
…
sh
xcodebuild -project device-export/Unity-iPhone.xcodeproj -scheme UnityFramework \
-configuration Release -sdk iphoneos BUILD_DIR="$PWD/device-build" build
xcodebuild -project simulator-export/Unity-iPhone.xcodeproj -scheme UnityFramework \
-configuration Release -sdk iphonesimulator BUILD_DIR="$PWD/simulator-build" build
xcodebuild -create-xcframework \
-framework device-build/Release-iphoneos/UnityFramework.framework \
-framework simulator-build/Release-iphonesimulator/UnityFramework.framework \
-output /unity/builds/ios/UnityFramework.xcframework暂无开放 Issues,或尚未同步最近议题。