UnityPlayer.UnitySendMessage
は、void
関数であり、値を返しません。以下のUnitySendMessageExtension
関数の実装を見てください。 turnipinindia's答えに似ていますが、値を返します。 string
が返されるだけなので、その文字列を使用している変数の型に変換する必要があります。
の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
}
}
Javaから使用法:
String str = UnitySendMessageExtension("ProfileLoad", "testFunction","highScore");
これは、任意のC# FUNCで動作します。値を返すには、sendResultToJava
関数がtestFunction
関数のように、C#関数の最後に関数を呼び出すようにしてください。
ProfileSaveとは何ですか? – Programmer
そのゲームオブジェクト... – user3855589
'getTest' voidを作成し、' void'関数として呼び出します。その場合は私に教えてください – Programmer