import React from 'react'; import type { StyleProp, ViewStyle } from 'react-native'; import { Platform } from 'react-native'; import type { DirectEventHandler } from 'react-native/Libraries/Types/CodegenTypes'; import NativeUnityView, { Commands } from './specs/UnityViewNativeComponent'; type UnityViewContentUpdateEvent = Readonly<{ message: string; }>; type RNUnityViewProps = { androidKeepPlayerMounted?: boolean; fullScreen?: boolean; onUnityMessage?: DirectEventHandler; onPlayerUnload?: DirectEventHandler; onPlayerQuit?: DirectEventHandler; style?: StyleProp; }; type ComponentRef = InstanceType; export default class UnityView extends React.Component { ref = React.createRef(); public postMessage = ( gameObject: string, methodName: string, message: string ) => { if (this.ref.current) { Commands.postMessage(this.ref.current, gameObject, methodName, message); } }; public unloadUnity = () => { if (this.ref.current) { Commands.unloadUnity(this.ref.current); } }; public pauseUnity(pause: boolean) { if (this.ref.current) { Commands.pauseUnity(this.ref.current, pause); } } public resumeUnity() { if (this.ref.current) { Commands.resumeUnity(this.ref.current); } } public windowFocusChanged(hasFocus = true) { if (Platform.OS !== 'android') return; if (this.ref.current) { Commands.windowFocusChanged(this.ref.current, hasFocus); } } private getProps() { return { ...this.props, }; } componentWillUnmount() { if (this.ref.current) { Commands.unloadUnity(this.ref.current); } } render() { return ; } }