如何从 Unity 应用程序启动 Android 活动? [英] How to start an Android activity from a Unity Application?

查看:54
本文介绍了如何从 Unity 应用程序启动 Android 活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

The following is the code I have for a test button event handler for my test Unity app on Android:

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

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

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 不是很熟悉,但有相当多的 Android 经验.所以把我的回答当作一个建议而不是一个权威的答案.

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 与 Eclipse 集成.

修改在步骤 1 中创建的 Java 文件如下:

Modify the Java file created in Step 1 as below:

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 代码:

and modify your Unity code:

private void ExternalAppCallHandler()
{
    if(Application.platform == RuntimePlatform.WindowsEditor)
    {
        Process.Start(@"C:Program Files (x86)Notepad++
otepad++.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 消息.

In case you are facing any problems, please post the LogCat messages.

这篇关于如何从 Unity 应用程序启动 Android 活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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