问题
I am trying to access glass swipe pad in my unity3d project but it doesn't work. "KeyCode.Escape" works fine for swipe down. But how do I get swipe left right and tap working?
I checked the "secondaryTouchEnabled" using the following in OnGUI and it returns "False",
GUI.Label (new Rect (300, 100, 200, 60), ""+AndroidInput.secondaryTouchEnabled.ToString());
Shouldn't it return "True"?
It would be great if someone could share an example code here.
回答1:
The GUI.Label in your question should work the same as the two debug fields I've set up below. The code below inserts the data into the text field.
void Update () {
#if UNITY_ANDROID
if (Input.GetKey(KeyCode.Escape))
Application.Quit(); // HANDLE ESCAPE
// FINGERS
debug1.text = "TOUCHES: " + AndroidInput.touchCountSecondary.ToString(); // FINGER TOUCHES
// TOUCH COORDS
string resp = "COORDS: \n";
for (int i = AndroidInput.touchCountSecondary - 1; i > -1; i--) {
resp += "(id: " + i;
resp += " x: " + AndroidInput.GetSecondaryTouch(i).position.x;
resp += " y: " + AndroidInput.GetSecondaryTouch(i).position.y;
resp += ") \n";
}
debug2.text = resp;
#endif
}
If you want to do more advanced GDK methods you will need to register an AndroidJavaClass and extend UnityPlayerNativeActivity. Once you do that you can register a GestureDetector in Android and pass the results back to Unity using UnityPlayer.UnitySendMessage(); This also works with Voice Transcriptions and other GDK results.
回答2:
I have tryed to do that "If you want to do more advanced GDK methods you will need to register an AndroidJavaClass and extend UnityPlayerNativeActivity. Once you do that you can register a GestureDetector in Android and pass the results back to Unity using UnityPlayer.UnitySendMessage(); This also works with Voice Transcriptions and other GDK results.", using the code proposed by jainam here
But My UnityPlayerNativeActivity can't send the message to Unity, I don't Know why.
My quick solution has been the next (pending of optimize):
touches = int.Parse(AndroidInput.touchCountSecondary.ToString());
if (touches > 0) {
Debug.Log("TOUCHE");
xCurrent = AndroidInput.GetSecondaryTouch(0).position.x;
yCurrent = AndroidInput.GetSecondaryTouch(0).position.y;
if(xInitial == -1 && yInitial == -1){
xInitial = xCurrent;
yInitial = yCurrent;
}
}else if(xInitial > -1 && yInitial > -1){
if(yCurrent < yInitial && yCurrent == 0){
Debug.Log("Swap down");
Application.Quit();
}else if(xCurrent - xInitial < -100){
Debug.Log("Swap Right");
}else if(xCurrent - xInitial > 100){
Debug.Log("Swap Left");
}else{
Debug.Log("Tap");
}
xInitial = yInitial = xCurrent = yCurrent = -1;
}
What do you think about this solution?
does exist any solution to the UnityPlayerNativeActivity?
来源:https://stackoverflow.com/questions/21011924/accessing-google-glass-swipe-pad-in-unity3d