栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-09-27

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-09-27

如何使用UnityPlayer.UnitySendMessage调用方法并返回其值 - IT屋-程序员软件开发技术分享社区How to call method and return its value with UnityPlayer.UnitySendMessage(如何使用UnityPlayer.UnitySendMessage调用方法并返回其值) - IT屋-程序员软件开发技术分享社区https://www.it1352.com/1848997.html

如何从Java调用此C#方法然后返回字符串值?

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

这是我尝试过的:

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

我遇到以下错误

String str=UnityPlayer.UnitySendMessage("ProfileSave", "getTest","");                                 
^
 required: String  
 found:    void

解决方案

UnityPlayer.UnitySendMessage是void函数,不返回值.看一下下面的UnitySendMessageExtension函数实现.它类似于 turnipinindia的答案,但它返回一个值.它仅返回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#函数一起使用.如果要返回值,只需确保像在testFunction函数中一样在 C#函数末尾调用sendResultToJava函数即可.

  • 原文

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

解决方案

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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号