Any unity3D experts here?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Pleng, Aug 26, 2014.

  1. Pleng
    OP

    Pleng GBAtemp Advanced Maniac

    Member
    1,648
    787
    Sep 14, 2011
    Thailand
    Well I've tried both unityanswers and stackoverflow. I can't find anything in the official documentation so I guess I'll try the good old folk at gbaTemp... you never know!

    Does anybody know I consume key/touch events in Android, without passing them on to the player. I have exported my game to a "Google Android Project" and I have tried both overriding the dispatchKeyEvent() and onGenericMotionEvent, like this:

    Code:
           // For some reason the multiple keyevent type is not supported by the ndk.
            // Force event injection by overriding dispatchKeyEvent().
            @Override public boolean dispatchKeyEvent(KeyEvent event)
            {
                Log.d(String.valueOf(event.getDeviceId()), "Control");
                Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
            
                if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
                    return mUnityPlayer.injectEvent(event);
                return super.dispatchKeyEvent(event);
            }
        
            @Override
            public boolean onGenericMotionEvent(MotionEvent event) 
            { 
                Log.d(String.valueOf(event.getDeviceId()), "Control");
                Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
                return true;
            }
    
    I have also tried to attach KeyListener handlers directly to the UnityPlayer object, like this:

    Code:
               mUnityPlayer = new UnityPlayer(this);
                mUnityPlayer.setOnKeyListener(new View.OnKeyListener()
                {
                    
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event)
                    {
                        Log.d(String.valueOf(event.getDeviceId()), "Control");
                        Toast.makeText(UnityPlayerNativeActivity.this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                }
                );
    
    But in both cases, the Java code is not triggering. Any ideas why??