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

ksm*_*ing 15 c# mono android interprocess unity-game-engine

我知道这似乎是一个微不足道的问题,但我无法在互联网上的任何地方找到任何具体的答案.我在stackoverflow上看到了这个非常相似的问题:如何从android活动启动Unity应用程序? 但它与我的问题完全相反.此外,android活动必须能够从Unity应用程序接收一些输入字符串,就像使用system()调用行参数以在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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Raj*_*esh 9

我对Unity不太熟悉,但拥有相当多的Android体验.所以把我的答案作为建议而不是权威的答案.

查看从Unity中启动Android应用程序,您可以尝试以下操作:

遵循将Unity与Eclipse集成的指南.

修改在步骤1中创建的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并修改您的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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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


小智 7

试试这个将此Launch()方法更改为static并传递Android java对象即.就像下面的"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);`
Run Code Online (Sandbox Code Playgroud)

并将Launch()方法更改为:

public static Launch(Activity activity)
{
 Intent myIntent = new Intent();
 activity.startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助.