Unity
Copy Script "UnityMessageManager.cs" to project this script create a global "UnityMessageManager" insatance to magage messages
In any game object' script
void Awake()
{
...
//register Message handler
UnityMessageManager.Instance.OnRNMessage += onMessage;
}
void onDestroy()
{
...
//unregister Message handler
UnityMessageManager.Instance.OnRNMessage -= onMessage;
}
void onMessage(MessageHandler message)
{
//send callback to messages
var data = message.getData<string>();
message.reply(new { CallbackTest = "I am Unity callback" });
}
void myExposedFunction(string val)
{
...
//Send Message to RN
UnityMessageManager.Instance.SendMessageToRN(new UnityMessage(){
name = "click",
data = JObject.FromObject(
new {
colors = colors[CurrentColorIndex].ToString() ,
clickCount = clickCount
}),
callBack = (data) =>{
Debug.Log("onClickCallBack:" + data);
}
});
...
}
in build settings, check "Export project"
export unity project to ./androidBuild
build.gradle
allprojects {
repositories {
...
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
flatDir {
dirs 'libs'
}
...
}
}
gradle.properties
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService
8000
-Info.plist
settings.gradle
...
include ':unityLibrary'
project(':unityLibrary').projectDir=new File('..\\..\\UnityProject\\androidBuild\\unityLibrary')
app/build.gradle
dependencies {
implementation project(':unityLibrary')
implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
...
}
android\app\src\main\java\com\samplereactnativeapp\MainApplication.java
...
@Override
protected List<ReactPackage> getPackages() {
...
packages.add(new UnityViewPackage());
return packages;
}
...
copy files :
- UnityEventListener.java
- UnityNativeModule.java
- UnityUtils.java
- UnityView.java
- UnityViewManager.java
- UnityViewPackage.
into android\app\src\main\java\com\samplereactnativeapp
copy react component from "SampleReactNativeApp\src\components\UnityView"
import UnityView from '../components/UnityView';
...
function UnityScreen(props) {
const onUnityMessage = hander => {
console.log('onUnityMessage', hander);
setTimeout(() => {
hander.send('I am click callback!');
}, 2000);
};
...
return (
<UnityView
style={{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0 }}
onUnityMessage={onUnityMessage}
onMessage={onMessage}>
...
</UnityView>
)
}
you can call any methode of any gameObject in untity project. the only limitation is that param must be a string
import {unityModule} from '../components/UnityView';
...
unityModule.postMessage('GameObject', methodeName, param);
UnityMessageManager.Instance.SendMessageToRN(new UnityMessage(){
name = "click",
data = JObject.FromObject(
new {
...
}),
callBack = (data) =>{
Debug.Log("onClickCallBack:" + data);
}
});
- https://medium.com/@beaulieufrancois/show-unity3d-view-in-react-native-application-yes-its-possible-852923389f2d
- https://medium.com/@beaulieufrancois/part-2-show-unity3d-view-in-react-native-application-138219323cbc
- https://medium.com/codeexplorers/connecting-the-dots-between-react-native-and-unity-3d-using-gradle-67f93b92c254
- https://github.com/f111fei/react-native-unity-view
- https://github.com/Unity-Technologies/uaal-example