Integration with Unity is implemented by https://github.com/azesmway/react-native-unity,
which renders a Unity scene from an export from the Unity project.
The react-native-unity also has some limitations related to iOS Simulator
compatibility and requirements for exporting the Unity project itself.
The react-native-unity package is NOT compatible with the iOS Simulator. This
means any testing with a version of the app built with Unity artifacts must be
done using a real device.
The UnityFramework.framework for iOS exported from Unity is portable. This
means the export can be made anywhere and would still be usable to create an
iOS build with Unity artifacts. This also means the machine that's creating the
iOS build do not need to have Unity installed.
The react-native-unity package is compatible with the Android emulator.
The Android export of the Unity project is NOT portable. This means in order to create an Android build with Unity artifacts, the Android export must be done on the same machine that's performing the build for the app (and thus Unity must also be installed on that machine).
Due to the aforementioned concerns with iOS and Android, certain precaution are taken to ensure by defualt the app is not built with Unity artifacts. This would allow any developers not working on Unity related tasks to continue to use any simulator/emulator without any issues.
The postinstall script in package.json is updated to run a script to
"unlink" Unity artifacts from the app's build process:
- the
source_filesandvendored_frameworksattributes are commented out fromreact-native-unity's podspec file- this excludes Unity artifacts from the iOS build
- the
androiddirectory insidereact-native-unityis renamed toandroid.disabled- this excludes Untiy artifacts from the Android build
The postinstall script also includes an automatic patch to update the
UPlayer.java inside react-native-unity to ensure compatibility with Unity 6
(see azesmway/react-native-unity#123 (comment)).
Before the app can be built, prepare iOS and Android platform specific artifacts:
iOS Artifact
- Open the Unity project in Unity 6
- Create an iOS export
- The iOS export must be made with a minimal iOS version of
15.6
- The iOS export must be made with a minimal iOS version of
- Open and build the exported XCode project
- Set Target Membership of the
Datadirectory toUnityFramework - Set Header Visibility of
Libraries/Plugins/iOS/NativeCallProxytoPublic - Build the
UnityFrameworkproduct - Place the built
UnityFramework.frameworkat<repo>/unity/build/ios/UnityFramework.framework
Android Artifact
- Open the Unity project in Unity 6
- Create an Andriod export
- Ensure the
Export Projectcheckbox is checked - In "Player Settings":
- Set "Scripting Backend" to
IL2CPP - Set "Target Architecture" to ONLY
ARM64
- Set "Scripting Backend" to
- Ensure the
- Place the Android export at
<repo>/unity/build/android
Build Mobile App
Run the follow commands to prepare the build:
$ yarn
$ yarn clean
$ yarn unity:relink
$ yarn pods:reinstall
... the yarn (i.e. yarn install) command will trigger the postinstall
which will "unlink" the Unity artifacts from the build. This is why we have to
also run the relink-react-native-unity.sh script to relink them.
To build for iOS, run: $ yarn ios.
To build for Android, run: $ yarn android.
Run the follow commands to prepare the build:
$ yarn
$ yarn clean
$ yarn pods:reinstall
... the yarn (i.e. yarn install) command will trigger the postinstall
which will "unlink" the Unity artifacts from the build.
To build for iOS, run: $ yarn ios.
To build for Android, run: $ yarn android.