How to call method and return its value with UnityPlayer.UnitySendMessage

12,919

Solution 1

UnityPlayer.UnitySendMessage is a void function and does not return a value. Take a look at the UnitySendMessageExtension function implementation below. It is similar to turnipinindia's answer but it returns a value. It only returns a string so you have to convert that string to whatever variable type you are working with.

Java:

public final class MyPlugin
{
    //Make class static variable so that the callback function is sent to one instance of this class.
     public static MyPlugin testInstance;

     public static MyPlugin instance()
     {
         if(testInstance == null)
         {
             testInstance = new MyPlugin();
         }
         return testInstance;
     }

    string result = "";


    public string UnitySendMessageExtension(string gameObject, string functionName, string funcParam)
    {
        UnityPlayer.UnitySendMessage(gameObject, functionName, funcParam);
        string tempResult = result;
        return tempResult;
    }

    //Receives result from C# and saves it to result  variable
    void receiveResult(string value)
    {
        result = "";//Clear old data
        result = value; //Get new one
    }
}

C#:

class TestScript: MonoBehaviour
{
    //Sends the data from PlayerPrefs to the receiveResult function in Java
    void sendResultToJava(float value)
    {
        using(AndroidJavaClass javaPlugin = new AndroidJavaClass("com.company.product.MyPlugin"))
        {
             AndroidJavaObject pluginInstance = javaPlugin.CallStatic("instance");
             pluginInstance.Call("receiveResult",value.ToString());
        }
    }

    //Called from Java to get the saved PlayerPrefs
    void testFunction(string key)
    {
        float value = PlayerPrefs.GetFloat(key) //Get the saved value from key
        sendResultToJava(value); //Send the value to Java
    }
}

Usage from Java:

String str = UnitySendMessageExtension("ProfileLoad", "testFunction","highScore");  

This will work with any C# function. Just make sure that you call sendResultToJava function at the end of the C# function like we did in the testFunction function, if you want it to return value.

Solution 2

UnitySendMessage cannot return anything to Android.

One way you could do this is implement another method in Android that Unity will pass the value back to when requested.

So for example, first in android you request the data:

UnityPlayer.UnitySendMessage("ProfileSave", "getData");

In unity you have a method that receives that request and sends the data back to android:

public void getData () { 
    string dataString = "exampleData";
    //code to send data back to ReceiveData method in Android, something like this probably:
    AndroidJavaClass myAndroidClass = new AndroidJavaClass ("com.myCompany.myApplication.myClass");
    myAndroidClass.CallStatic ("ReceiveData",dataString);
}

Then in android you have a method waiting to receive the data (most likely a static method):

public static void ReceiveData(string data){
     //do stuff with data
}
Share:
12,919

Related videos on Youtube

user3855589
Author by

user3855589

Updated on June 04, 2022

Comments

  • user3855589
    user3855589 almost 2 years

    How can I call this C# method from Java then return the string value?

    public string getTest () { return "test"; }
    

    This is what I've tried:

    String str = UnityPlayer.UnitySendMessage("ProfileSave", "getTest","");
    

    I am getting the below error

    String str=UnityPlayer.UnitySendMessage("ProfileSave", "getTest","");                                 
    ^
     required: String  
     found:    void
    
  • Faruk
    Faruk over 6 years
    When I write c# code do I need to export Unity project again? I can't find c# files anywhere in Android project after export.
  • Programmer
    Programmer over 6 years
    @Faruk ?? The C# code should be in the Unity project.You'll have to re-export the Unity project after adding the C# code above otherwise the Java side can't find it. The Java code from this answer should be created on the Android Project side. Nothing complicated here....
  • Faruk
    Faruk over 6 years
    I am not sure what am I doing wrong, but I can't send message from one to another...