首页 > 解决方案 > Unity 中的 Android 插件 - 无返回值

问题描述

我试图创建一个 android 插件并统一导入它,但我总是从插件方法中得到 0 作为返回值,我无法解决这个问题。

这是我所做的:

AndroidStudio:
-创建一个新项目
-创建一个库模块(包:com.e.timerlibrary)
-创建公共类“MyPlugin”
-Build->“Make Module'timerlibrary'”
-将“classes.jar”文件复制到“Assets”中Unity 中的 /Plugins/Android"

MyPlugin 只有一种方法:

 public int testmethode(){
        return 100;
    }

统一:

    AndroidJavaObject JavaTimerClass = new AndroidJavaObject("com.e.timerlibrary.MyPlugin");
    TextPlugin.text = JavaTimerClass.Call<int>("testmethode").ToString();

TextPlugin.text 应该显示“100”但它始终为 0。似乎找不到包的名称

希望有人可以帮助我。我是 unity&Androidstudio 的新手,并在 youtube 上学习了许多教程,但我无法解决这个问题。

这是我的完整 C# 代码。它基本上是一个计时器。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CountDownTimer : MonoBehaviour
{

    public float timeValue = 0;
    public float timeValuePause = 0;
    public float sliderValue;
    public Slider slider;
    public Text textTimer;
    public Text textTimerBtn;
    public Text TextPlugin;
    bool timerActive = false;
    bool backTimerActive = false;
    AndroidJavaObject JavaTimerClass = new AndroidJavaObject("com.e.timerlibrary.MyPlugin");


    int testvar1;
    // Start is called before the first frame update
    void Start()
    {
        textTimer.text = timeValue.ToString();
        //startService();
    }

    // Update is called once per frame
    void Update()
    {
        if (timerActive)
        {
            timeValue -= Time.deltaTime;
            textTimer.text = timeValue.ToString("f0");
            slider.value = timeValue;

            if (timeValue <= 0)
            {
                timerActive = false;
                textTimerBtn.text = "Start";
                timeValue = 0;
                slider.interactable = true;
            }
        }
    }

    public void OnTimerBtnClick()
    {
        textTimerBtn.text = timerActive ? "Start" : "Stop";
        timerActive = !timerActive;

        slider.interactable = false;

        if (timerActive==false)
        {
            timeValue = 0;
            textTimer.text = timeValue.ToString("f0");
            slider.interactable = true;
            slider.value = 0;
        }


    }

    public void GetValueOfSlider(float value)
    {
        if (timerActive == false)
        {
            textTimer.text = value.ToString("f0");
            timeValue = value;
        }
    }

    void OnApplicationPause(bool pause) //Wird nur jeweils 1 mal ausgeführt!! 
    {
        if (pause)
        {
            //startService1();
            //TextPlugin.text = "pause";
            TextPlugin.text = JavaTimerClass.Call<int>("testmethode").ToString();
        }
        else
        {
            //TextPlugin.text = "unpause";
            stopService1();
        }
    }


    void startService1()
    {
        /* if (!backTimerActive)
        {
          JavaTimerClass.Call("StartTimerService");
          backTimerActive = true;
        } */

        TextPlugin.text = JavaTimerClass.Call<int>("testmethode").ToString();

        //TextPlugin.text = JavaTimerClass.Call<int>("GetTime").ToString();
    }

    void stopService1()
    {
        //TextPlugin.text = JavaTimerClass.Call<int>("StopTimerService").ToString();
        backTimerActive = false;
    }

}

这是java代码:

package com.e.timerlibrary;

public class MyPlugin {
    public int testmethode(){
        return 100;
    }
}

我知道它看起来很糟糕,但我只是想让插件工作

标签: androidandroid-studiounity3dplugins

解决方案


推荐阅读