将数据从Unity传递到本机应用程序-如何? [英] Passing data from Unity to a native app - how to?

查看:140
本文介绍了将数据从Unity传递到本机应用程序-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本机Android应用程序(说NAA)

Native Android Application (Say NAA)

团结游戏(说UG)

我想将统一游戏(UG)集成到本机Android应用程序(NAA)

I want to integrate the unity game(UG) into the Native Android Application (NAA)

我将统一游戏导出到android中,该游戏转换为.aar库,并且能够将UG集成到NAA中.但是我想将统一游戏分数发送到本机android应用程序.

I exported the unity game into android which is converted into .aar library and I am able to integrate the UG into NAA. But I want to send the unity game score to native android application.

AndroidManagerScript.

AndroidManagerScript.

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

public class AndroidManager : MonoBehaviour {

    private AndroidJavaObject curActivity;
    public string strLog = "No Java Log";
    static AndroidManager _instance;
    public Text _text;
    public static AndroidManager GetInstance()
    {
        if( _instance == null )
        {
            _instance = new GameObject("AndroidManager").AddComponent<AndroidManager>();   
        }
        return _instance;
    }
    void Awake()
    {

        #if UNITY_ANDROID 
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        //AndroidJavaClass jc = new AndroidJavaClass("great.good.com.rolauncher");
        curActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
        #endif


        _text = (Text)GameObject.Find ("Canvas/Text").GetComponent<Text>();
        _text.text = strLog;
    }
    void Start ()
    {
    }
    public void CallJavaFunc( string strFuncName, string strTemp )
    {
        if( curActivity == null )
        {
            strLog = curActivity + " is null";
            return;
        }

        strLog = "Before call " + strFuncName;
        //curActivity.Call(strFuncName, new object[] { strTemp } );

        curActivity.Call( strFuncName, strTemp );
        strLog = strFuncName + " is Called with param " + strTemp;
    }
    void SetJavaLog(string strJavaLog)
    {
        strLog = strJavaLog;
        _text.text = strLog;
    }
}

我在游戏退出时正在调用函数javaTestFunc

I am calling a function javaTestFunc on game exit

public void GQuit()
        {
            AndroidManager.GetInstance().CallJavaFunc( "javaTestFunc", "UnityJavaJarTest" );
            
            Application.Quit();
        }

本地Android代码

Native Android Code

import com.Hash.FC.UnityPlayerActivity;

public class MainActivity extends com.unity3d.player.UnityPlayerActivity {

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button)findViewById(R.id.button);
        Log.e("data","start");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, UnityPlayerActivity.class);
                startActivity(i);
            }
        });
    }

    public void javaTestFunc(String strFromUnity) {

        //java to unity
        //UnityPlayer.UnitySendMessage("AndroidManager", "SetJavaLog", strFromUnity + "HelloWorld");
        Log.e("data",strFromUnity);

    }

    public static void testMethod(){
        Log.e("data","data");
    }
}

JavaTestFunc没有触发.任何帮助将不胜感激.

JavaTestFunc is not triggering. Any Help will be appreciated.

推荐答案

一个想法是将数据保存在persistentDataPath中: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

An idea is to just save data in the persistentDataPath: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

,然后从您的本机应用程序读取该文件夹. 我不确定这一限制,因为我只是将Unity3d与本机PC应用程序一起使用(我不确定是否可以读取android上的/storage/emulated/0/Android/data//files的persistentDataPath.另一个应用程序),但如果可能的话,您只需要从本机应用程序中读取数据,然后从统一应用程序中写入数据即可.

and read that folder from your native app. I'm not sure about the limitation of this since I just use Unity3d with a native PC application (I'm not sure it is possible to read the persistentDataPath that is /storage/emulated/0/Android/data//files on android with another app) but if it is possible you will just need to read data from there from your native app and write data there from your unity app.

这篇关于将数据从Unity传递到本机应用程序-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