问题描述
我开发一个统一Android插件,我不知道是否有在Android code的方式来调用返回值的统一C#方法,并得到此值。
I am developing an Android plugin for Unity and I am wondering if there is a way in the Android code to call a Unity C# method that returns a value and get this value.
当然,这是不行的,但有一种方法,技巧,技巧来达到这样的:
Of course this will NOT work but is there a way, tips, tricks to achieve something like this:
String myReturnedString = UnityPlayer.UnitySendMessage("MyGameObject",
"ReturnThisString","hello");
您的帮助非常感谢。
Many thanks for your help.
推荐答案
的一种方法是在与您的Android应用程序,通过一种方法在Android应用通信的统一程序的方法,使用的 AndroidJavaObject.Call 的在团结类。
One way is to have a method in your Unity program that communicates with your Android application through a method in your Android app, by using AndroidJavaObject.Call in your Unity class.
例如。
你在你的Android应用此方法。
e.g.You have this method in your Android application.
AndroidActivity.java
String stringFromUnity = "";
public void setStringFromUnity(String input){
stringFromUnity = input;
}
然后在你的脚本团结(我使用C#)。
Then in your Unity script (I am using C#).
UnityScript.cs
// Get the UnityPlayer class
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
// Get the current activity
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
// Call the setStringFromUnity method from Android Activity
activity.Call("setStringFromUnity", "Here is a string for you, Android");
或者我们可以直接使用设置字符串的 AndroidJavaObject.Set 的方法。
// Set the String directly provided the String is public
activity.Set<string>("stringFromUnity", "I set your String directly, hah!");
希望这有助于。
有关参考,您可以搜索团结插件为Android或者看看团结文档在此的。
For reference, you can search for "Unity Plugins for Android" or take a look at Unity documentation at this link.
这篇关于调用从Android电子/ Java的返回值的统一C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!