How to start an Android activity from a Unity Application?

前端 未结 2 1773
天涯浪人
天涯浪人 2021-01-31 06:09

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 Uni

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 06:34

    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.

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

    Follow the Guide on Integrating Unity with Eclipse.

    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);
        }
    }
    

    and modify your Unity code:

    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("currentActivity");
            jo.Call("Launch");
        }
    }
    

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

提交回复
热议问题