NRDefaultPhoneScreenProvider.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. public class NRDefaultPhoneScreenProvider : NRPhoneScreenProviderBase
  14. {
  15. SystemButtonState m_buttonState = new SystemButtonState();
  16. private static AndroidJavaObject m_VirtualDisplayFragment;
  17. public class AndroidSystemButtonDataProxy : AndroidJavaProxy, ISystemButtonDataProxy
  18. {
  19. private NRPhoneScreenProviderBase m_Provider;
  20. public AndroidSystemButtonDataProxy(NRPhoneScreenProviderBase provider) : base("ai.nreal.virtualcontroller.ISystemButtonDataReceiver")
  21. {
  22. this.m_Provider = provider;
  23. }
  24. public void OnUpdate(AndroidJavaObject data)
  25. {
  26. SystemButtonState state = new SystemButtonState();
  27. #if UNITY_2019_1_OR_NEWER
  28. sbyte[] sbuffer = data.Call<sbyte[]>("getRawData");
  29. byte[] bytes = new byte[sbuffer.Length];
  30. Buffer.BlockCopy(sbuffer, 0, bytes, 0, bytes.Length);
  31. #else
  32. byte[] bytes = data.Call<byte[]>("getRawData");
  33. #endif
  34. state.DeSerialize(bytes);
  35. m_Provider.OnSystemButtonDataChanged(state);
  36. }
  37. }
  38. public override void OnPreUpdate()
  39. {
  40. base.OnPreUpdate();
  41. m_buttonState.Reset();
  42. var data = m_VirtualDisplayFragment.Call<AndroidJavaObject>("GetSystemButtonState");
  43. if (data != null)
  44. {
  45. bool btnApp = data.Call<bool>("GetButtonApp");
  46. bool btnTouch = data.Call<bool>("GetButtonTouch");
  47. bool btnHome = data.Call<bool>("GetButtonHome");
  48. float touchX = data.Call<float>("GetTouchX");
  49. float touchY = data.Call<float>("GetTouchY");
  50. m_buttonState.Set(btnApp, btnTouch, btnHome, touchX, touchY);
  51. OnSystemButtonDataChanged(m_buttonState);
  52. }
  53. }
  54. public override void RegistFragment(AndroidJavaObject unityActivity, ISystemButtonDataProxy proxy)
  55. {
  56. NRDebugger.Info("[VirtualController] RegistFragment...");
  57. var VirtualDisplayFragment = new AndroidJavaClass("ai.nreal.virtualcontroller.VirtualControllerFragment");
  58. m_VirtualDisplayFragment = VirtualDisplayFragment.CallStatic<AndroidJavaObject>("RegistFragment", unityActivity, proxy);
  59. }
  60. public static void RegistDebugInfoProxy(AndroidJavaProxy proxy)
  61. {
  62. if (m_VirtualDisplayFragment != null)
  63. {
  64. m_VirtualDisplayFragment.Call("setDebugInfoProvider", proxy);
  65. }
  66. }
  67. public override ISystemButtonDataProxy CreateAndroidDataProxy()
  68. {
  69. // return new AndroidSystemButtonDataProxy(this);
  70. return null;
  71. }
  72. }
  73. }