c# - 如何从 Unity 应用程序启动 Android Activity ?

标签 c# android mono unity3d interprocess

我知道这似乎是一个微不足道的问题,但我无法在互联网上的任何地方找到任何具体的答案。我在 stackoverflow 上看到了这个非常相似的问题:How to start Unity application from android activity? 但这与我的问题完全相反。此外,android Activity 必须能够从 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");
    }
}

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

最佳答案

我对 Unity 不是很熟悉,但有相当多的 Android 经验。因此,请将我的回答作为建议而非权威回答。

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

按照 Integrating Unity with 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);
    }
}

并修改你的 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 消息。

关于c# - 如何从 Unity 应用程序启动 Android Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069340/

相关文章:

c# - DevExpress XtraTreeList 禁用节点编辑

c# - 如何将 std::string& SWIG 转换为 C# 引用字符串

android - 我有使 ImageView 变圆的代码,现在我想使用背景

android - 如何找回android系统底部的按键?

linux - 在 Linux (mono) 上运行的 TeamCity NUnitLauncher 出现 "Corlib not in sync with this runtime"错误

c# - Mono 仅在某些计算机上获得 "System.ExecutionEngineException: SIGILL "

c# - 使用 C# MVC4 Razor 将表单输入保存到数据库中?

c# - Newtonsoft JSON - 动态对象

android - NDK 问题

c# - 将 C# WPF .NET 4.0 应用程序移植到 Mac OS X(Mono?)的建议