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("Prefabs/BluetoothHolder")); SetServerIP(); OpenBluetoothDebugView(); //StartCoroutine(ConnectBLE()); StartCoroutine(ConnectBleUDP()); } void SetServerIP() { BluetoothClient bluetoothClient = obj_bluetoothHolder.GetComponentInChildren(); FieldInfo field_serverIP = bluetoothClient.GetType().GetField("serverIP", BindingFlags.Instance | BindingFlags.NonPublic); field_serverIP.SetValue(bluetoothClient, serverIP); } void OpenBluetoothDebugView() { BluetoothHolder bluetoothHolder = obj_bluetoothHolder.GetComponent(); FieldInfo field_debug = bluetoothHolder.GetType().GetField("debug", BindingFlags.Instance | BindingFlags.NonPublic); field_debug.SetValue(bluetoothHolder, true); //改变canvas渲染模式,让3d物体渲染在前面 Canvas[] canvasArray = bluetoothHolder.transform.GetComponentsInChildren(); 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(); MethodInfo method_DoConnect = bluetoothAim.GetType().GetMethod("DoConnect"); method_DoConnect.Invoke(bluetoothAim, new object[]{}); #endif } IEnumerator ConnectBleUDP() { yield return new WaitForSecondsRealtime(1); BluetoothAim.ins.DoConnect(); } }