[英]Return from unity activity to MainActivity in android studio
[英]How to start an Android activity from a Unity Application?
我知道這似乎是一個微不足道的問題,但我無法在 inte.net 上的任何地方找到任何具體答案。 我在 stackoverflow 上看到了這個非常相似的問題: How to start Unity application from android activity? 但這與我的問題完全相反。 此外,android 活動必須能夠從 Unity 應用程序接收一些輸入字符串,就像使用 system() 調用行 arguments 在 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 設備上打開“Inte.net”應用程序時,它失敗了。 有誰知道為什么會這樣? 使用 Process.Start 打開另一個 Android 應用程序的正確字符串應該是什么?
我對 Unity 不是很熟悉,但有相當多的 Android 經驗。 所以把我的回答當作建議而不是權威的回答。
查看 Launching an Android application from within Unity ,您可以嘗試以下操作:
按照Unity 與 Eclipse 的集成指南進行操作。
修改第一步創建的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 消息。
試試這個將此 Launch() 方法更改為 static 並傳遞 Android java object 即。 像下面這樣給它“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);`
並將 Launch() 方法更改為:
public static Launch(Activity activity)
{
Intent myIntent = new Intent();
activity.startActivity(myIntent);
}
希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.