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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10069340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:20:09  来源:igfitidea点击:

How to start an Android activity from a Unity Application?

c#androidmonounity3dinterprocess

提问by ksming

I know this seems to be a trivial question but I could not find any concrete answer anywhere on the internet. I saw this very similar question on 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.

我知道这似乎是一个微不足道的问题,但我在互联网上的任何地方都找不到任何具体的答案。我在 stackoverflow 上看到了这个非常相似的问题:How to start Unity application from android activity? 但这与我的问题完全相反。此外,android 活动必须能够从 Unity 应用程序接收一些输入字符串,就像使用带有行参数的 system() 调用在 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. However, when I tried to open the "Internet" app on my Samsung Galaxy S2 device it failed. Does anyone knows why this is the case? What should be the correct string for opening another Android application using Process.Start?

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

采纳答案by Rajesh

I am not very familiar with Unity, but have a fair amount of Android experience. So take my answer as a suggestion rather than an authoritative answer.

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

Looking at the Launching an Android application from within Unity, you could try the following:

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

Follow the Guide on Integrating Unity with Eclipse.

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

Modify the Java file created in Step 1 as below:

修改步骤 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);
    }
}

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 消息。

回答by Ashish Dhore

try this Change this Launch() method to static and pass Android java object ie. "jo" to it like below.

试试这个 将此 Launch() 方法更改为静态并传递 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);`

and change Launch() method to :

并将 Launch() 方法更改为:

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

Hope it will help.

希望它会有所帮助。