| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Reflection;
- public class AutoConnectBLE : MonoBehaviour
- {
- [SerializeField] string serverIP;
- GameObject obj_bluetoothHolder;
- void Start()
- {
- obj_bluetoothHolder = Instantiate(Resources.Load<GameObject>("Prefabs/BluetoothHolder"));
- SetServerIP();
- OpenBluetoothDebugView();
- //StartCoroutine(ConnectBLE());
- StartCoroutine(ConnectBleUDP());
- }
- void SetServerIP()
- {
- BluetoothClient bluetoothClient = obj_bluetoothHolder.GetComponentInChildren<BluetoothClient>();
- FieldInfo field_serverIP = bluetoothClient.GetType().GetField("serverIP", BindingFlags.Instance | BindingFlags.NonPublic);
- field_serverIP.SetValue(bluetoothClient, serverIP);
- }
- void OpenBluetoothDebugView()
- {
- BluetoothHolder bluetoothHolder = obj_bluetoothHolder.GetComponent<BluetoothHolder>();
- FieldInfo field_debug = bluetoothHolder.GetType().GetField("debug", BindingFlags.Instance | BindingFlags.NonPublic);
- field_debug.SetValue(bluetoothHolder, true);
- //改变canvas渲染模式,让3d物体渲染在前面
- Canvas[] canvasArray = bluetoothHolder.transform.GetComponentsInChildren<Canvas>();
- foreach (var canvas in canvasArray)
- {
- canvas.renderMode = RenderMode.ScreenSpaceCamera;
- canvas.worldCamera = Camera.main;
- }
- StartCoroutine(ShowDebugView(bluetoothHolder));
- }
- IEnumerator ShowDebugView(BluetoothHolder bluetoothHolder)
- {
- yield return new WaitForSeconds(0.1f);
- bluetoothHolder.openDebug();
- }
- IEnumerator ConnectBLE()
- {
- yield return null;
- #if PLATFORM_ANDROID
- BluetoothAim bluetoothAim = obj_bluetoothHolder.GetComponentInChildren<BluetoothAim>();
- MethodInfo method_DoConnect = bluetoothAim.GetType().GetMethod("DoConnect");
- method_DoConnect.Invoke(bluetoothAim, new object[]{});
- #endif
- }
- IEnumerator ConnectBleUDP()
- {
- yield return new WaitForSecondsRealtime(1);
- BluetoothAim.ins.DoConnect();
- }
- }
|