VirtualControllerSample.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Rokid.UXR;
  6. using Rokid.UXR.Native;
  7. using UnityEngine.SceneManagement;
  8. using Rokid.UXR.Module;
  9. //using Google.XR.Cardboard;
  10. namespace Rokid.UXR.Demo
  11. {
  12. public class VirtualControllerSample : MonoBehaviour
  13. {
  14. public GameObject cube;
  15. public Text startInfo;
  16. public Text leftRockerInfo;
  17. public Text rightRockerInfo;
  18. private float rotateSpeed = 1f;
  19. private int moveSpeed = 2;
  20. private int colorIndex = 0;
  21. private List<Color> colors = new List<Color>() { Color.red, Color.blue, Color.yellow, Color.green, Color.gray };
  22. void Start()
  23. {
  24. // Configures the app to not shut down the screen
  25. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  26. RKVirtualController.Instance.Change(ControllerType.GAMEPAD);
  27. //Use RKInput instead of UnityEngine.Input for ABXY KeyEvent and Axis event, true as default.
  28. RKVirtualController.Instance.UseCustomGamePadEvent(true);
  29. }
  30. void Update()
  31. {
  32. Api.UpdateScreenParams();
  33. if (RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_OK))
  34. {
  35. cube.transform.localPosition = new Vector3(0, 1, 0);
  36. }
  37. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_X))
  38. {
  39. cube.transform.Rotate(new Vector3(0, rotateSpeed, 0));
  40. }
  41. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_B))
  42. {
  43. cube.transform.Rotate(new Vector3(0, -rotateSpeed, 0));
  44. }
  45. if (RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_Y))
  46. {
  47. colorIndex++;
  48. cube.GetComponent<MeshRenderer>().material.color = colors[Mathf.Abs(colorIndex) % colors.Count];
  49. }
  50. if (RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_A))
  51. {
  52. colorIndex--;
  53. cube.GetComponent<MeshRenderer>().material.color = colors[Mathf.Abs(colorIndex) % colors.Count];
  54. }
  55. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_LEFT) || Input.GetKeyDown(KeyCode.LeftArrow))
  56. {
  57. cube.transform.Translate(Vector3.left * moveSpeed * Time.deltaTime, Space.World);
  58. }
  59. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_RIGHT) || Input.GetKeyDown(KeyCode.RightArrow))
  60. {
  61. cube.transform.Translate(Vector3.right * moveSpeed * Time.deltaTime, Space.World);
  62. }
  63. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_UP) || Input.GetKeyDown(KeyCode.UpArrow))
  64. {
  65. cube.transform.Translate(Vector3.up * moveSpeed * Time.deltaTime, Space.World);
  66. }
  67. if (RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_DOWN) || Input.GetKeyDown(KeyCode.DownArrow))
  68. {
  69. cube.transform.Translate(Vector3.down * moveSpeed * Time.deltaTime, Space.World);
  70. }
  71. //RKLog.Info("UXR-PLUGIN::lr axis = " + RKInput.GetAxis(AxisEvent.Horizontal_Left) + ", " + RKInput.GetAxis(AxisEvent.Vertical_Left));
  72. leftRockerInfo.text = "Left Horizontal Axis: " + RKNativeInput.Instance.GetAxis(AxisEvent.Horizontal_Left) + ", Vertical Axis: " + RKNativeInput.Instance.GetAxis(AxisEvent.Vertical_Left);
  73. rightRockerInfo.text = "Right Horizontal Axis: " + RKNativeInput.Instance.GetAxis(AxisEvent.Horizontal_Right) + ", Vertical Axis: " + RKNativeInput.Instance.GetAxis(AxisEvent.Vertical_Right);
  74. }
  75. }
  76. }