我知道这似乎是一个微不足道的问题,但我无法在互联网上的任何地方找到任何具体的答案。我在 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/