Notes:
- Newtonsoft.Json.dll needs to be includeded as a unity package separately in your main project
$ npm install @wowmaking/react-native-unity --save
- Install package via
npm
- Move your Unity project to
unity
folder at project root
- Add following line at your
unity/Packages/manifest.json
{ ... "com.wowmaking.react-native-unity": "file:../../node_modules/@wowmaking/react-native-unity/unity" }
- Right-click at you
Hierarchy
menu, then clickCreate Empty
, rename new game object (example:UICommandsDelegate
). IMPORTANT! Remember the name of new game object, that needed to initialize JavaScript lib. - Add script
RNCommadsDelegate.cs
to new game object (step 2) (location:Packages/react-native-unity/Runtime/Scripts
) - To receive commands from JavaScript, you must create another game object, or use existing. Commands receiver object must implements
IRNCommandsReceiver
interfaceusing Wowmaking.RNU; public class NewGameObject : MonoBehaviour, IRNCommandsReceiver { ... }
- Set your object as commands receiver to
RNBridge
onAwake
using Wowmaking.RNU; public class NewGameObject : MonoBehaviour, IRNCommandsReceiver { private void Awake() { RNBridge.SetCommandsReceiver(this); } }
- Implement
IRNCommandsReceiver
interface by addingHandleCommand
methodIMPORTANT! Callusing Wowmaking.RNU; public class NewGameObject : MonoBehaviour, IRNCommandsReceiver { private void Awake() { RNBridge.SetCommandsReceiver(this); } public void HandleCommand(RNCommand command) { switch (command.name) { // command.Resolve(new {}) || command.Reject(new {}) } } }
Resolve
orReject
method of receivedRNCommand
instance to remove it from JavaScript thread
If you want to test your app at xcode simulator, you need following next steps:
- Go to
Menu
->Edit
->Project setings...
->Player
->iOS
->Other Settings
- Remove check from
Auto Graphics API
- Add
OpenGLES3
toGraphics APIs
list by clicking+
- Find
Target SDK
setting and selectSimulator SDK
You ready to debug your app at simulator!
- Run
pod install
- Add
RNUProxy.xcodeproj
to your workspace:Menu
->File
->Add Files to [workspace_name]...
->[project_root]/node_modules/@wowmaking/react-native-unity/ios/RNUProxy/RNUProxy.xcodeproj
- Build Unity app to
[project_root]/unity/builds/ios
- Add
Unity-iPhone.xcodeproj
to your workspace:Menu
->File
->Add Files to [workspace_name]...
->[project_root]/unity/builds/ios/Unity-iPhone.xcodeproj
- Add
UnityFramework.framework
toEmbedded Binaries
:- select
your_app
target in workspace - in
General
/Embedded Binaries
press+
- select
Unity-iPhone/Products/UnityFramework.framework
- remove
UnityFramework.framework
fromLinked Frameworks and Libraries
( select it and press-
) - in
Build Phases
moveEmbedded Binaries
beforeCompile Sources
( drag and drop )
- select
- Add following lines to your project
main.m
file (located at same folder withAppDelegate
)
#import <UIKit/UIKit.h>
+++ #import <RNUnity/RNUnity.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
+++ [RNUnity setArgc:argc];
+++ [RNUnity setArgv:argv];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
- Add following lines to your project
AppDelegate.m
file
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
+++ #import <RNUnity/RNUnity.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
+++ [RNUnity launchWithOptions:launchOptions]; // IMPORTANT to add this before react view creation
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
...
}
+++ - (void)applicationWillResignActive:(UIApplication *)application { [[[RNUnity ufw] appController] applicationWillResignActive: application]; }
+++ - (void)applicationDidEnterBackground:(UIApplication *)application { [[[RNUnity ufw] appController] applicationDidEnterBackground: application]; }
+++ - (void)applicationWillEnterForeground:(UIApplication *)application { [[[RNUnity ufw] appController] applicationWillEnterForeground: application]; }
+++ - (void)applicationDidBecomeActive:(UIApplication *)application { [[[RNUnity ufw] appController] applicationDidBecomeActive: application]; }
+++ - (void)applicationWillTerminate:(UIApplication *)application { [[[RNUnity ufw] appController] applicationWillTerminate: application]; }
@end
- In
AppDelegate.m
file make background color of React root view transparent
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:0];
- Add
RNUProxy
andUnityFramework
to your project scheme. Select your project scheme ->Edit scheme...
->Build
-> Click+
-> SelectRNUProxy
/UnityFramework
from list -> moveUnityFramework
before your app (drag and drop) andRNUProxy
beforeUnityFramework
- Add ndk support into
android/app/build.gradle
defaultConfig { ... ndk { abiFilters "armeabi-v7a", "arm64-v8a" } }
- Append the following lines to
android/settings.gradle
:include ':unityLibrary' project(':unityLibrary').projectDir=new File('..\\unity\\builds\\android\\unityLibrary')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:implementation project(':unityLibrary') implementation files("${project(':unityLibrary').projectDir}/libs/unity-classes.jar")
- Change parent activity in
MainActivity.java
fromReactActivity
toUnityReactActivity
import com.wowmaking.rnunity.UnityReactActivity; public class MainActivity extends UnityReactActivity { ... }
- Add strings to
res/values/strings.xml
<string name="game_view_content_description">Game view</string> <string name="unity_root">unity_root</string>
- Update
.MainActivity
intoAndroidManifest.xml
<activity android:name=".MainActivity" ... android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="true" android:launchMode="singleTask" >
- Setup
minSdkVersion
greater than or equal to19
- Remove
<intent-filter>...</intent-filter>
from AndroidManifest.xml at unityLibrary to leave only integrated version.
import { Unity, UnityResponderView } from '@wowmaking/react-native-unity';
// Don't forget to initialize with name of GameObject, that you create at `Unity`->`Step 2`
Unity.init('UICommandsDelegate');
const App = () => {
return (
<View>
<!-- UnityResponderView provide all touch events to Unity -->
<UnityResponderView />
<Touchable onPress={()=>Unity.execCommand('command_name', { /* any specific command data */ })}>Press ME!</Touchable>
</View>
);
};
-
init
- initializereact-native-unity
lib Params:delegateName
(string
) - name of Unity GameObject, that was created atUnity
->Step 2
Usage:
Unity.init('UICommandsDelegate');
-
execCommand
- send command to Unity Params:name
(string
) - Unity command namedata
(Object
, optional) - Unity command data
Return
Promise
Usage:Unity.execCommand('command_name', { a: 1, b: 'b', })
-
addEventListener
- add listener of Unity events Params: -type
(string
) - type of Unity event -listener
(function
) - function, that's calling on Unity event receivingUsage:
Unity.addEventListener('event_type', (e) => { console.warn(e); });
-
removeEventListener
- remove Unity event listener Params: -type
(string
) - type of Unity event -listener
(function
) - specific listener to removeUsage:
Unity.addEventListener('event_type', listener });
void HandleCommand(RNCommand command)
- method, that calls from JavaScript Params:command
(RNCommand
) - command object, received from JavaScript
name
(string
) - name of received commanddata
('object') - data of received command
-
Resolve
- invoke on successful command execution Params:data
(object
, optional) - object, that will receive JavaScript
Usage:
command.Resolve(new { text = "test", });
-
Reject
- invoke on unsuccessful command execution Params:data
(object
, optional) - object, that will receive JavaScript
Usage:
command.Reject(new { text = "test", });
-
SetCommandsReceiver
- set commands reveiver to bridge Params:cReceiver
(IRNCommandsReceiver
) - game object, that implements IRNCommandsReceiver interface
Usage:
private void Awake() { RNBridge.SetCommandsReceiver(this); }
-
SendEvent
- send event to JavaScript Params:name
(string
) - event name, that receive JavaScriptdata
(object
) - data object, that receive JavaScript listeners