0.1.12 • Published 2 years ago
react-native-unity-play-ts v0.1.12
react-native-unity-play-ts
react-native-unity-play-ts
Installation
npm install react-native-unity-play-ts
Usage
import React, { useRef, useEffect } from 'react';
import UnityPlayView from 'react-native-unity-play-ts';
interface IMessage {
gameObject: string;
methodName: string;
message: string;
}
const Unity = () => {
const unityRef = useRef();
const message: IMessage = {
gameObject: 'gameObject',
methodName: 'methodName',
message: 'message',
};
useEffect(() => {
if (unityRef && unityRef.current) {
unityRef.current.postMessage(message);
}
}, []);
return (
<UnityPlayView
ref={unityRef}
style={{ flex: 1 }}
onUnityMessage={(result) =>
console.log('onUnityMessage', result.nativeEvent.message)
}
/>
);
};
export default Unity;
UNITY
- Copy from folder "unity" to "Unity_Project_Name" folder and rebuild unity project.
OnEvent in Unity
Add this code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using UnityEngine;
public class NativeAPI {
[DllImport("__Internal")]
public static extern void sendMessageToMobileApp(string message);
}
public class ButtonBehavior : MonoBehaviour
{
public void ButtonPressed()
{
NativeAPI.sendMessageToMobileApp("The button has been tapped!");
}
}
iOS
- Build Unity app to
[project_root]/unity/builds/ios
- Add
Unity-iPhone.xcodeproj
to your workspace:Menu
->File
->Add Files to [workspace_name]...
->[project_root]/unity/builds/ios/Unity-iPhone.xcodeproj
- Add
UnityFramework.framework
toFrameworks, Libraries, and Embedded Content
: - Select Data folder and set a checkbox in the "Target Membership" section to "UnityFramework"
- You need to select the NativeCallProxy.h inside the Libraries/Plugins/iOS folder of the Unity-iPhone project and change UnityFramework’s target membership from Project to Public. Don’t forget this step! https://miro.medium.com/max/1400/1*6v9KfxzR6olQNioUp_dFQQ.png
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT