简体   繁体   English

无法将void转换为int

[英]cannot convert void to int

I'm making my first Android plug in for unity and I've ran into one issue I can't resolve. 我正在制作第一个Android统一插件,但遇到了一个我无法解决的问题。 Right now, this plug in is extremely simple. 现在,这个插件非常简单。 All it does it return an increasing number but with the method being create in a native Android file. 它所做的一切都会返回一个递增的数字,但方法是在本地Android文件中创建的。 Then I call this method from inside Unity to see the number rise. 然后,我从Unity内部调用此方法以查看数量的增加。

Here is my code. 这是我的代码。 The Android Java file: Android Java文件:

package com.test.app;

import com.unity3d.player.UnityPlayerActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;

public class PluginTest extends UnityPlayerActivity
{
    private static final String TAG = "PluginTest_Unity";
    private static int number = 0;

@Override
protected void onCreate(Bundle myBundle)
{
    super.onCreate(myBundle);
}

@Override
protected void onResume()
{
    if(Config.DEBUG)
    {
        Log.d(TAG, "onResume");
    }
    super.onResume();
}

@Override
protected void onPause()
{
    super.onPause();
}

@Override
protected void onStop()
{
    if(Config.DEBUG)
    {
        Log.d(TAG, "onStop");

    }
    super.onStop();
}

public static int getNumber()
{
    number++;
    return number;
}

} }

My C# file in Unity: 我在Unity中的C#文件:

using UnityEngine;
using System.Collections;

public class NumberExample : MonoBehaviour 
{
    public GUIText number_output_text;
    AndroidJavaClass pluginTutorialActivityJavaClass;

    // Use this for initialization
    void Start () 
    {
        AndroidJNI.AttachCurrentThread();
        pluginTutorialActivityJavaClass = new AndroidJavaClass("com.test.app.PluginTest");
    }

    // Update is called once per frame
    void Update () 
    {
        int number = pluginTutorialActivityJavaClass.CallStatic("getNumber");
        number_output_text.text = "nr: " + number;
    }
}

However, when I try to build my program in Unity it complains that I cannot convert a void to an int with the following line: 但是,当我尝试在Unity中构建程序时,它抱怨无法用以下行将void转换为int:

int number = pluginTutorialActivityJavaClass.CallStatic("getNumber");

Can anyone see the change I need to make with my code? 谁能看到我需要对代码进行的更改?

From the docs : 文档

To call a static method with a non-void return type, use the generic version. 要使用非无效返回类型调用静态方法,请使用通用版本。

// Create a java.lang.String object, and call static method valueOf(float value).
function Start() {
    var jo = new AndroidJavaObject("java.lang.String");
    var valueString = jo.CallStatic<string>("valueOf", 42.f);
}

You should probably call 你应该打电话给

int number = pluginTutorialActivityJavaClass.CallStatic<int>("getNumber");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM