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
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:
In unity you have a method that receives that request and sends the data back to android:
Then in android you have a method waiting to receive the data (most likely a static method):
UnityPlayer.UnitySendMessage
is avoid
function and does not return a value. Take a look at theUnitySendMessageExtension
function implementation below. It is similar to turnipinindia's answer but it returns a value. It only returns astring
so you have to convert that string to whatever variable type you are working with.Java:
C#:
Usage from Java:
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 thetestFunction
function, if you want it to return value.