Search Unity

Using Unity Android In a Sub View

Discussion in 'Android' started by markharkness, Jul 27, 2011.

  1. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    With the eclipse guide removed from the docs I have updated this to go from start to end in doing this.

    First thing is to get your unity project set up and built.
    As a basic example just create a spinning cube project then build for android ( obviously ).

    The next step is to move some files around.
    Go into your project directory and you will see a Temp folder in there you will see a Staging Area folder. You need to copy that folder out with the Temp folder. It doesn't matter where to but as long as it not in the Temp folder. The reason for this is when you use the unity project in eclipse if you use the staging area in temp folder directly when unity rebuilds it changes the contents and this leads to problems with eclipse.

    Now you need to open up eclipse.

    Once open create a new android project and select create from existing source and point to the staging area folder that you created. This will give you the unity application in eclipse. I called this UnityLib for ease of reference in the future.

    Next thing to do is to make another android project in eclipse.
    The two important parts here are to make sure you have the correct API levels and the package name matches that of the package name you gave for the project in unity. I called this Unity Java for ease of reference.

    Now we need to change a bunch of settings and move some file around.

    * Right Click UnityLib and select properties. Then under android check IsLibrary, Apply, OK
    * Right Click UnityJava and select properties. Then under Java build Path there are a few things that need to be done
    * First click external jar then navigate to your unity install directory then select Data\PlaybackEngines\androidplayer\bin\classes.jar
    * Then under android you need to add the library UnityLib.

    * now move the folder and their contents under the asset folde rin UnityLIb to the asset folder in UnityJava
    * next move the AndroidManifest from the UnityLib to UnityJava

    Now you need to make some small code changes

    * in the main source file in UnityJava you need to extend from UnityPlayerActivity and not Activity.
    * if you press ctrl+shift+O after doing that it will automatically resolve the dependencies
    * next you need to remove the line of code setContentView(R.layout.main);

    And that should be it if you now run the UnityJava applicaiton for android it will build and run and you will see your unity content launched through an eclipse project.



    Now assuming you have managed that without running into any errors its time to do some work in eclipse.


    Repairing the damage we have already caused

    1) The class needs to go back to extending a normal activity and not the unity one.

    Creating the Frame Layout We Require

    Under the Java part of the project( as I referred to it in the docs ) expand the res folder to res->layout then double click main.xml. You should then see the main.xml open up. Notice there are now two tabs at the bottom one for the graphical layout and the other for the actual xml.
    I prefer to do it by manually editing some of the xml to start with. I suspect that by default that a LinearLayout and a Textview will have been created for you in the xml. Delete it all apart from the <?xml version="1.0" encoding="utf-8"?> line. This gives us a blank slate to work on in the much friendlier Graphical Layout tab.
    In the Graphical Layout drag a FrameLayout onto the screen area. Then drag another view onto the screen area. This second view can then be resized and will represent the area of the screen where you want the unity player to render.
    That's it for setting up the view.

    Gratuitous ScreenShot



    The code

    A large portion of the code that is being used in here is the same as the code that is being used in the UnityPlayerActivity.java file that you would find in
    C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\src\com\unity3d\player\


    • You will need a UnityPlayer variable to start off with.

    • The Unity Player then needs to be setup
      Code (csharp):
      1.         // Create the UnityPlayer
      2.         m_UnityPlayer = new UnityPlayer(this);
      3.         int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
      4.     boolean trueColor8888 = false;
      5.     m_UnityPlayer.init(glesMode, trueColor8888);

    • Then the unity player needs to be allocated to a view to get the player to run.
      Code (csharp):
      1.  
      2.         // Add the Unity view
      3.         FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);    
      4.         LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      5.         layout.addView(m_UnityPlayer.getView(), 0, lp);

    The important part of this is the line of code:
    Code (csharp):
    1. FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
    This will determine which framelayout we add the view to render unity in.
    The "R.id.framelayout2" is and autogenerated file that you can find in the project under the gen folder.

    Here is the complete source file:

    Code (csharp):
    1. package com.unity3d.viewexample;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.FrameLayout;
    7. import android.widget.LinearLayout.LayoutParams;
    8.  
    9. import com.unity3d.player.UnityPlayer;
    10.  
    11.  
    12. public class JavaCubeViewActivity extends Activity {
    13.     private UnityPlayer m_UnityPlayer;
    14.        
    15.     /** Called when the activity is first created. */
    16.     @Override
    17.     public void onCreate(Bundle savedInstanceState) {
    18.         super.onCreate(savedInstanceState);
    19.        
    20.         // Create the UnityPlayer
    21.         m_UnityPlayer = new UnityPlayer(this);
    22.         int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
    23.         boolean trueColor8888 = false;
    24.         m_UnityPlayer.init(glesMode, trueColor8888);
    25.  
    26.         setContentView(R.layout.main);
    27.        
    28.         // Add the Unity view
    29.         FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);    
    30.         LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    31.         layout.addView(m_UnityPlayer.getView(), 0, lp);
    32.     }
    33. }
    34.  
    Did you just say pics or it didn't happen.

     
    Last edited: Jun 29, 2012
    coeing and AxelMG like this.
  2. PaulT

    PaulT

    Joined:
    Oct 25, 2008
    Posts:
    17
    Thanks Rolf! It seems like this did not get any intention from anyone here on the forums. But I think this could be really helpful to some people.

    For my current project I have reverse engineered the above steps. Too bad you did not post it 3 weeks ago ;-)

    Anyway, I have a still open question for which I did not found an answer yet. In addition to the above, how would you detect whether or not the Unity view has been fully initialized (Splash screen is gone)? It could make sense in some situation to wait adding native android element, until the scene is present and can be used. I could call something from Unity on a AndroidJavaClass instance myself, but that is not what I prefer to do.

    Do you or anyone else has thoughts on that?
     
  3. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
  4. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    Haha yeah it's something I've been sitting on for a while but other commitments got in the way.

    Using the AndroidJavaClass would be the best way. Just call something in a start function in a script attached to your main camera in your main scene... that was a mouthfull.
     
  5. aneeshere

    aneeshere

    Joined:
    Aug 11, 2011
    Posts:
    6
    I am not able to integrate unity with eclipse. I donno why I cant but when I do all the steps and run the java project there seems to be an error. its showing invalid apk file
     
  6. zalogic

    zalogic

    Joined:
    Oct 6, 2010
    Posts:
    273
    RoflHarris, do you know if the path to the Unity "Data" folder can be overridden on the Android using a custom activity?

    I would like to set the engine to look for it's assets in another path instead that of the application folder and I can't seem to find any solution to this.
     
  7. lbalasubbaiahapps

    lbalasubbaiahapps

    Joined:
    Oct 6, 2011
    Posts:
    23
    Resource re-package Failed!
    package -v -f -m -J gen -M AndroidManifest.xml -S "res" -I "/Users/admin/Public/android-sdk-mac_x86/platforms/android-14/android.jar" -F bin/resources.ap_
    Configurations:
    (default)

    Files:
    drawable/app_icon.png
    Src: () res/drawable/app_icon.png
    values/strings.xml
    Src: () res/values/strings.xml
    AndroidManifest.xml
    Src: () AndroidManifest.xml

    Resource Dirs:
    Type drawable
    drawable/app_icon.png
    Src: () res/drawable/app_icon.png
    Type values
    values/strings.xml
    Src: () res/values/strings.xml
    Including resources from package: /Users/admin/Public/android-sdk-mac_x86/platforms/android-14/android.jar
    applyFileOverlay for drawable
    applyFileOverlay for layout
    applyFileOverlay for anim
    applyFileOverlay for animator
    applyFileOverlay for interpolator
    applyFileOverlay for xml
    applyFileOverlay for raw
    applyFileOverlay for color
    applyFileOverlay for menu
    applyFileOverlay for mipmap
    Processing image: res/drawable/app_icon.png
    (processed image res/drawable/app_icon.png: 68% size of source)
    (new resource id app_icon from drawable/app_icon.png #generated)
    AndroidManifest.xml:4: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_launcher').

    UnityEngine.Debug:LogError(Object)
    PostProcessAndroidPlayer:postProcess(BuildTarget, String, String, String, String, String, String, BuildOptions)
    UnityEditor.HostView:OnGUI()




    I am Getting this error.In my Project i dont have any errors but when Build the project i am getting this error. i followed this reference also but i got issue file://localhost/Applications/Unity/Unity.app,
    file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/Manual/Android-Integrating%20Unity%20With%20Eclipse.html
    /Contents/Documentation/Documentation/Manual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html any help friends how integrate eclipse in unity
     
    Last edited: Dec 1, 2011
  8. dimat

    dimat

    Joined:
    Feb 11, 2012
    Posts:
    1
    Is it possible to make unity view transparent and show it on top of android UI?
     
    TroyDraws likes this.
  9. tryfrie

    tryfrie

    Joined:
    Apr 3, 2012
    Posts:
    2
    hi.. thx for this.. i was trying to make something similar with the qualcomm AR lib.. the QCARPlayer in a Subview.. Do you have any idea how that could be done? thx..
     
  10. Asse83

    Asse83

    Joined:
    Aug 1, 2009
    Posts:
    169
    Hmm.. for some reason when the application starts I get an errors saying "Could not find class 'com.unity3d.player.UnityPlayer.." but I've referenced the classes.jar in the build settings.

    Any help?
     
  11. Asse83

    Asse83

    Joined:
    Aug 1, 2009
    Posts:
    169
    Ok, Unity 3.5.1 works!

    Any idea how to call C# code from Java? Or just to comunicate with the C# code somehow?
     
    Last edited: May 3, 2012
  12. txasti

    txasti

    Joined:
    Oct 26, 2010
    Posts:
    10
    Asse, did it work for you? What did you do? I'm trying with 3.5.2 version and I have the same problem, it crashes after loading. :-(
     
  13. DragonEagle

    DragonEagle

    Joined:
    May 19, 2012
    Posts:
    3
    Make sure the checkbox next to the classes.jar file is checked. that tells eclipse to export the file out with the package. I fought with the same problem for the better part of the night.
     
  14. mikolo

    mikolo

    Joined:
    Nov 27, 2009
    Posts:
    249
    Hi to all, i made a nice tutorial (4 Parts) how to work with Unity and Eclipse! Check it out here, an send me a feedback please -> http://www.youtube.com/watch?v=zxn2CuQszvw

    So what i want to do now.. i want to start a Webview Activity from a GUI Button and stop the game Activity. And then close the Webview Activity and go back to the game! If somebody knows how to do i can continue the tutorial..it would be nice. :)
     
  15. mimesis

    mimesis

    Joined:
    May 22, 2012
    Posts:
    4
    ok found the solution as framelayout2 isnt the view id but layout id ^^'

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framelayout2"

    thanks anyways...

    -----------------------------------------------------------------------------------------------------------------------------------------------

    can you post a sub-view demo project please ? whatever I do, I am getting " Caused by: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.FrameLayout" error :(

    main.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    </FrameLayout>

    Code:

    private UnityPlayer m_UnityPlayer;
    private FrameLayout layout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //Create the UnityPlayer
    m_UnityPlayer = new UnityPlayer(this);
    int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
    boolean trueColor8888 = false;
    m_UnityPlayer.init(glesMode, trueColor8888);
    setContentView(R.layout.main);

    // Add the Unity view
    layout = (FrameLayout) findViewById(R.id.view1);
    LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    layout.addView(m_UnityPlayer.getView(), 0, lp);

    }
     
    Last edited: Jun 21, 2012
  16. mikolo

    mikolo

    Joined:
    Nov 27, 2009
    Posts:
    249
    Hello, i have succesfully done this by following code. (This time sorry i have no time for another video tutorial :( )

    A kind of webview start by klicking a button in Unity and brings a view to front of Unity Player...now the problem is to close the webview again. Somebody knows how to do? And maybe also to pause the game? And also make the webview zoomable, because this also doesn't work?

    PHP:
    package com.babyninja.babyninjadance;

    import android.app.ActionBar.LayoutParams;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.unity3d.player.UnityPlayer;
    import com.unity3d.player.UnityPlayerActivity;

    public class 
    MyStartActivity extends UnityPlayerActivity {
        
        @
    Override
        
    public void onCreate(Bundle savedInstanceState) {
            
    super.onCreate(savedInstanceState);
        }
        
        @
    Override
        
    public void onBackPressed()
        {
           
    // instead of calling UnityPlayerActivity.onBackPressed() we just ignore the back button event
           
    super.onBackPressed();
           
    Log.d("onBackPressed","onBackPressed");
        }
        
        public static 
    void setupViewStatic() {
            
    Log.d("setupViewStatic","setupViewStatic");
            
            
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
                public 
    void run() {
                    
                    
    // And this is the same, but done programmatically
                    
    LinearLayout layout = new LinearLayout(UnityPlayer.currentActivity.getApplicationContext());
                    
    layout.setOrientation(LinearLayout.HORIZONTAL);
                    
                    
    // SET HERE IF YOU WANT THE BANNER TO BE DISPLAYED AT THE TOP OR BOTTOM OF THE SCREEN
                    
    layout.setGravity(Gravity.BOTTOM);
                    
    UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENTLayoutParams.FILL_PARENT));
                    
                    
    //FIRST positive test
                    /*
                    TextView text = new TextView(UnityPlayer.currentActivity);
                    text.setText("This is an example for the Bright Hub!"); 
                    
                    layout.addView(text);
                    */
                    
                    
                    
                    
    WebView myWebView = new WebView(UnityPlayer.currentActivity);
                    
    myWebView.getSettings().setJavaScriptEnabled(true);
                    
    myWebView.loadUrl("http://www.asteam.de/game_news_iphone3.php");
                    
                    
    myWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this if ROM supports Multi-Touch     
                    
    myWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
                    
    myWebView.getSettings().setLoadWithOverviewMode(true);
                    
    myWebView.getSettings().setUseWideViewPort(true);
                    
                    
    layout.addView(myWebView);
                    
                    
    //AdView adView = new AdView(UnityPlayer.currentActivity);
                    //layout.addView(myWebView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                    //adView.setBackgroundColor(0xff000000);
                    //adView.setPrimaryTextColor(0xffffffff);
                    //adView.setSecondaryTextColor(0xffcccccc);
                    
                    
                
    };
            });
            
        }
        
    }
     
  17. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Hello i have the code without errors but when i run in logCat i have the follow error:
    Could not find class 'com.unity3d.player.UnityPlayer', referenced from method com.unity3d.viewexample.JavaCubeViewActivity.onCreate

    I had attached the unity library "classes" and I dont know what is wrong, please help me, if finally it works, I will upload the project. :D

    Thanks all and nice tutorial!

    ----------------------------

    I solved this problem but i have the error:

    PHP:
     E/Unity(604): Unable to locate player settingsbin/Data/settings.xml
     D
    /AndroidRuntime(604): Shutting down VM
     W
    /dalvikvm(604): threadid=1thread exiting with uncaught exception (group=0x40015560)
     
    E/AndroidRuntime(604): FATAL EXCEPTIONmain
     E
    /AndroidRuntime(604): java.lang.UnsatisfiedLinkErrorCouldn't load mono: findLibrary returned null
     E/AndroidRuntime(604):     at java.lang.Runtime.loadLibrary(Runtime.java:429)
     E/AndroidRuntime(604):     at java.lang.System.loadLibrary(System.java:554)
     E/AndroidRuntime(604):     at com.unity3d.player.UnityPlayer.<init>(Unknown Source)
     E/AndroidRuntime(604):     at com.unity3d.viewexample.JavaCubeViewActivity.onCreate(JavaCubeViewActivity.java:31)
     E/AndroidRuntime(604):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     E/AndroidRuntime(604):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
     E/AndroidRuntime(604):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
     E/AndroidRuntime(604):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
     E/AndroidRuntime(604):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
     E/AndroidRuntime(604):     at android.os.Handler.dispatchMessage(Handler.java:99)
     E/AndroidRuntime(604):     at android.os.Looper.loop(Looper.java:123)
     E/AndroidRuntime(604):     at android.app.ActivityThread.main(ActivityThread.java:3683)
     E/AndroidRuntime(604):     at java.lang.reflect.Method.invokeNative(Native Method)
     E/AndroidRuntime(604):     at java.lang.reflect.Method.invoke(Method.java:507)
     E/AndroidRuntime(604):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)   
     E/AndroidRuntime(604):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
     E/AndroidRuntime(604):     at dalvik.system.NativeStart.main(Native Method)

    my code:
    PHP:
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.FrameLayout;

    import com.unity3d.player.*;


     

     

    public class 
    JavaCubeViewActivity extends Activity {

        private 
    UnityPlayer m_UnityPlayer;

            

        
    /** Called when the activity is first created. */

        
    @Override
        
    public void onCreate(Bundle savedInstanceState) {

            
    super.onCreate(savedInstanceState);

            
    // Create the UnityPlayer
            
    m_UnityPlayer = new UnityPlayer(this);
            
    int glesMode m_UnityPlayer.getSettings().getInt("gles_mode"1);
            
    boolean trueColor8888 false;
            
    m_UnityPlayer.init(glesModetrueColor8888);

            
    setContentView(R.layout.main);

            
    // Add the Unity view
            
    FrameLayout layout = (FrameLayoutfindViewById(R.id.frameLayout2);     
            
    LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENTLayoutParams.FILL_PARENT);
            
    layout.addView(m_UnityPlayer.getView(), 0lp);

        }

    }
    The main.xml:
    PHP:
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frameLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <View
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    </FrameLayout>

    The error is when try this I think m_UnityPlayer = new UnityPlayer(this);
     
    Last edited: Jun 27, 2012
  18. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
  19. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    Hey guys it has been removed from the docs as it was merely a hacky way of doing this and not really an "official" solution. We are working on something that should automate the process for a future release. I am at this very moment working on an updated guide that I will post in the forums. All being well and I don't get too bogged down today with other things it should be updated later today.
     
  20. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
  21. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
    Same crash results, so does not seem to work still..

    For safety, I have also tried with an empty unity project with Android 2.2 target

    07-01 04:03:25.282: E/AndroidRuntime(7609): Caused by: java.lang.ClassNotFoundException: com.unity3d.player.UnityPlayerProxyActivity in loader dalvik.system.PathClassLoader

    It seems some classes have been renamed in the new Unity 3.5.x runtime?
     
  22. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    That looks like the error I got before I moved the androidmanifest from unitylib to the unityjava project.
    Do you have a custom manifest?
     
  23. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Hello Everybody! finally it works for me, but I did any changes respect this tutorial. I didnt add "Unitylib" as a lib, I only copy the AndroidManifest.xml and assets and libs folders from "UnityLib" to "UnityJava". Only I have a question, if I build it for 4.0.3 it works fine but for 2.3.3 it doesn't work :S
    I hope that it helps somebody.
     
  24. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Hello again, I have 2 questions, I try to resize the view in that way:

    PHP:
            LayoutParams lp = new LayoutParams(200300);
     
            
    View unityView = (Viewm_UnityPlayer.getView();
            
    unityView.setLayoutParams(layout.getLayoutParams());
            
    layout.addView(unityView0lp);
    and in main.xml:
    PHP:
            <FrameLayout
                android
    :id="@+id/frameLayout2"
                
    android:layout_width="wrap_content"
                
    android:layout_height="wrap_content" >

            </
    FrameLayout>
    But ever is set in full screen.

    The second is taht if I want to communicate with the "unity view" how can I do it? I read somewhere that I can send a "message" with m_UnityPlayer.UnitySendMessage(arg0, arg1, arg2) , but I dont have it clear at all how to process it . If know a tutorial or something reply me, thanks for all.
     
  25. inac

    inac

    Joined:
    Aug 9, 2010
    Posts:
    2
    I couldn't find the default manifest, so I just copy/pasted a manifest from another Unity project that built (though not in Eclipse).. What would a default Unity Android manifest look like? Can you include your manifest?
     
  26. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    This is the standard manisfest from the project that I built:
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.unity3d.unity" android:versionName="1.0" android:versionCode="1">
    4.   <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
    5.   <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    6.     <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    7.       <intent-filter>
    8.         <action android:name="android.intent.action.MAIN" />
    9.         <category android:name="android.intent.category.LAUNCHER" />
    10.       </intent-filter>
    11.     </activity>
    12.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    13.     </activity>
    14.     <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    15.       <meta-data android:name="android.app.lib_name" android:value="unity" />
    16.       <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    17.     </activity>
    18.     <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    19.     </activity>
    20.   </application>
    21.   <uses-feature android:glEsVersion="0x00020000" />
    22.   <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" />
    23. </manifest>
    24.  
     
  27. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176
    I have not tested it with 2.3.3 yet I will try that as soon as possible, though I cannot think of any reason why this would not work. I will update the thread as soon as I get a chance.
     
  28. markharkness

    markharkness

    Unity Technologies

    Joined:
    Nov 2, 2010
    Posts:
    176

    This thread might help you with your issue. http://forum.unity3d.com/threads/78598-Android-Plugin-Call-C-from-Java
     
    Last edited: Jul 4, 2012
  29. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Thanks Rofl!
     
  30. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
    It seems that for Android 2.3.3, a different manifest is required? copy/paste of this one is generating errors, presumably due to android:configChanges syntax differences.

    Speaking of which, is there a source to look up different default Unity Android manifests for different Android API versions?

     
  31. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    Two things. Do you know if this process works with the new Android SDK (revision 20 i think) because when I import the library the manifest just gives me errors.

    Question #2 can we use android resources while importing to unity? EG: I have a layout file layout.xml and i wish to use that as an activity main content. I get an exception that it cannot find that resource. Do you know what might be up?? This question is based on a plugin tutorial used by plato evolved
    http://www.platoevolved.com/blog/programming/android/creating-an-android-plugin-for-unity/
     
    Last edited: Jul 6, 2012
  32. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Hello, I think that some of configChanges are not supported, try to delete screenSize|smallestScreenSize i think that it was the problem. like this:
    PHP:
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|uiMode|touchscreen"
    Hope it will help you.
     
  33. pfialho

    pfialho

    Joined:
    Oct 24, 2011
    Posts:
    13
    Hi all,

    Thanks for the tutorial.

    I have all the code you post, but at the end I added:

    Code (csharp):
    1.  
    2. final EditText textw = (EditText) this.findViewById(R.id.editText1);
    3.  
    4. final RelativeLayout textsend = (RelativeLayout) this.findViewById(R.id.imageButton2);
    5. textsend.setOnTouchListener(new OnTouchListener() {
    6.     @Override
    7.     public boolean onTouch(View arg0, MotionEvent event) {
    8.         if (event.getAction() == MotionEvent.ACTION_DOWN) {
    9.             String written = textw.getText().toString();
    10.             Log.i("txt", "written: " + written);
    11.             if (written.length() > 0) {
    12.                 UnityPlayer.UnitySendMessage("/mydoll", "mydollsfunc", written);
    13.             }
    14.             textw.setText("");
    15.         }
    16.         return true;
    17.     }
    18. });
    19.  
    but that Log.i never shows. Any hint? (I also tried with onClick).

    Best,
     
    Last edited: Jul 10, 2012
  34. txasti

    txasti

    Joined:
    Oct 26, 2010
    Posts:
    10
    Thak you! Finally it worked! :D
     
  35. tato469

    tato469

    Joined:
    Jun 22, 2012
    Posts:
    16
    Hello I had some troubles for set the unityPlayer as a subView and finally I have found the solution, my problem was that when I copy the AndroidManifest I didn't change it, if u want that the UnityPlayer run as a subView in your app you have to change the android:name="com.unity3d.player.UnityPlayerProxyActivity" in the manifest by your "mainActivity" name, in my case: android:name=".MainActivity" finally it will seems like that:


    Code (csharp):
    1.   <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    2.       <intent-filter>
    3.         <action android:name="android.intent.action.MAIN" />
    4.         <category android:name="android.intent.category.LAUNCHER" />
    5.       </intent-filter>
    6.     </activity>
    Hope it helps somebody :)
     
    Last edited: Jul 18, 2012
  36. mvokal

    mvokal

    Joined:
    Aug 7, 2012
    Posts:
    1
    Is there a way to do this so that Unity doesn't launch as soon as the app does? I'm trying to make an app that does some stuff in Java before launching Unity, but whatever I do it just launches my game. I even commented out all the custom code you put in the second part of your tutorial, but it didn't do any good. :/
     
  37. tryfrie

    tryfrie

    Joined:
    Apr 3, 2012
    Posts:
    2
    THX i couldn't figure out why it stopped working :)
     
  38. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
    Hi Rolf, when should we expect the automated process be released?
     
  39. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
    the class is certainly there... and i replaced the UnityPlayerProxyActivity with UnityJavaProjActivity... receiving these errors:

    09-11 03:30:24.805: E/AndroidRuntime(19071): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.testing123/com.app.testing123.UnityJavaProjActivity}: java.lang.ClassNotFoundException: com.app.testing123.UnityJavaProjActivity

    09-11 03:30:24.805: E/AndroidRuntime(19071): Caused by: java.lang.ClassNotFoundException: com.app.testing123.UnityJavaProjActivity


     
  40. fanave

    fanave

    Joined:
    May 17, 2012
    Posts:
    3
    Hi all,

    I have followed the steps explained by Rolf (thanks Rolf!!) and I am able to see a simple Unity project into the subview (shows a static tree). Now I have an Unity project more complex and I want to integrate it with Android the same way but I can't because I have the following error after the Unity Splash Screen:

    Activity xxx has leaked IntentReceiver com.unity3d.player.UnityPlayer$18@4057eb90 that was originally registered here. Are you missing a call to unregisterReceiver()?


    Can anyone help me?

    Thanks in advance.
     
  41. turkert

    turkert

    Joined:
    May 4, 2012
    Posts:
    18
    How I manage to integrate Unity to Eclipse ?

    I use Unity 3.5.5f3 and Android 2.3.3. No zipaling required.

    I've followed RoflHarris's tutorial and first problem was "The container 'Library Projects' references non existing library 'D:\Projects\Android\UnityLibrary\bin\unitylibrary.jar'". When I try to Build UnityLibrary I got "AndroidManifest.xml file is missing" error. I've copied that file from StagingArea folder.

    Second problem was "android:configChanges" section some Android 4 specific features. So I removed some of them and it became like
    Code (csharp):
    1. android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout"
    Third problem was "Couldn't load mono: findLibrary returned null". I've copied StagingArea\libs folder to UnityApplication\libs folder.

    Fourth problem was ".UnityEclipse.UnityApplicationActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8". I'have modified AndroidManifest.xml file in UnityApplication so that activity section has
    Code (csharp):
    1. android:configChanges="orientation"
    feature.

    My complete AndroidManifest in UnityApplication
    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.OnayBilisim.UnityEclipse"
    4.     android:versionCode="1"
    5.     android:versionName="1.0" >
    6.  
    7.     <uses-sdk android:minSdkVersion="10" />
    8.  
    9.     <application
    10.         android:icon="@drawable/ic_launcher"
    11.         android:label="@string/app_name" >
    12.         <activity
    13.             android:name=".UnityApplicationActivity" android:configChanges="orientation"
    14.             android:label="@string/app_name" >
    15.             <intent-filter>
    16.                 <action android:name="android.intent.action.MAIN" />
    17.  
    18.                 <category android:name="android.intent.category.LAUNCHER" />
    19.             </intent-filter>
    20.         </activity>
    21.     </application>
    22.  
    23. </manifest>
    AndroidManifest.xml in UnityLibrary
    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.OnayBilisim.UnityEclipse" android:versionName="1.0" android:versionCode="1">
    3.   <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
    4.   <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    5.     <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout" android:screenOrientation="portrait">
    6.       <intent-filter>
    7.         <action android:name="android.intent.action.MAIN" />
    8.         <category android:name="android.intent.category.LAUNCHER" />
    9.       </intent-filter>
    10.     </activity>
    11.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout" android:screenOrientation="portrait">
    12.     </activity>
    13.     <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout" android:screenOrientation="portrait">
    14.       <meta-data android:name="android.app.lib_name" android:value="unity" />
    15.       <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    16.     </activity>
    17.     <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout" android:screenOrientation="portrait">
    18.     </activity>
    19.   </application>
    20.   <uses-feature android:glEsVersion="0x00020000" />
    21.   <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" />
    22. </manifest>
    I have just tried to implement a Cube in Eclipse. I don't know if it will work for QCAR, Vuforia, AdMob, Mobiclix or any other libraries and scripts.
     
    Last edited: Sep 15, 2012
  42. Navid

    Navid

    Joined:
    Sep 3, 2012
    Posts:
    39
    @turkert
    try
    this plugin.
    you can even use it to create lwp!
     
  43. turkert

    turkert

    Joined:
    May 4, 2012
    Posts:
    18
  44. turkert

    turkert

    Joined:
    May 4, 2012
    Posts:
    18
    I wonder if this plugin works well with other extensions such as Vuforia, Mobile Movie Texture, etc...
     
  45. marijn

    marijn

    Joined:
    Sep 25, 2012
    Posts:
    1
    Hi Rolf, thanks for the tutorial. I've successfully used it to integrate a Unity sample app into our existing Android app.

    We've been asked to integrate a Vuforia/Unity app (made by another company) into our Android app, but in the near future also need to include a second Unity app.
    Would there be any way to include multiple unity apps into an existing Android app this way?

    Thanks,
    Marijn
     
  46. BT2142

    BT2142

    Joined:
    Sep 28, 2012
    Posts:
    3
  47. blacksh33p

    blacksh33p

    Joined:
    Oct 4, 2012
    Posts:
    2
    It turns out the way that ADT 17 handles .jar files broke this method of integrating Unity into Eclipse, causing various errors. Instead of adding classes.jar to your build path, you should just copy the classes.jar file into the lib directory of your project, and everything should work properly.
     
  48. ftujftuftu

    ftujftuftu

    Joined:
    Sep 4, 2012
    Posts:
    1
    Hi, I have several LinearLayout one in other with button and Listviews. What do I do for get work it?

    Thank you.

    Sorry for my bad english.
     
  49. rawatenator

    rawatenator

    Joined:
    Dec 25, 2010
    Posts:
    5
    I recently had similar problems to Ina using this guide on Unity 3.5.6, figured them out and would like to suggest some updates.

    1st.) To deal with " ClassNotFoundException: com.unity3d.player.UnityPlayerProxyActivity ", instead of adding the external JAR "classes", it seems best to copy and paste it into the lib of UnityJava (source: see blacksh33ps comment at http://forum.unity3d.com/threads/71607-Integrating-Unity-and-Eclipse/page5 )

    2nd.) make sure to keep OPENGL settings at 1.x otherwise you might get FATAL EXCEPTION: GLThread 88, see link: http://forum.unity3d.com/threads/131640-quot-unfortunately-unity-project-has-stopped-quot
     
  50. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    I seemed to have first read this thread in the middle. And now my head is catching up to the tail.

    I've been working around all kinds of hacks. I understand the process well enough that can now manage to produce the final build directly from Unity by inserting content into the Plugins/Android folder.

    I tried extending the UnityPlayerActivity, UnityPlayerNativeActivity, and UnityPlayerProxyActivity java files, but when I use the derived classes I get bogus errors in DDMS on start.

    Code (csharp):
    1. 11-07 15:53:55.780: E/AndroidRuntime(18629): FATAL EXCEPTION: main
    2. 11-07 15:53:55.780: E/AndroidRuntime(18629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.example1/com.me.example1.OverrideExampleProxy}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.me.example1/com.unity3d.player.UnityPlayerNativeActivity}; have you declared this activity in your AndroidManifest.xml?
    It's trying to look for the Unity file inside of my package oddly. I'll insert their files into mine and see if that helps.
    C:\Program Files (x86)\Unity\Editor-3.5.6\Data\PlaybackEngines\androidplayer\src\com\unity3d\player