20

Anyone know integrate Android with Unity Studio? (i will explain)
I created a simple scene in Unity (4.3.x on OSX Maverics) for testing. Has a 3D object and nothing else.
I do this in XCode using the Stackoverflow explanations here and i post my complete code here ( Touch a UIButton and show Unity on UIView or UIViewController ) to show it's really simple.
But now, I need to do the same on Android Studio (which I installed and I can export the project from Unity to Android)
The only thing I know is that the AndroidManifest.xml and the file is in "res/layout/(something).xml" files that are the first (read) and (displays) the layout on the screen when you create a project on Android Studio.

When you open the generated project from Unity, the only XML you have is AndroidManifest.
So I'm lost. Since the document of third part site (http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/android-GettingStarted.html) only mentions the Unity Eclipse and a JAVA files that do not exist in the project, makes me more lost.

In Objective-C, you create your somethingDelegate.mm and .h and inserts "a line of code" and ready. In Android does not seem to be as simple as this official document (http://docs.unity3d.com/Manual/PluginsForAndroid.html ) says.

Someone already did this in the Android Studio and could help me?

Thanks in advance.

** Edited for bounty: **

Need to create 3 views:
1 - Main View with 2 buttons;
2 - One button go to a second View;
3 - Other button go to Unity View;

There must be a simple way to do this on Android as a studio made ​​the link above.

Community
  • 1
  • 1
Daniel Arantes Loverde
  • 2,317
  • 2
  • 28
  • 41
  • 1
    Last time I checked Unity supported only Eclipse projects. Moreover, since Android Studio is still in beta, I believe Unity has not made any effort to support it yet. – ozbek Aug 04 '14 at 01:40
  • Since Android Studio and Unity work with JAVA and C# i believe that is a way to do it, because Unity export Google project using JDK, and others DK that is built in on Android Studio. So, theres a way... i think... rs – Daniel Arantes Loverde Aug 04 '14 at 12:51
  • What exactly is your problem? Importing the project into Android Studio is no problem. What are you trying to achieve after importing the project? – Stefan Hoffmann Aug 05 '14 at 00:06
  • im trying to integrate with unity, like: create a simple page with 2 buttons, one button is another view and the other is unity. Only that. But in Android Studio, is not that simple... – Daniel Arantes Loverde Aug 05 '14 at 00:09
  • Have you got any luck? I have tried integrating a unity project with android using eclipse and its done. two buttons and a UnityPlayerNativeActivity generated by unity software. Please let me know how can i help. – MOSO Aug 09 '14 at 09:25
  • Not Eclipse, Android Studio – Daniel Arantes Loverde Aug 09 '14 at 13:35

3 Answers3

22

Setting up an Android project is really simple. The document you linked is outdated and you don't need to move any files at all.

Unity

  1. Goto File -> Build Settings -> Android and click Switch Platform
  2. Enable Google Android Project
  3. Click Export and choose where you want the Android project to be

Android Studio

  1. Import or open the project you have just created with Unity
  2. Done :)

If you want to display the Unity engine inside a subactivity, I suggest you take a look at this answer.

If you're completely unfamiliar with Android development, you should start with a simple "Hello World" app without complicating things by adding Unity to the mix. Just follow some of the official Android guides.

Community
  • 1
  • 1
