@azesmway/react-native-unity

中文文档
1.1.1发布于 2026/08/07MITTypeScriptESM + CJS零依赖近 30 天下载 7229

React Native Unity

原始使用文档
文档目录10 节
  1. Android
  2. Device Support:
  3. Install this package in your react-native project:
  4. Configure your Unity project:
  5. Export iOS Unity Project:
  6. Android
  7. Sample code
  8. Props
  9. Methods
  10. iOS Simulator support (optional)

@azesmway/react-native-unity

The plugin that allows you to embed a Unity project into React Native as a full-fledged component. The plugin now supports the new architecture.

Android

Attention! Added support for Unity 2023 and above

[!IMPORTANT] For iOS, it is no longer necessary to embed a project created with Unity. Only the built UnityFramework is used. It should be placed in the plugin folder at the path - <YOUR_RN_PROJECT>/unity/builds/ios

Device Support:

PlatformSupported
iOS Simulator⚠️ optional (see iOS Simulator support)
iOS Device✅
Android Emulator✅
Android Device✅

The iOS Simulator works only with the optional XCFramework export described below; the default device-only UnityFramework.framework does not link on the simulator.

Installation

Install this package in your react-native project:

npm install @azesmway/react-native-unity

or

yarn add @azesmway/react-native-unity

Configure your Unity project:

  1. Copy the contents of the folder unity to the root of your Unity project. This folder contains the necessary scripts and settings for the Unity project. You can find these files in your react-native project under node_modules/@azesmway/react-native-unity/unity. This is necessary to ensure iOS has access to the NativeCallProxy class from this library.

  2. (optional) If you're following along with the example, you can add the following code to the ButtonBehavior.cs script in your Unity project. This allows the button press in Unity to communicate with your react-native app.

ButtonBehavior.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using UnityEngine;

public class NativeAPI {
#if UNITY_IOS && !UNITY_EDITOR
  [DllImport("__Internal")]
  public static extern void sendMessageToMobileApp(string message);
#endif
}

public class ButtonBehavior : MonoBehaviour
{
  public void ButtonPressed()
  {
    if (Application.platform == RuntimePlatform.Android)
    {
      using (AndroidJavaClass jc = new AndroidJavaClass("com.azesmwayreactnativeunity.ReactNativeUnityViewManager"))
      {
        jc.CallStatic("sendMessageToMobileApp", "The button has been tapped!");
      }
    }
    else if (Application.platform == RuntimePlatform.IPhonePlayer)
    {
#if UNITY_IOS && !UNITY_EDITOR
      NativeAPI.sendMessageToMobileApp("The button has been tapped!");
#endif
    }
  }
}

Export iOS Unity Project:

After you've moved the files from the unity folder to your Unity project, you can export the iOS unity project by following these steps:

  1. Open your Unity project
  2. Build Unity project for ios in ANY folder - just not the main RN project folder!!!
  3. Open the created project in XCode
  4. Select Data folder and set a checkbox in the "Target Membership" section to "UnityFramework" image info
  5. You need to select the NativeCallProxy.h inside the Unity-iPhone/Libraries/Plugins/iOS folder of the Unity-iPhone project and change UnityFramework’s target membership from Project to Public. Don’t forget this step! (if you don't see these files in your Xcode project, you didn't copy over the unity folder to your Unity project correctly in previous steps) image info
  6. If required - sign the project UnityFramework.framework and build a framework image info
  7. Open the folder with the built framework (by right-clicking) and move it to the plugin folder (<YOUR_RN_PROJECT>/unity/builds/ios) image info
  8. Remove your Pods cache and lockfile with this command in the root of the main RN project rm -rf ios/Pods && rm -f ios/Podfile.lock && npx pod-install

Android

  1. Open your Unity project
  2. Export Unity app to <YOUR_RN_PROJECT>/unity/builds/android
  3. Remove <intent-filter>...</intent-filter> from <YOUR_RN_PROJECT>/unity/builds/android/unityLibrary/src/main/AndroidManifest.xml at unityLibrary to leave only integrated version.

If you're using expo, you're done. The built-in expo plugin will handle the rest. If you're not using expo, you'll need to follow the steps below.

  1. Add the following lines to android/settings.gradle:

    include ':unityLibrary'
    project(':unityLibrary').projectDir=new File('..\\unity\\builds\\android\\unityLibrary')
    
  2. Add into android/build.gradle

    allprojects {
      repositories {
        // this
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
        // ...
      }
    }
    
  3. Add into android/gradle.properties

    unityStreamingAssets=.unity3d
    
  4. Add strings to android/app/src/main/res/values/strings.xml

    <string name="game_view_content_description">Game view</string>
    

