Search Unity

Android Augmented Reality Question

Discussion in 'Android' started by g4, Mar 3, 2011.

  1. g4

    g4

    Joined:
    Aug 4, 2010
    Posts:
    3
    Hello,

    I'm trying to make an Unity Augmented Reality Application, and i have some trouble to get the camera background working properly with my Unity player.

    I manage to use my own android activity to run the camera, and the unity player with the java UnityPlayer class, but that all. I have got the unity player running on top of the camera but it's not a transparent layout, so i can't see my camera in background.

    Changing the clear flags has no effect.

    Is there a way to get the unity player to render in an android activity has an overlay or something like that.

    here is my android activity.


    Code (csharp):
    1.  
    2.  
    3. import com.unity3d.player.UnityPlayer;
    4.  
    5. public class JavaUnityPlayer extends Activity
    6. {
    7.  
    8.     private Activity mActivity;
    9.    
    10.     private UnityPlayer mUnityPlayer;
    11.            
    12.     private Preview mPreview;
    13.     private Camera mCamera;
    14.        
    15.         public static Activity currentActivity;
    16.    
    17.         protected void onCreate(Bundle savedInstanceState) {
    18.  
    19.         super.onCreate(savedInstanceState);
    20.        
    21.         getWindow().setFormat(PixelFormat.TRANSLUCENT);
    22.  
    23.      
    24.         mActivity = this;
    25.         currentActivity = this;
    26.        
    27.         mUnityPlayer = new UnityPlayer(this);
    28.        
    29.        
    30.         mUnityPlayer.init(0, false);       
    31.        
    32.         //CAMERA INIT
    33.        
    34.         mPreview = new Preview(mActivity);
    35.         mPreview.setBackgroundColor(0x00000000);
    36.      
    37.        
    38.         LayoutParams mylayout2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    39.         LayoutParams mylayout = new LayoutParams(500, 500);
    40.        
    41.  
    42.        
    43.         setContentView(mPreview, mylayout2);
    44.         addContentView(mUnityPlayer,mylayout );
    45.        
    46.  
    47. ...
    48.  
    49.  
    50.  
    51.  

    For the camera i'm using a custom class.

    If somebody already got this problem, and got a solution, it will be really nice to have some help.

    Thank you all.
     
  2. chenyangxia

    chenyangxia

    Joined:
    Mar 31, 2011
    Posts:
    1
    Hi:

    I worked on solving the problem for a while and the way I got it to work was using a GLSurfaceView, and a custom class that extends UnityPlayer class and also implements GLSurfaceView.Renderer. It looks something like this:

    Code (csharp):
    1. class MyUnityPlayer extends UnityPlayer implements GLSurfaceView.Renderer {
    2.         public MyUnityPlayer(Context context) {
    3.             super(context);
    4.         }
    5.  
    6.                 // UnityPlayer and GLSurfaceView.Renderer both have onDrawFrame(),
    7.                 //which is what makes what we want to do possible ^_^
    8.         public void onDrawFrame(GL10 gl) {
    9.             super.onDrawFrame(gl);   
    10.             //adjust this if you want the "clear" color to be something other than completely transparent
    11.             //gl.glClearColor(0,0,0,0);
    12.         }
    13. }
    Then in your activity's onCreate() function, initialize your GLSurfaceView similar to this:

    Code (csharp):
    1.  
    2. // mUnityPlayer is a member of your Activity class. It's the class memtioned earlier
    3. mUnityPlayer = new MyUnityPlayer(this);
    4. mUnityPlayer.init(0, false);
    5.  
    6. // Assuming mTransView is a member of your Activity class, type is GLSurfaceView
    7. mTransView = new GLSurfaceView(getApplication());
    8. mTransView.setZOrderMediaOverlay(true);
    9. mTransView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    10.  
    11. // It needs a renderer. Assign it to be our mUnityPlayer member
    12. // Also the order in which setEGLConfigChooser() and setRenderer() is important.
    13. // Android will give you error if it's switched
    14. mTransView.setRenderer(mUnityPlayer);
    15. mTransView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    After these, you can add mTransView to your layout and it should show up with any transparent (alpha = 0) portions transparent. It should be the first thing added to the layout to make sure it shows on top. I'm not actually very familiar with android's layout but it's what's been working for me.

    Hopefully it helps ^_^
     
  3. flanker

    flanker

    Joined:
    Jun 29, 2011
    Posts:
    9
    Hello,
    I unearth this topic because as you, I'm trying to mix an android camera in background of a Unity player for an AR application.
    I have followed the example of g4 but without success: a black layout seems to hide my camera and my Unity app (I can access app buttons through!)

    I have a myUnityPlayer class which extends UnityPlayer and implements GLSurfaceView.Renderer (for calling my camera) and SurfaceHolder.Callback (because a SurfaceHolder is required for defining the previewDisplay). Yet, I had some difficulties to catch the good SurfaceHolder for my camera.
    My main activity extends Activity and create a GLSurfaceView and define MyUnityPlayer as its renderer.

    Any idea to rescue me?
     
  4. g4

    g4

    Joined:
    Aug 4, 2010
    Posts:
    3
    Oh well, it's been a long time i've not been in this forum.
    i managed to solve my probleme. it's kind of a trick, but it's was working. As chenyangxia said, i used the UnityPlayer class as new rendering system for my glSurfaceView. Sadly by doing this, unity lost the ability to read input from the touch screen, so i add another function that stream every touch input back to unity. i call in my unity project a native java function that sending me the last touch input.

    Be sure to use setEGLConfigChooser(8, 8, 8, 8, 16, 0); it set your view in RGBA 32bits, i tried in RGB16 but it didn't work. make also sure that your unity camera as a black transparent background.

    but it's really an odd trick, i don't know if it will still works in the future.