I embedded a Unity WebGl build in a react-native webview, but I cannot send messages from the react-native app to unity?
I want to call the ChangeText function from the MyTextComponet GameObject.
My react-native code looks like this:
<code>// React Native JSX code
import React from 'react';
import { WebView } from 'react-native-webview';
import { View, Button, StyleSheet} from 'react-native';
const Test = () => {
let webViewRef;
const sendMessageToUnity = (message) => {
webViewRef.injectJavaScript(`window.UnityInstance.SendMessage('MyTextComponent', 'ChangeText', '${message}');`);
};
return (
<View style={styles.container}>
<WebView
ref={(webView) => { webViewRef = webView; }}
source={{ uri: 'http://localhost:8000/index.html' }}
/>
<Button
title="Send Message to Unity"
onPress={() => sendMessageToUnity('Hello from React Native!')}
style={styles.button}
/>
</View>
);
};
/**
styles ...
*/
export default Test;
</code>
<code>// React Native JSX code
import React from 'react';
import { WebView } from 'react-native-webview';
import { View, Button, StyleSheet} from 'react-native';
const Test = () => {
let webViewRef;
const sendMessageToUnity = (message) => {
webViewRef.injectJavaScript(`window.UnityInstance.SendMessage('MyTextComponent', 'ChangeText', '${message}');`);
};
return (
<View style={styles.container}>
<WebView
ref={(webView) => { webViewRef = webView; }}
source={{ uri: 'http://localhost:8000/index.html' }}
/>
<Button
title="Send Message to Unity"
onPress={() => sendMessageToUnity('Hello from React Native!')}
style={styles.button}
/>
</View>
);
};
/**
styles ...
*/
export default Test;
</code>
// React Native JSX code
import React from 'react';
import { WebView } from 'react-native-webview';
import { View, Button, StyleSheet} from 'react-native';
const Test = () => {
let webViewRef;
const sendMessageToUnity = (message) => {
webViewRef.injectJavaScript(`window.UnityInstance.SendMessage('MyTextComponent', 'ChangeText', '${message}');`);
};
return (
<View style={styles.container}>
<WebView
ref={(webView) => { webViewRef = webView; }}
source={{ uri: 'http://localhost:8000/index.html' }}
/>
<Button
title="Send Message to Unity"
onPress={() => sendMessageToUnity('Hello from React Native!')}
style={styles.button}
/>
</View>
);
};
/**
styles ...
*/
export default Test;
And I have this script attached to the GameObject in Unity:
<code>using TMPro;
using UnityEngine;
using UnityEngine.UI; // This namespace is needed for UI elements
public class RNToUnity : MonoBehaviour
{
public Text text; // No need to assign this in the inspector
public void ChangeText(string newText)
{
text.text = newText;
}
}
</code>
<code>using TMPro;
using UnityEngine;
using UnityEngine.UI; // This namespace is needed for UI elements
public class RNToUnity : MonoBehaviour
{
public Text text; // No need to assign this in the inspector
public void ChangeText(string newText)
{
text.text = newText;
}
}
</code>
using TMPro;
using UnityEngine;
using UnityEngine.UI; // This namespace is needed for UI elements
public class RNToUnity : MonoBehaviour
{
public Text text; // No need to assign this in the inspector
public void ChangeText(string newText)
{
text.text = newText;
}
}