Integrate unity3d within a React Native app. Add a react native component to show unity. Works on both iOS and Android.
.
├── Unity Project(Demo)
├── Modified Library(React-native_unity_view)
├── React Naive Sample Project(RNView)
│ └── Android (Android project for React Native)
│ └── ios (iOS project for React Native)
│ └── node_modules
│ └── package.json
├── README.md
-
Create a folder for Project
-
Navigate to the project in terminal
-
Run command
npx react-native init <project-name> npm i react-native-unity-view
- Start Unity Hub
- Follow the wizard to create project.
- Copy Editor folder from Demo/Assets to your UnityProject/Assets.
- Copy Newtonsoft.Json to your project.
- Copy UnityMessageManager to your Project.
- Copy files from React-native_unity_view/android to Assets/Plugins/Android (if directory is not present please create 1)
- Delete folder node_modules/react-native-unity-view/android/src/main/java
- Go to Unity and open build setting ctrl + shift + B.
- Click on Player Setting and go to Publishing Setting.
- Tick option Custom Main Gradle Template. (It will create a Gradle file in Assets/Plugins/Android)
- Open mainTemplate and add line to dependencies
implementation "com.facebook.react:react-native:+"
- Tick option Export Project and press Build
- Choose the path /android/UnityExport
- Go to /android/UnityExport.
- Copy files from launcher/src/main/res/values
to unityLibrary/src/main/res/values.
ids.xml string.xml
- Once done with all step open /android in Android Studio.
- Got to app/build.gradle and line to dependencies and remove line if any implements react-native-unity-view from the same
implementation project(':unityLibrary')
- Got to setting.gradle and remove react-native-unity-view project and unityLibrary (it will look like this)
rootProject.name = 'RNView' include ':unityLibrary' project(':unityLibrary').projectDir = new File(rootProject.projectDir, './unity/unityLibrary') apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) include ':app'
- Try to build if there is no error you are good to go.
- Copy all files from React-native_unity_view except android folder to /node_modules/react-native-unity-view folder
- Go to unity in top bar and select ReactNative/ExportiOS or you can do this from build setting as well.
- It will export iOS project to /ios/UnityExport
- open terminal and navigate to /ios and run command
pod install
- Open <yourreactnativeprojectname.workspace> in xCode
- Modify
main.m
#import "UnityUtils.h" int main(int argc, char * argv[]) { @autoreleasepool { InitArgs(argc, argv); return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }
- Open pod file and add these lines
post_install do |installer| installer.pods_project.targets.each do |target| if target.name == 'RNUnityView' target.build_configurations.each do |config| config.build_settings['HEADER_SEARCH_PATHS'] ||= "$(inherited)"\ " $(PODS_ROOT)/../../node_modules/React/**"\ " $(PODS_ROOT)/../../node_modules/react-native/React/**"\ " $(PODS_ROOT)/../UnityExport/Classes"\ " $(PODS_ROOT)/../UnityExport"\ " $(PODS_ROOT)/../UnityExport/Classes/Unity"\ " $(PODS_ROOT)/../UnityExport/Classes/PluginBase"\ " $(PODS_ROOT)/../UnityExport/Libraries"\ " $(PODS_ROOT)/../UnityExport/Libraries/libil2cpp/include"\ " $(PODS_ROOT)/../UnityExport/Libraries/bdwgc/include" react_native_post_install(installer) end end end end
- Go to Libraries folder under and rigth click on it. Click on "AddFiles".
- Navigate to UnityExport folder and add Unity-iPhone.xcodeproj
- Under Unity-iPhone.xcodeproj got products there is UnityFramwork.framework.
- Add UnityFramwork.framework to /General/EmbeddedContent
- Click run and you are good to go.