azesmway / react-native-unity

MIT License
272 stars 66 forks source link

Not able to receive message in Unity #80

Open musawarbilal786 opened 1 year ago

musawarbilal786 commented 1 year ago

I am sending message from react native to Unity but not able to receive message in Unity,

-> Message sending from Unity to RN is working fine but RN to Unity is not working for me

-> Not able to find any function in document to receive message in unity

=> Please provide a way to get message in Unity

here is RN code:

import React, { useRef } from 'react';
import UnityView from '@azesmway/react-native-unity';
import { View, Button, Alert } from 'react-native';

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

const Unity = () => {
  const unityRef = useRef<UnityView>(null);

  const sendDataToUnity = () => {
    if (unityRef?.current) {
      console.log("Message Sent to Unity!")
      const message: IMessage = {
        gameObject: 'gameObject',
        methodName: 'methodName',
        message: 'message',
      };
      unityRef.current.postMessage(message.gameObject, message.methodName, message.message);
    }
  };

  return (
    <View style={{ flex: 1 }}>
      <UnityView
        ref={unityRef}
        style={{ flex: 1 }}
        onUnityMessage={(result) => {
          console.log('OnUnityMessage:', result.nativeEvent.message);
          Alert.alert('Unity Message', 'Unity button Clicked!');
        }}
      />
      <Button title="Send Data to Unity" onPress={sendDataToUnity} />
    </View>
  );
};

export default Unity;

. . .

here is my Unity Script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using UnityEngine;

public class NativeAPI {
#if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    public static extern void sendMessageToMobileApp(string message);
#endif

}

public class ButtonBehavior : MonoBehaviour
{

    public  Text displayText;

    public void handleMessage(string message)
    {
        Debug.Log("onMessage:" + message);
        displayText.text = message;
    }

    public void ButtonPressed()
    {

        if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass jc = new AndroidJavaClass("com.azesmwayreactnativeunity.ReactNativeUnityViewManager"))
            {
                jc.CallStatic("sendMessageToMobileApp", "Unity button Clicked!");
            }
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IOS && !UNITY_EDITOR
            NativeAPI.sendMessageToMobileApp("Unity button Clicked!");
#endif
        }
    }
}
DigohD commented 1 year ago

Hi, not sure if this relates to your issue, but based on your message you are sending from RN:

const message: IMessage = { gameObject: 'gameObject', methodName: 'methodName', message: 'message', };

Make sure that:

  1. You have a GameObject in your scene with the exact name "gameObject". Not sure if its required, but I have it directly under the scene root (i.e. it has no parent object).
  2. Attach a script to this GameObject and make sure the script has a function with the exact name "methodName"

From your code snippet it seems like your function is named "handleMessage" which is not the same name as the "methodName" you specify in React Native when sending the message.

Hope this helps.