How to call a method and return its value using UnityPlayer.UnitySendMessage

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

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

Here is what I tried:

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

I get the error below

String str=UnityPlayer.UnitySendMessage("ProfileSave", "getTest","");                                 
^
 required: String  
 found:    void
+4
source share
2 answers

UnityPlayer.UnitySendMessageis a function voidand does not return a value. Take a look at the implementation of the function UnitySendMessageExtensionbelow. This is similar to turnipinindia's answer , but it returns a value. It only returns string, so you need to convert this string to any type of variable 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
    }
}

Using Java:

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

#. , sendResultToJava #, testFunction, , .

+1

UnitySendMessage Android.

- Android, Unity .

, , android :

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

, 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);
}

android , ( , ):

public static void ReceiveData(string data){
     //do stuff with data
}
0

Source: https://habr.com/ru/post/1657249/


All Articles