8000 GitHub - brunomarchasson/RNUnity
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

brunomarchasson/RNUnity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity

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);
            }
        });
    ...
}

Export Settings

in build settings, check "Export project"

199bef38997df5c32f30447caeb0c4e3.pngcc04e1db6103406c0736789ca6fa4800.png

export unity project to ./androidBuild

Android

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

IOS

ReactNative

copy react component from "SampleReactNativeApp\src\components\UnityView"

Usage

display unity in rn

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>
    )
}

send message from rn to unity

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);

send message from unity to rn

UnityMessageManager.Instance.SendMessageToRN(new UnityMessage(){
            name = "click",
            data = JObject.FromObject(
                new {
                  ...
            }),
            callBack = (data) =>{
                Debug.Log("onClickCallBack:" + data);
            }
        });

usefull links

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  
0