Home > Mobile >  Argument of type 'string' is not assignable to parameter of type '{ results: string;
Argument of type 'string' is not assignable to parameter of type '{ results: string;

Time:09-22

When I'm trying to pass the result.nativeEvent.message to other function im getting the Argument of type 'string' is not assignable to parameter of type '{ results: string; } on onUnityMessageController(result.nativeEvent.message).

I don't understand why the 'string' can't be assigned to paramater of 'string'.

Unity.tsx

import React, { useRef, useEffect } from 'react';
import UnityView from '@azesmway/react-native-unity';
import { View, Button, NativeSyntheticEvent } from 'react-native';
import { CommonActions } from '@react-navigation/native';

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

const Unity = ({ navigation, route }: { navigation: undefined, route: any }) => {
  const unityRef = useRef<UnityView>(null);

  const shape = route.params.shape;
  const color = route.params.color;

  const parsedMess = JSON.stringify(route.params);
  console.log(parsedMess);

  const closeUnity = () => {
    unityRef.current?.unloadUnity();
    navigation.goBack();
  }

  useEffect(() => {
    if (unityRef?.current) {
      const message: IMessage = {
        gameObject: 'gameObject',
        methodName: 'methodName',
        message: 'message',
      };
      unityRef.current.postMessage(message.gameObject, message.methodName, message.message);
      if (shape != null && color != null) unityRef.current.postMessage("SceneManager", "startUnity", shape   ";"   color);
    }
  }, [shape, color]);



  const onUnityMessageController = ({ results }: { results: string }) => {
    console.log('onUnityMessage', results);
  }

  return (
    <View style={{ flex: 1 }}>
      <UnityView
        ref={unityRef}
        style={{ flex: 1 }}
        onUnityMessage={(result) => { onUnityMessageController(result.nativeEvent.message), console.log(result.nativeEvent.message) }} />
      <Button title="Close Unity Screen" onPress={() => closeUnity()} />
    </View >
  );
};

export default Unity;

result.nativeEvent.message comes from:

import React from 'react';
import { NativeSyntheticEvent, ViewStyle } from 'react-native';
interface UnityMessage {
    message: string;
}
declare type ReactNativeUnityViewProps = {
    androidKeepPlayerMounted?: boolean;
    fullScreen?: boolean;
    onUnityMessage?: (event: NativeSyntheticEvent<UnityMessage>) => void;
    onPlayerUnload?: (event: NativeSyntheticEvent<void>) => void;
    onPlayerQuit?: (event: NativeSyntheticEvent<void>) => void;
    style?: ViewStyle;
};
export default class UnityView extends React.Component<ReactNativeUnityViewProps> {
    static defaultProps: {};
    constructor(props: ReactNativeUnityViewProps);
    postMessage(gameObject: string, methodName: string, message: string): void;
    unloadUnity(): void;
    pauseUnity(pause: boolean): void;
    resumeUnity(): void;
    private getCommand;
    private getProps;
    componentWillUnmount(): void;
    render(): JSX.Element;
}
export { };

CodePudding user response:

You are passing simple String as an argument to the function onUnityMessageController but your type check is expecting object. So simply change your function likewise :

  const onUnityMessageController = (results : string) => {
    console.log('onUnityMessage', results);
  }
  • Related