简体   繁体   English

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

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

I know this seems to be a trivial question but I could not find any concrete answer anywhere on the inte.net.我知道这似乎是一个微不足道的问题,但我无法在 inte.net 上的任何地方找到任何具体答案。 I saw this very similar question on stackoverflow: How to start Unity application from android activity?我在 stackoverflow 上看到了这个非常相似的问题: How to start Unity application from android activity? but it is exactly opposite from my question.但这与我的问题完全相反。 Additionally the android activity must be able to receive some input strings from the Unity application much like how one use system() calls with line arguments to start another program on a PC.此外,android 活动必须能够从 Unity 应用程序接收一些输入字符串,就像使用 system() 调用行 arguments 在 PC 上启动另一个程序一样。

The following is the code I have for a test button event handler for my test Unity app on Android:以下是我在 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");
    }
}

When I use Unity Editor to test, the application successfully opens Notepad++.exe when I click on the test button.当我使用 Unity Editor 进行测试时,当我点击测试按钮时,应用程序成功打开了 Notepad++.exe。 However, when I tried to open the "Inte.net" app on my Samsung Galaxy S2 device it failed.但是,当我尝试在我的 Samsung Galaxy S2 设备上打开“Inte.net”应用程序时,它失败了。 Does anyone knows why this is the case?有谁知道为什么会这样? What should be the correct string for opening another Android application using Process.Start?使用 Process.Start 打开另一个 Android 应用程序的正确字符串应该是什么?

I am not very familiar with Unity, but have a fair amount of Android experience.我对 Unity 不是很熟悉,但有相当多的 Android 经验。 So take my answer as a suggestion rather than an authoritative answer.所以把我的回答当作建议而不是权威的回答。

Looking at the Launching an Android application from within Unity , you could try the following:查看 Launching an Android application from within Unity ,您可以尝试以下操作:

Follow the Guide on Integrating Unity with Eclipse .按照Unity 与 Eclipse 的集成指南进行操作。

Modify the Java file created in Step 1 as below:修改第一步创建的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);
    }
}

and modify your Unity code:并修改您的 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");
    }
}

In case you are facing any problems, please post the LogCat messages.如果您遇到任何问题,请发布 LogCat 消息。

try this Change this Launch() method to static and pass Android java object ie.试试这个将此 Launch() 方法更改为 static 并传递 Android java object 即。 "jo" to it like below.像下面这样给它“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);`

and change Launch() method to:并将 Launch() 方法更改为:

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

Hope it will help.希望它会有所帮助。

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

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