ButtonEventWatcher.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using UnityEngine;
  21. using UnityEngine.Events;
  22. namespace Oculus.Voice.Demo
  23. {
  24. public class ButtonEventWatcher : MonoBehaviour
  25. {
  26. #if ENABLE_LEGACY_INPUT_MANAGER
  27. // By default: uses space bar, oculus quest a button and oculus quest x button
  28. [SerializeField] private KeyCode[] _keys = new KeyCode[] { KeyCode.Space, KeyCode.JoystickButton0, KeyCode.JoystickButton2 };
  29. #endif
  30. // Used for click or hold events
  31. public UnityEvent OnButtonDown;
  32. // Used for button up hold events
  33. public UnityEvent OnButtonUp;
  34. #if ENABLE_LEGACY_INPUT_MANAGER
  35. // Update activation
  36. void Update()
  37. {
  38. // Iterate keys
  39. foreach (var key in _keys)
  40. {
  41. if (Input.GetKeyDown(key))
  42. {
  43. OnButtonDown?.Invoke();
  44. }
  45. else if (Input.GetKeyUp(key))
  46. {
  47. OnButtonUp?.Invoke();
  48. }
  49. }
  50. }
  51. #endif
  52. }
  53. }