Known issues

  • Does not work on the iOS simulator with the default device-only framework. See iOS Simulator support (optional) for the XCFramework workaround.
  • On iOS the Unity view is waiting for a parent with dimensions greater than 0 (from RN side). Please take care of this because if it is not the case, your app will crash with the native message MTLTextureDescriptor has width of zero.

Usage

Sample code

import React, { useRef, useEffect } from 'react';

import UnityView from '@azesmway/react-native-unity';
import { View } from 'react-native';

interface IMessage {
  gameObject: string;
  methodName: string;
  message: string;
}

const Unity = () => {
  const unityRef = useRef<UnityView>(null);

  useEffect(() => {
    if (unityRef?.current) {
      const message: IMessage = {
        gameObject: 'gameObject',
        methodName: 'methodName',
        message: 'message',
      };
      unityRef.current.postMessage(
        message.gameObject,
        message.methodName,
        message.message
      );
    }
  }, []);

  return (
    <View style={{ flex: 1 }}>
      <UnityView
        ref={unityRef}
        style={{ flex: 1 }}
        onUnityMessage={(result) => {
          console.log('onUnityMessage', result.nativeEvent.message);
        }}
      />
    </View>
  );
};

export default Unity;

Props

  • style: ViewStyle - styles the UnityView. (Won't show on Android without dimensions. Recommended to give it flex: 1 as in the example)
  • onUnityMessage?: (event: { nativeEvent: { message: string } }) => void - receives a message from Unity. The payload is available as event.nativeEvent.message
  • onPlayerUnload?: (event) => void - fired when the Unity player has been unloaded
  • onPlayerQuit?: (event) => void - fired when the Unity player has quit
  • androidKeepPlayerMounted?: boolean - if set to true, keep the player mounted even when the view that contains it has lost focus. The player will be paused on blur and resumed on focus. ANDROID ONLY
  • fullScreen?: boolean - defaults to true. If set to false, will not request full screen access. ANDROID ONLY

Methods

  • postMessage(gameObject, methodName, message) - sends a message to the Unity. FOR IOS: The native method of unity is used to send a message sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg;, more details can be found in the documentation
  • unloadUnity() - the Unity is unloaded automatically when the react-native component is unmounted, but if you want to unload the Unity, you can call this method
  • pauseUnity(pause: boolean) - pause (true) or resume (false) the Unity player
  • resumeUnity() - resume the Unity player after it was paused
  • windowFocusChanged(hasFocus: boolean = true) - simulate focus change (intended to be used to recover from black screen (not rendering) after remounting Unity view when resumeUnity does not work) ANDROID ONLY

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

iOS Simulator support (optional)

The classic setup vendors a device-only UnityFramework.framework, which is why the support table lists the iOS Simulator as unsupported: simulator builds fail to link the device-arm64 dylib. Unity itself can target the simulator — pack both slices into an XCFramework and the plugin picks it up automatically:

  1. In Unity, export the iOS project twice: once with Target SDK: Device SDK and once with Target SDK: Simulator SDK (Player Settings → iOS → Target SDK).
  2. Build UnityFramework from each export and create the XCFramework:
xcodebuild -project device-export/Unity-iPhone.xcodeproj -scheme UnityFramework \
  -configuration Release -sdk iphoneos BUILD_DIR="$PWD/device-build" build
xcodebuild -project simulator-export/Unity-iPhone.xcodeproj -scheme UnityFramework \
  -configuration Release -sdk iphonesimulator BUILD_DIR="$PWD/simulator-build" build
xcodebuild -create-xcframework \
  -framework device-build/Release-iphoneos/UnityFramework.framework \
  -framework simulator-build/Release-iphonesimulator/UnityFramework.framework \
  -output <YOUR_RN_PROJECT>/unity/builds/ios/UnityFramework.xcframework
  1. Reinstall pods. The App Store build is unaffected: Xcode links only the device slice, so the shipped app size does not change.

版本与发布记录

@azesmway/react-native-unity 共发布 37 个版本,首次发布于 2022/03/29;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.1.1latest2026/08/073 个月前
1.1.02026/07/133 个月前
1.0.112024/12/131.9 年前
1.0.102024/06/132 年前
1.0.92024/05/082 年前
1.0.82024/05/082 年前

关于 @azesmway/react-native-unity 的常见问题

@azesmway/react-native-unity 是什么?

React Native Unity

如何安装 @azesmway/react-native-unity?

在项目目录运行 npm install @azesmway/react-native-unity,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@azesmway/react-native-unity 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azesmway/react-native-unity 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azesmway/react-native-unity 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azesmway/react-native-unity 可以商用吗?

@azesmway/react-native-unity 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azesmway/react-native-unity 的下载量有多少?

根据 npm 官方下载统计,@azesmway/react-native-unity 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,229 次,日均约 241 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azesmway/react-native-unity 还在维护吗?

最新版本 1.1.1 发布于 2026/08/07(3 个月前);至今共发布 37 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/03/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。