react-native-unity
react-native-unity copied to clipboard
How to reload unity instance
I have kept androidKeepPlayerMounted this false , so first time unity loads but when i navigate to any other screen and come back again to unity screen then unity does not load below is the code. Thank you for taking a look.
import { ImageBackground, StatusBar, Image, TouchableOpacity, View, TextInput, SafeAreaView, Button, } from 'react-native'; import React, {useRef, useEffect, useState} from 'react'; import Images from '../../../Styles/Images'; import styles from './Styles'; import UnityView from '@azesmway/react-native-unity'; import {useNavigation, useFocusEffect} from '@react-navigation/native'; import Styles from '../../Auth/Login/Styles'; import {Colors} from 'react-native/Libraries/NewAppScreen'; export default function FaceInteraction(props): React.JSX.Element { const {navigation} = props; const unityRef = useRef<UnityView>(null); const [message, setMessage] = useState(''); const [showTextField, setShowTextField] = useState(false); console.log(unityRef); const handleInputChange = inputValue => { setMessage(inputValue); };
const submitHandler = () => { const unityData = { name: message, age: 3, }; let jsonedData = JSON.stringify(unityData); console.log(unityData) SendData(jsonedData); }; const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); async function SendData(data: any) { await delay(500); console.log('data is sended from react native'); unityRef.current?.postMessage('ReactToUnity', 'GetDatas', data); }
useEffect(() => { return () => { unityRef.current?.componentWillUnmount(); }; }, []);
return (
<SafeAreaView>
<View style={{backgroundColor: 'black'}}>
<UnityView
ref={unityRef}
style={{ height: 100%', width: '100%'}}
androidKeepPlayerMounted={false}
onUnityMessage={result => {
console.log('Message Here : ', result.nativeEvent.message);
let d = result.nativeEvent.message;
if (d.includes('home')) {
navigation.navigate('BottomNavigation', {screen: 'HomeTab'});
}
}}
/>
</View>
</SafeAreaView>
); }