Search Unity

Problem on touches after player resume on ICS.

Discussion in 'Android' started by lyh1, Jul 24, 2012.

  1. lyh1

    lyh1

    Joined:
    Jun 30, 2010
    Posts:
    92
    I am writing a app using unity3d as sub view. It will make the unity3d view invisible and put and another view on it for other propuse.
    The overlay view will keep sending touch event to the unity view. And unity got all touch before I switch app.
    If I keep the unity3d invisible and switch to other app(e.g.: home screen) and return to app, any touched in unity3d, the position will become infinity.
    I can repeat this on ICS only with unity 3.5.3f.
    I don't know if this is a bug of unity.
     
  2. lyh1

    lyh1

    Joined:
    Jun 30, 2010
    Posts:
    92
    I just have the minimal code of the Java part of my plugin which will reproduce my problem.
    Press home go to home screen and return to unity again, see position of touches in unity.:(
    Code (csharp):
    1.  
    2. package org.myCode.DummyPlugin;
    3.  
    4. import javax.microedition.khronos.egl.EGLConfig;
    5. import javax.microedition.khronos.opengles.GL10;
    6.  
    7. import com.unity3d.player.UnityPlayer;
    8.  
    9. import android.app.Activity;
    10. import android.content.Context;
    11. import android.content.res.Configuration;
    12. import android.opengl.GLSurfaceView;
    13. import android.opengl.GLSurfaceView.Renderer;
    14. import android.os.*;
    15.  
    16. import android.util.AttributeSet;
    17. import android.util.Log;
    18. import android.view.KeyEvent;
    19. import android.view.MotionEvent;
    20. import android.view.Surface;
    21. import android.view.SurfaceHolder;
    22. import android.view.View;
    23. import android.view.Window;
    24. import android.view.WindowManager;
    25. import android.view.ViewGroup.LayoutParams;
    26. import android.widget.TextView;
    27.  
    28. public class DummyPluginActivity extends Activity
    29. {  
    30.     protected UnityPlayer mUnityPlayer;
    31.     private mGLSurfaceView m3DView = null;
    32.    
    33.     @Override
    34.      protected void onResume()
    35.      {           
    36.         super.onResume();
    37.         mUnityPlayer.resume();
    38.      }
    39.    
    40.     @Override
    41.     protected void onPause()
    42.     {              
    43.       super.onPause();
    44.       mUnityPlayer.pause();
    45.     }
    46.    
    47.     public void onConfigurationChanged(Configuration newConfig)
    48.     {
    49.         super.onConfigurationChanged(newConfig);
    50.         mUnityPlayer.configurationChanged(newConfig);
    51.     }
    52.     public void onWindowFocusChanged(boolean hasFocus)
    53.     {
    54.         super.onWindowFocusChanged(hasFocus);
    55.         mUnityPlayer.windowFocusChanged(hasFocus);
    56.     }
    57.     // Pass any keys not handled by (unfocused) views straight to UnityPlayer
    58.     public boolean onKeyDown(int keyCode, KeyEvent event)
    59.     {
    60.         return mUnityPlayer.onKeyDown(keyCode, event);
    61.     }
    62.     public boolean onKeyUp(int keyCode, KeyEvent event)
    63.     {
    64.         return mUnityPlayer.onKeyUp(keyCode, event);
    65.     }
    66.     public boolean dispatchTouchEvent (MotionEvent ev)
    67.       {
    68.          Log.d("Debug", "dispatchTouchEvent:"+ev.getX()+" "+ev.getY());//        
    69.          return mUnityPlayer.dispatchTouchEvent(ev);//       
    70.       }
    71.    
    72.       protected void onCreate(Bundle savedInstanceState)
    73.       {
    74.             super.onCreate(savedInstanceState);
    75.             super.onCreate(savedInstanceState);
    76.             requestWindowFeature(Window.FEATURE_NO_TITLE);
    77.             setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
    78.            
    79.             mUnityPlayer = new UnityPlayer(this);
    80.             if (mUnityPlayer.getSettings().getBoolean ("hide_status_bar", true))
    81.                 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    82.                                      WindowManager.LayoutParams.FLAG_FULLSCREEN);
    83.  
    84.             int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
    85.             boolean trueColor8888 = false;
    86.             mUnityPlayer.init(glesMode, trueColor8888);
    87.  
    88.             View playerView = mUnityPlayer.getView();
    89.             setContentView(playerView);
    90.             playerView.requestFocus();
    91.            
    92.             mUnityPlayer.getView().setVisibility(View.GONE);
    93.             m3DView =  new mGLSurfaceView(this);       
    94.             addContentView(m3DView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    95.             m3DView.setVisibility(View.VISIBLE);
    96.             m3DView.requestFocus();
    97.       }
    98.    
    99.       public class mGLSurfaceView extends GLSurfaceView implements Renderer
    100.       {
    101.             private static final String TAG = "mGLSurfaceView";
    102.  
    103.             private SurfaceHolder holder;
    104.             public int mode;
    105.  
    106.             public mGLSurfaceView(Context context)
    107.             {
    108.                 super(context);
    109.                 init();
    110.             }
    111.  
    112.             public mGLSurfaceView(Context context, AttributeSet attrs)
    113.             {
    114.                 super(context, attrs);
    115.                 init();
    116.             }
    117.  
    118.             private void init()
    119.             {
    120.                 holder = getHolder();
    121.                 holder.addCallback(this);
    122.                 this.setEGLContextClientVersion(2);
    123.                 this.setRenderer(this);
    124.                 this.requestFocus();
    125.                 this.setFocusableInTouchMode(true);
    126.             }
    127.  
    128.             public void onSurfaceCreated(GL10 gl, EGLConfig config)
    129.             {
    130.                 mUnityPlayer.onSurfaceCreated(gl, config);
    131.             }
    132.  
    133.             @Override
    134.             public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
    135.             {
    136.                 super.surfaceChanged(holder, format, w, h);
    137.             }
    138.  
    139.             public void draw(GL10 gl)
    140.             {
    141.             }
    142.  
    143.             public void onSurfaceChanged(GL10 gl, int w, int h)
    144.             {
    145.                 mUnityPlayer.onSurfaceChanged(gl, w, h);
    146.                 Log.d(TAG, "changed w=" + w + " h=" + h);
    147.             }
    148.  
    149.             public void onDrawFrame(GL10 gl)
    150.             {
    151.                 mUnityPlayer.onDrawFrame(gl);
    152.             }
    153.            
    154.             @Override
    155.             public void onPause()
    156.             {
    157.             }
    158.          
    159.             @Override
    160.             public void onResume()
    161.             {                      
    162.             }
    163.         }
    164. }
    165.