Giter VIP home page Giter VIP logo

unireact's Introduction

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-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.java

into android\app\src\main\java\com\samplereactnativeapp

IOS

open react native ios project with xcode

create workspace

add iosBuild/Unity-IPhone.xproj to workspace

set Unity-Iprone/Data folder menbership to UnityFramework

1.png

set Unity-Iprone/Library/Plugins/NativeCallProxy.h visibility to public

2.png Add UnityFramework in RN project 3.png Remove UnityFramework from Link Binary with libraries 4.png

copy files :

  • UnityModule.h
  • UnityModule.m
  • UnityView.h
  • UnityView.m
  • UnityViewManager.h
  • UnityViewManager.m

into ios/[ProjectName]

AppDelegate.m

...
#include <UnityFramework/UnityFramework.h>
#include <UnityFramework/NativeCallProxy.h>
#import "UnityModule.h"
...

- (void)applicationWillResignActive:(UIApplication *)application { [[[UnityModule ufw] appController] applicationWillResignActive: application]; }
- (void)applicationDidEnterBackground:(UIApplication *)application { [[[UnityModule ufw] appController] applicationDidEnterBackground: application]; }
- (void)applicationWillEnterForeground:(UIApplication *)application { [[[UnityModule ufw] appController] applicationWillEnterForeground: application]; }
- (void)applicationDidBecomeActive:(UIApplication *)application { [[[UnityModule ufw] appController] applicationDidBecomeActive: application]; }
- (void)applicationWillTerminate:(UIApplication *)application { [[[UnityModule ufw] appController] applicationWillTerminate: application]; }

main.m

...
@autoreleasepool {
    [UnityModule setArgc:argc];
    [UnityModule setArgv:argv];
    ...
  }
...

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.sendMessage(methodeName,params, callback);
   

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

unireact's People

Contributors

bardiaalavi-2020 avatar bruno-perspectives avatar nicolas-perspectives avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.