Forum Discussion
mrgreen72
11 years agoSuperstar
Button up/down support added to OVRGamepadController
I should have [hopefully] more interesting stuff to share/discuss soon but while working on it I just implemented button down/up support on the OVRGamepadController class (like the Unity Input class provides) and I thought it might be useful to someone else even though it's not exactly rocket science.
The current GPC_GetButton of the class returns true if the button is held down but sometimes (most of the time actually) you just want to do something when it was just pressed down, or released.
Two new public methods GPC_GetButtonDown and GPC_GetButtonUp.
I just added code to the original class. I know, I know... :oops:
Here's the whole damn thing:
If you think it's crap feel free to tell me to stick to Java... :lol:
The current GPC_GetButton of the class returns true if the button is held down but sometimes (most of the time actually) you just want to do something when it was just pressed down, or released.
Two new public methods GPC_GetButtonDown and GPC_GetButtonUp.
I just added code to the original class. I know, I know... :oops:
Here's the whole damn thing:
/************************************************************************************
Filename : OVRGamepadController.cs
Content : Interface to gamepad controller
Created : January 8, 2013
Authors : Peter Giokaris
Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR SDK License Version 2.0 (the "License");
you may not use the Oculus VR SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
using UnityEngine;
using System;
using System.Runtime.InteropServices;
//-------------------------------------------------------------------------------------
// ***** OVRGamepadController
//
// OVRGamepadController is an interface class to a gamepad controller.
//
// On Win machines, the gamepad must be XInput-compliant.
//
public class OVRGamepadController : MonoBehaviour
{
//-------------------------
// Import from plugin
[DllImport ("OculusPlugin")]
private static extern bool OVR_GamepadController_Initialize();
[DllImport ("OculusPlugin")]
private static extern bool OVR_GamepadController_Destroy();
[DllImport ("OculusPlugin")]
private static extern bool OVR_GamepadController_Update();
[DllImport ("OculusPlugin")]
private static extern float OVR_GamepadController_GetAxis(int axis);
[DllImport ("OculusPlugin")]
private static extern bool OVR_GamepadController_GetButton(int button);
//-------------------------
// Input enums
public enum Axis { LeftXAxis, LeftYAxis, RightXAxis, RightYAxis, LeftTrigger, RightTrigger };
public enum Button { A, B, X, Y, Up, Down, Left, Right, Start, Back, LStick, RStick, L1, R1 };
// Hugo
private static int[] ButtonStatus = new int[Enum.GetNames(typeof(Button)).Length];
public enum Status {None, Pressed, Held, Released};
private static bool GPC_Available = false;
//-------------------------
// Public access to plugin functions
// GPC_Initialize
public static bool GPC_Initialize()
{
return OVR_GamepadController_Initialize();
}
// GPC_Destroy
public static bool GPC_Destroy()
{
return OVR_GamepadController_Destroy();
}
// GPC_Update
public static bool GPC_Update()
{
return OVR_GamepadController_Update();
}
// GPC_GetAxis
public static float GPC_GetAxis(int axis)
{
return OVR_GamepadController_GetAxis(axis);
}
// GPC_GetButton
public static bool GPC_GetButton(int button)
{
return OVR_GamepadController_GetButton(button);
}
// Hugo - Button down
public static bool GPC_GetButtonDown(int button)
{
return ButtonStatus[button] == (int)Status.Pressed;
}
// Hugo - Button down
public static bool GPC_GetButtonUp(int button)
{
return ButtonStatus[button] == (int)Status.Released;
}
// GPC_Ready
public static bool GPC_IsAvailable()
{
return GPC_Available;
}
// GPC_Test
void GPC_Test()
{
// Axis test
Debug.Log(string.Format("LT:{0:F3} RT:{1:F3} LX:{2:F3} LY:{3:F3} RX:{4:F3} RY:{5:F3}",
GPC_GetAxis((int)Axis.LeftTrigger), GPC_GetAxis((int)Axis.RightTrigger),
GPC_GetAxis((int)Axis.LeftXAxis), GPC_GetAxis((int)Axis.LeftYAxis),
GPC_GetAxis((int)Axis.RightXAxis), GPC_GetAxis((int)Axis.RightYAxis)));
// Button test
Debug.Log(string.Format("A:{0} B:{1} X:{2} Y:{3} U:{4} D:{5} L:{6} R:{7} SRT:{8} BK:{9} LS:{10} RS:{11} L1{12} R1{13}",
GPC_GetButton((int)Button.A), GPC_GetButton((int)Button.B),
GPC_GetButton((int)Button.X), GPC_GetButton((int)Button.Y),
GPC_GetButton((int)Button.Up), GPC_GetButton((int)Button.Down),
GPC_GetButton((int)Button.Left), GPC_GetButton((int)Button.Right),
GPC_GetButton((int)Button.Start), GPC_GetButton((int)Button.Back),
GPC_GetButton((int)Button.LStick), GPC_GetButton((int)Button.RStick),
GPC_GetButton((int)Button.L1), GPC_GetButton((int)Button.R1)));
}
void CheckButtonStatus() {
// Hugo
for (int i = 0; i < Enum.GetNames(typeof(Button)).Length; i++) {
if (OVR_GamepadController_GetButton(i)) {
switch (ButtonStatus[i]) {
case (int)Status.None:
case (int)Status.Released:
ButtonStatus[i] = (int)Status.Pressed;
break;
case (int)Status.Pressed:
ButtonStatus[i] = (int)Status.Held;
break;
}
} else {
switch (ButtonStatus[i]) {
case (int)Status.Pressed:
case (int)Status.Held:
ButtonStatus[i] = (int)Status.Released;
break;
case (int)Status.Released:
ButtonStatus[i] = (int)Status.None;
break;
}
}
}
}
// * * * * * * * * * * * * *
// Awake
void Awake ()
{
for (int i = 0; i < Enum.GetNames(typeof(Button)).Length; i++) {
ButtonStatus[i] = (int)Status.None;
}
}
// Start
void Start()
{
GPC_Available = GPC_Initialize();
}
// Update
void Update()
{
GPC_Available = GPC_Update();
// GPC_Test();
// Hugo
CheckButtonStatus();
}
// OnDestroy
void OnDestroy()
{
GPC_Destroy();
GPC_Available = false;
}
}
If you think it's crap feel free to tell me to stick to Java... :lol:
12 Replies
Replies have been turned off for this discussion
- tipatatExplorerThanks so much for this! I'm surprised this wasn''t already part of the code.
- mrgreen72Superstar6 months later someone finally gives a shit! :lol:
Glad I could help! :) - BaseDeltaZeroHonored Guest
"mrgreen72" wrote:
6 months later someone finally gives a shit! :lol:
Glad I could help! :)
Some of us are just too busy to check in here every day, when i do, i click on active posts and read a couple of pages worth, so maybe yours slipped under the radar.
If i had seen it i would have said....
Fantastic work! This will make things easier as i am busy coding other things :)
I tried your other controller, where you are in tank mode, and i did enjoy that, just wasnt ready to fully integrate it.
Now that the new SDK is out, would you mind checking this again to make sure it works? (no DK2 yet on my end)
Thanks for all your efforts - mrgreen72Superstar
"basedeltazero" wrote:
"mrgreen72" wrote:
6 months later someone finally gives a shit! :lol:
Glad I could help! :)
Some of us are just too busy to check in here every day, when i do, i click on active posts and read a couple of pages worth, so maybe yours slipped under the radar.
If i had seen it i would have said....
Fantastic work! This will make things easier as i am busy coding other things :)
I tried your other controller, where you are in tank mode, and i did enjoy that, just wasnt ready to fully integrate it.
Now that the new SDK is out, would you mind checking this again to make sure it works? (no DK2 yet on my end)
Thanks for all your efforts
Oh don't get me wrong, I'm not looking for credit or praise for those little pieces of code, I was just surprised to see this thread pop up 6 months later. :D
But thanks for the kind words anyway mate!
Sadly, my Unity pro license has expired and I've moved to UE4.
I noticed Oculus has implemented the "tank" mode (press E) into the C++ version of Tuscany though, so I guess it should/will be in the Unity package as well. - petereptProtegeThank you. A clever solution for tracking press/held/release.
They should add that to the SDK!
Peter - SAOTAHonored GuestWould someone mind getting this working with the latest Integration package.
I tried replacing the script, but it seems to have changed since the last version.
If not I'll do it and post here. - cyberealityGrand ChampionI believe this has been fixed already. Have you updated to the latest SDK?
- SAOTAHonored GuestI have, unity 5.0.1 package. There is no way standard way to call ButtonDown or ButtonUp.
- cyberealityGrand ChampionI added the following code to the bottom of Update() in OVRPlayerController.cs.
if(OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.Up)){
Debug.Log("Pressed UP!");
}
if(OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.Down)){
Debug.Log("Pressed DOWN!");
}
Is that what you guys are looking for? - cyberealityGrand ChampionAh, sorry. I think I understand what you guys want. You're asking for an event that fires when a particular button (like the X button) is first pressed or released. That makes more sense.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 6 months ago
- 7 months ago