繁体   English   中英

如何从 Unity 应用程序启动 Android 活动?

[英]How to start an Android activity from a Unity Application?

我知道这似乎是一个微不足道的问题,但我无法在 inte.net 上的任何地方找到任何具体答案。 我在 stackoverflow 上看到了这个非常相似的问题: How to start Unity application from android activity? 但这与我的问题完全相反。 此外,android 活动必须能够从 Unity 应用程序接收一些输入字符串,就像使用 system() 调用行 arguments 在 PC 上启动另一个程序一样。

以下是我在 Android 上测试 Unity 应用程序的测试按钮事件处理程序的代码:

private void ExternalAppCallHandler()
{
    if(Application.platform == RuntimePlatform.WindowsEditor)
    {
        Process.Start(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
    }
    else if(Application.platform == RuntimePlatform.Android)
    {
        Process.Start("Internet");
    }
}

当我使用 Unity Editor 进行测试时,当我点击测试按钮时,应用程序成功打开了 Notepad++.exe。 但是,当我尝试在我的 Samsung Galaxy S2 设备上打开“Inte.net”应用程序时,它失败了。 有谁知道为什么会这样? 使用 Process.Start 打开另一个 Android 应用程序的正确字符串应该是什么?

我对 Unity 不是很熟悉,但有相当多的 Android 经验。 所以把我的回答当作建议而不是权威的回答。

查看 Launching an Android application from within Unity ,您可以尝试以下操作:

按照Unity 与 Eclipse 的集成指南进行操作。

修改第一步创建的Java文件如下:

package com.Unity3D.EclipseIntegration;

import android.os.Bundle;

import com.unity3d.player.UnityPlayerActivity;

public class EclipseIntegration extends UnityPlayerActivity {

    private Intent myIntent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Assuming that we want to launch the browser to browse a website
        Uri uri = Uri.parse("http://www.google.com");
        myIntent= new Intent(Intent.ACTION_VIEW, uri);
    }

    public void Launch()
    {       
        startActivity(myIntent);
    }
}

并修改您的 Unity 代码:

private void ExternalAppCallHandler()
{
    if(Application.platform == RuntimePlatform.WindowsEditor)
    {
        Process.Start(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
    }
    else if(Application.platform == RuntimePlatform.Android)
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("Launch");
    }
}

如果您遇到任何问题,请发布 LogCat 消息。

试试这个将此 Launch() 方法更改为 static 并传递 Android java object 即。 像下面这样给它“jo”。

AndroidJavaClass androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo= androidJC.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass jc = new AndroidJavaClass("package_name.Ur_Actvity_Name");
jc.CallStatic("Launch",jo);`

并将 Launch() 方法更改为:

public static Launch(Activity activity)
{
 Intent myIntent = new Intent();
 activity.startActivity(myIntent);
}

希望它会有所帮助。

暂无
暂无

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

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