0

i write an android library and it's extension is .jar. I want to use that in Unity.I used onKeyDown() method but when i called as an AndroidJavaObject, script isnt working. How can i call onKeyDown method in Unity?

Edit: Unity Codes c#:

public AndroidJavaClass jc;
jc = new AndroidJavaClass("com.unitylib.unity.unitylib.StartActivity");
jc.Call ("onKeyDown");

Android Codes:

public int x = 0 ;
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == event.KEYCODE_VOLUME_UP) {

        x = 2 ;

    }
return  true;
}

And also i tried to start with UnityActivity but it didnt work.

exploit4
  • 11
  • 4
  • You will likely not get answer if you don't provide a code – Programmer Oct 13 '16 at 10:48
  • I added codes. Thanks for answer. – exploit4 Oct 13 '16 at 11:03
  • It's so confuse on what you are trying to achieve here. What exactly is this plugin suppose to do? `onKeyDown` function is a callback function that is called automatically...Why are you calling `onKeyDown` function directly? – Programmer Oct 13 '16 at 11:18
  • I am trying to detect volume up,volume down and headsethook buttons in unity and i tried that :/ . How can i do that? – exploit4 Oct 13 '16 at 11:24

1 Answers1

2

You can't call AndroidJavaClass.Call without an object reference because it is public but not static.

So either get the object reference (the instance of your class) as a AndroidJavaObject and then call from that.

or a little easier, if possible make onKeyDown static, and use AndroidJavaClass.CallStatic

turnipinrut
  • 614
  • 4
  • 12
  • I cant make static onKeyDown it is giving error and @Programmer said onKeyDown function is a callback function in other comments. How can i detect volume up , volume down and headsethook button in unity? – exploit4 Oct 13 '16 at 11:35
  • In simple terms, like this: Make a plugin that listens for the event and then sends a message to unity about it. Check this out: http://stackoverflow.com/a/27166551/5756888 – turnipinrut Oct 13 '16 at 11:46
  • Thanks for answer. I will try that. – exploit4 Oct 13 '16 at 11:50
  • It works :D. I forgot to copy AndroidManifest.xml file to assets :/ Thanks for help. – exploit4 Oct 13 '16 at 13:24