AutoConnectBLE.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.Reflection;
  6. public class AutoConnectBLE : MonoBehaviour
  7. {
  8. [SerializeField] string serverIP;
  9. GameObject obj_bluetoothHolder;
  10. void Start()
  11. {
  12. obj_bluetoothHolder = Instantiate(Resources.Load<GameObject>("Prefabs/BluetoothHolder"));
  13. SetServerIP();
  14. OpenBluetoothDebugView();
  15. //StartCoroutine(ConnectBLE());
  16. StartCoroutine(ConnectBleUDP());
  17. }
  18. void SetServerIP()
  19. {
  20. BluetoothClient bluetoothClient = obj_bluetoothHolder.GetComponentInChildren<BluetoothClient>();
  21. FieldInfo field_serverIP = bluetoothClient.GetType().GetField("serverIP", BindingFlags.Instance | BindingFlags.NonPublic);
  22. field_serverIP.SetValue(bluetoothClient, serverIP);
  23. }
  24. void OpenBluetoothDebugView()
  25. {
  26. BluetoothHolder bluetoothHolder = obj_bluetoothHolder.GetComponent<BluetoothHolder>();
  27. FieldInfo field_debug = bluetoothHolder.GetType().GetField("debug", BindingFlags.Instance | BindingFlags.NonPublic);
  28. field_debug.SetValue(bluetoothHolder, true);
  29. //改变canvas渲染模式,让3d物体渲染在前面
  30. Canvas[] canvasArray = bluetoothHolder.transform.GetComponentsInChildren<Canvas>();
  31. foreach (var canvas in canvasArray)
  32. {
  33. canvas.renderMode = RenderMode.ScreenSpaceCamera;
  34. canvas.worldCamera = Camera.main;
  35. }
  36. StartCoroutine(ShowDebugView(bluetoothHolder));
  37. }
  38. IEnumerator ShowDebugView(BluetoothHolder bluetoothHolder)
  39. {
  40. yield return new WaitForSeconds(0.1f);
  41. bluetoothHolder.openDebug();
  42. }
  43. IEnumerator ConnectBLE()
  44. {
  45. yield return null;
  46. #if PLATFORM_ANDROID
  47. BluetoothAim bluetoothAim = obj_bluetoothHolder.GetComponentInChildren<BluetoothAim>();
  48. MethodInfo method_DoConnect = bluetoothAim.GetType().GetMethod("DoConnect");
  49. method_DoConnect.Invoke(bluetoothAim, new object[]{});
  50. #endif
  51. }
  52. IEnumerator ConnectBleUDP()
  53. {
  54. yield return new WaitForSecondsRealtime(1);
  55. BluetoothAim.ins.DoConnect();
  56. }
  57. }