百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-native-unity-view

> 前端框架
开源

在 React Native 中显示统一视图

400 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 React Native 中显示统一视图

NOTICE: STOP MAINTENANCE

react-native-unity-view

Integrate unity3d within a React Native app. Add a react native component to show unity. Works on both iOS and Android.

Example

See react-native-unity-demo

Recommend Clone The Demo Project to Learn API.

Make sure you run the Demo properly before opening the Issue.

Preview

How to use

Install

bash
npm install react-native-unity-view --save

react-native link react-native-unity-view

Add Unity Project

  1. Create an unity project, Example: 'Cube'.
  2. Create a folder named unity in react native project folder.
  3. Move unity project folder to unity folder.

Now your project files should look like this.

.
├── android
├── ios
├── unity
│   └──     // Example: Cube
├── node_modules
├── package.json
├── README.md

Configure Player Settings

  1. First Open Unity Project.

  2. Click Menu: File => Build Settings => Player Settings

  3. Change Product Name to Name of the Xcode project, You can find it follow ios/${XcodeProjectName}.xcodeproj.

IOS Platform:

Other Settings find the Rendering part, uncheck the Auto Graphics API and select only OpenGLES2.

Add Unity Build Scripts and Export

Copy Build.cs and XCodePostBuild.cs to unity//Assets/Scripts/Editor/

Open your unity project in Unity Editor. Now you can export unity project with Build/Export Android or Build/Export IOS menu.

Android will export unity project to android/UnityExport.

IOS will export unity project to ios/UnityExport.

Add UnityMessageManager Support

Copy UnityMessageManager.cs to your unity project.

Copy Newtonsoft.Json to your unity project.

Copy link.xml to your unity project.

Configure Native Build

Android Build

Make alterations to the following files:

  • android/settings.gradle
...
include ":UnityExport"
project(":UnityExport").projectDir = file("./UnityExport")

IOS Build

  1. Open your react native project in XCode.

  2. Copy File UnityConfig.xcconfig to ios/${XcodeProjectName}/.

  3. Drag UnityConfig.xcconfig to XCode. Choose Create folder references.

  4. Setting .xcconfig to project.

  5. Go to Targets => Build Settings. Change Dead Code Stripping to YES.

  6. Modify main.m

#import "UnityUtils.h"

int main(int argc, char * argv[]) {
  @autoreleasepool {
    InitArgs(argc, argv);
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
}

Do not run in the simulator

Use In React Native

UnityView Props

onMessage

Receive message from unity.

Please copy UnityMessageManager.cs to your unity project and rebuild first.

Example:

  1. Send Message use C#.
UnityMessageManager.Instance.SendMessageToRN("click");
  1. Receive Message in javascript
onMessage(event) {
    console.log('OnUnityMessage: ' + event.nativeEvent.message);    // OnUnityMessage: click
}

render() {
    return (
        
            
        
    );
}
onUnityMessage

[Recommended]Receive json message from unity.

onUnityMessage(handler) {
    console.log(handler.name); // the message name
    console.log(handler.data); // the message data
    setTimeout(() => {
      // You can also create a callback to Unity.
      handler.send('I am callback!');
    }, 2000);
}

render() {
    return (
        
            
        
    );
}

UnityModule

import { UnityModule } from 'react-native-unity-view';
isReady(): Promise

Return whether is unity ready.

createUnity(): Promise

Manual init the Unity. Usually Unity is auto created when the first view is added.

postMessage(gameObject: string, methodName: string, message: string)

Send message to unity.

  • gameObject The Name of GameObject. Also can be a path string.
  • methodName Method name in GameObject instance.
  • message The message will post.

Example:

  1. Add a message handle method in MonoBehaviour.
public class Rotate : MonoBehaviour {
    void handleMessage(string message) {
		Debug.Log("onMessage:" + message);
	}
}
  1. Add Unity component to a GameObject.

  2. Send message use javascript.

onToggleRotate() {
    if (this.unity) {
      // gameobject param also can be 'Cube'.
      UnityModule.postMessage('GameObject/Cube', 'toggleRotate', 'message');
    }
}

render() {
    return (
        
             this.unity = ref}
                style={style.unity}
            />
            
        
    );
}
postMessageToUnityManager(message: string | UnityViewMessage)

Send message to UnityMessageManager.

Please copy UnityMessageManager.cs to your unity project and rebuild first.

Same to postMessage('UnityMessageManager', 'onMessage', message)

This is recommended to use.

  • message The message will post.

Example:

  1. Add a message handle method in C#.
void Awake()
{
    UnityMessageManager.Instance.OnMessage += toggleRotate;
}

void onDestroy()
{
    UnityMessageManager.Instance.OnMessage -= toggleRotate;
}

void toggleRotate(string message)
{
    Debug.Log("onMessage:" + message);
    canRotate = !canRotate;
}
  1. Send message use javascript.
onToggleRotate() {
    UnityModule.postMessageToUnityManager('message');
}

render() {
    return (
        
             this.unity = ref}
                style={style.unity}
            />
            
        
    );
}
addMessageListener(listener: (message: string | MessageHandler) => void): number

Receive string and json message from unity.

addStringMessageListener(listener: (message: string) => void): number

Only receive string message from unity.

addUnityMessageListener(listener: (handler: MessageHandler) => void): number

Only receive json message from unity.

pause()

Pause the unity player.

Thanks @wezzle. See #13

resume()

Resume the unity player.

Example Code

bash
import React from 'react';
import { StyleSheet, Image, View, Dimensions } from 'react-native';
import UnityView from 'react-native-unity-view';

export default class App extends React.Component {
    render() {
    return (
      
         : null}
        
          Welcome to React Native!
        
      
    );
  }
}

Enjoy!!!

Issues· 67 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javareactreact-nativeunity

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架