Stefan Hoffmann
  • 3,214
  • 16
  • 30
  • 1
    I already do that, my question is how to integrate by creating 3 views, one, default with 2 buttons, one go to another view and other go to unity, only this code i need explained. – Daniel Arantes Loverde Aug 05 '14 at 15:18
  • 3
    @JackLoverde sorry, but I will not write you a step-by-step tutorial how to add 2 buttons and several views. Embedding Unity is explained in my linked answer and for the rest read the linked guides. If you have trouble with Android Studio, google for a tutorial or ask a specific question. – Stefan Hoffmann Aug 07 '14 at 18:05
  • I Google it and i found ONLY Eclipse. Thanks for your time – Daniel Arantes Loverde Aug 07 '14 at 18:09
  • @JackLoverde \*sigh\* googling for "Android Studio tutorial", [Android Adventures - Getting Started With Android Studio](http://www.i-programmer.info/programming/android/5887-android-adventures-getting-started-with-android-studio.html) They explain *everything* in detail as far as I can tell. – Stefan Hoffmann Aug 07 '14 at 18:17
  • I've read everything, EVERYTHING, including this site that pointed me now, before coming here and donate my points to anyone who could help me. Thanks for your time. – Daniel Arantes Loverde Aug 07 '14 at 18:20
  • @JackLoverde 12 mins ago you said you have only found Eclipse-related stuff... I'm out – Stefan Hoffmann Aug 07 '14 at 18:23
  • Android Studio AND Unity integration, search only returns Eclipse... try that... if you found a SIMPLE integration for Android Studio AND Unity and i could build it, i give you my points... – Daniel Arantes Loverde Aug 07 '14 at 18:28
  • 2
    @leosori - sorry to resurrect this thread - I have a minor workflow Q w/r/t Android Studio. if I "open" my unity export with A.S., I have to set up a bunch of configurations and things don't seem quite right. if I "import" the unity export, things seem pretty good - it runs and everything, but the minor hiccup is that A.S. has duplicated my project files - which leaves me wondering how to make changes in unity and get them into A.S. - reimport the project ? Or can I use symlinks ? Or setup gradle to point to the previous project ? TIA, and sorry for the android noob Q's. – orion elenzil Dec 15 '14 at 17:38
  • 1
    @StefanHoffmann, this does not address the OP question. OP is asking how to launch a Unity window INSIDE an Android app. NOT open a Unity project in Android Studio. If you review the link he referred to about doing the same thing in Xcode, you would have realized that. – kfblake Apr 25 '17 at 21:19
  • @kfblake And if you had followed my link you would have realized that it answers that problem. His question was unclear from the beginning (especially before his edit after my answer was posted). If he knew how to get it running under Android Studio he could have left this whole part out. If he doesn't know how to launch any kind of activity at all, he needs an Android tutorial, which I also provided. – Stefan Hoffmann Apr 27 '17 at 07:54
  • 1
    @StefanHoffmann. Again, not addressing the OP original question. I'm doing something very similar and am coming into the exact same issue. Arguing about it in comments is not going to help anyone. OP is asking how to run Unity in an activity side by side with a native Activity. For iOS and Eclipse, it is a very involved, detailed process. It is unclear how to do this same thing in Android Studio. – kfblake Apr 27 '17 at 14:20
  • 1
    How to close the unity app game and go back to main activity? – Bluetree Aug 15 '18 at 05:01
14

I integrated a Unity project to an Android application.

I had a simple activity with a button created using Android Studio.

On clicking this button the Unity scenes will start.

My Activity Unity Activity


The android project exported from unity should like this: enter image description here


It needed to be converted to Gradle project first.

For that:

  1. Open Android Studio -> choose import project -> select the unity project (Non-Gradle project) -> save project and close.

Now your unity project folder looks like this:

enter image description here


Now open your project and do the following to import the unity project in to your project:

  1. Click File -> New -> New Module -> Select "import Gradle Project" -> choose the unity gradle project folder you just created -> rename module name if prompted by checking the “import” checkbox in Android studio -> Click Finish
  2. Verify in Settings.gradle file to see if the new module name is added in the include command:

    include ':app', ':yournewmodulename’
    
  3. Open build.gradle file of the new unity module from navigation window and rename the below line:

    apply plugin: 'com.android.application'
    

    to

    apply plugin: 'com.android.library'
    
  4. Remove the line from the defaultConfig section of the new unity module:

    applicationId "com.xxx.xxx"
    
  5. Open Manifest of this new unity module and comment the application tag (Do not remove the uses-sdk tag).Integrate the new unity module's Manifest with your project’s Manifest by moving necessary tags to our project’s Manifest (e.g. :Activity, uses-feature etc.).

  6. Open build.gradle of your project and add below line in the dependencies section:

    dependencies {
    …
    compile project(path: ':yournewmodulename')
    
    }
    

Now on button click you can call the Activity in the unity module from your activity as below:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.xxx.xxx.UnityPlayerActivity;

public class MainActivity extends AppCompatActivity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button= (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, UnityPlayerActivity.class);
            startActivity(i);
        }
    });
}
}
mustaq
  • 961
  • 14
  • 16
  • How to close the unity app game and go back to main activity? – Bluetree Aug 15 '18 at 05:02
  • 2
    You can create function with "finish()" method in your "UnityPlayerActivity" class. And call it from unity game when the user wants to exit the game. https://stackoverflow.com/a/27730800/3629513 – mustaq Sep 02 '18 at 04:14
  • Hello mustaq I have successfully integrated as you said so can you please tell how to pass a string from android to the unity game? My question link is https://stackoverflow.com/questions/59370546/retrieve-data-from-android-activity-to-unity3d please review my question – Suhail Pappu Dec 18 '19 at 14:51
  • Hi Suhail, as per your last comment in the question you shared, I believe you found the solution. – mustaq Dec 20 '19 at 06:45
  • Any new techniques? This is not working on new android studio and new unity. Now unity has created library with launcher that has no java activity. Any new techniques or tutorial or suggestions please.. –  May 06 '22 at 07:31
4

For open a Android Studio project from Unity3D inside Android studio as march 2016 you need to File -> New -> Import and let the thing do his work.

Or if you have the quick start window:

enter image description here

tyoc213
  • 1,223
  • 18
  • 21