lvjincheng 3 роки тому
батько
коміт
2a59374c86

+ 1 - 1
Assets/BowArrow/Scripts/Game/BowCamera.cs

@@ -173,7 +173,7 @@ public class BowCamera : MonoBehaviour
         }
         onAfterLateUpdate?.Invoke();
     }
-    Action onAfterLateUpdate;
+    [NonSerialized] public Action onAfterLateUpdate;
 
     //---------------相机视野的相关操作---------------------
 

+ 20 - 11
Assets/Test9Axis/AutoConnectBLE.cs

@@ -8,23 +8,25 @@ 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(ConnectBLE());
+        StartCoroutine(ConnectBleUDP());
     }
 
-    void SetServerIP() 
+    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() {
+    void OpenBluetoothDebugView()
+    {
         BluetoothHolder bluetoothHolder = obj_bluetoothHolder.GetComponent<BluetoothHolder>();
         FieldInfo field_debug = bluetoothHolder.GetType().GetField("debug", BindingFlags.Instance | BindingFlags.NonPublic);
         field_debug.SetValue(bluetoothHolder, true);
@@ -34,24 +36,31 @@ public class AutoConnectBLE : MonoBehaviour
         foreach (var canvas in canvasArray)
         {
             canvas.renderMode = RenderMode.ScreenSpaceCamera;
-            canvas.worldCamera = Camera.main;       
+            canvas.worldCamera = Camera.main;
         }
 
         StartCoroutine(ShowDebugView(bluetoothHolder));
     }
 
-    IEnumerator ShowDebugView(BluetoothHolder bluetoothHolder) {
-        yield return new WaitForSeconds(0.1f); 
+    IEnumerator ShowDebugView(BluetoothHolder bluetoothHolder)
+    {
+        yield return new WaitForSeconds(0.1f);
         bluetoothHolder.openDebug();
     }
-    
-    IEnumerator ConnectBLE() 
+
+    IEnumerator ConnectBLE()
     {
         yield return null;
-        #if PLATFORM_ANDROID
+#if PLATFORM_ANDROID
             BluetoothAim bluetoothAim = obj_bluetoothHolder.GetComponentInChildren<BluetoothAim>();
             MethodInfo method_DoConnect = bluetoothAim.GetType().GetMethod("DoConnect");
             method_DoConnect.Invoke(bluetoothAim, new object[]{});
-        #endif
+#endif
+    }
+
+    IEnumerator ConnectBleUDP()
+    {
+        yield return new WaitForSecondsRealtime(1);
+        BluetoothAim.ins.DoConnect();
     }
 }

+ 23 - 10
Assets/Test9Axis/BleUDP.cs

@@ -1,4 +1,4 @@
-using System.Collections;
+锘縰sing System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
@@ -9,16 +9,16 @@ using System.Threading;
 
 public class BleUDP : MonoBehaviour
 {
-    public static BleUDP ins; //单例记录
+    public static BleUDP ins; //鍗曚緥璁板綍
 
-    readonly string localIP = "127.0.0.1"; //本地IP
-    readonly int myPort = 8001; //自身的注册端口
-    readonly int targetPort = 8000; //发消息的目标端口
-    Socket client; //UDP对象
+    readonly string localIP = "127.0.0.1"; //鏈�湴IP
+    readonly int myPort = 8001; //鑷�韩鐨勬敞鍐岀�鍙�
+    readonly int targetPort = 8000; //鍙戞秷鎭�殑鐩�爣绔�彛
+    Socket client; //UDP瀵硅薄
 
-    float noneMsgTime = 0; //多久没收到字节消息了
-    Queue<byte[]> byteQueue = new Queue<byte[]>(); //接收信息的线程把字节信息放这里
-    Queue<byte[]> byteMainQueue = new Queue<byte[]>(); //主线程从byteQueue中取出数据后放在这里,供主线程update使用
+    float noneMsgTime = 0; //澶氫箙娌℃敹鍒板瓧鑺傛秷鎭�簡
+    Queue<byte[]> byteQueue = new Queue<byte[]>(); //鎺ユ敹淇℃伅鐨勭嚎绋嬫妸瀛楄妭淇℃伅鏀捐繖閲�
+    Queue<byte[]> byteMainQueue = new Queue<byte[]>(); //涓荤嚎绋嬩粠byteQueue涓�彇鍑烘暟鎹�悗鏀惧湪杩欓噷锛屼緵涓荤嚎绋媢pdate浣跨敤
 
     public Action OnConnected;
     public Action OnConnectionFailed;
@@ -39,6 +39,19 @@ public class BleUDP : MonoBehaviour
 
         client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         client.Bind(new IPEndPoint(IPAddress.Parse(localIP), myPort));
+
+        /*
+         C# UDP Socket ReceiveFrom 杩滅▼涓绘満寮鸿揩鍏抽棴浜嗕竴涓�幇鏈夌殑杩炴帴銆�
+        锛堢粡杩囧弽澶嶇爺绌讹紝涓嬭堪鎯呭喌鐨勫師鍥犵粓浜庢悶娓呮�浜嗭紝鏄�"ICMP port unreachable"鐨勯棶棰橈紝鍗筹細鑻ュ悜涓€涓�病鏈夌浉搴擴DP鐩戝惉绔�彛鐨勬湰鏈哄湴鍧€锛堟瘮濡�127.0.0.1锛夊彂閫乁DP鏁版嵁鍖咃紝浼氬洖澶岻CMP port unreachable鍖咃紝鑰岃繖涓�寘浼氳�C#鐨刄DP Socket ReceiveFrom鍑芥暟寰楀埌锛屽苟鎶ラ敊涓衡€滆繙绋嬩富鏈哄己杩�叧闂�簡涓€涓�幇鏈夎繛鎺モ€濓紒锛�
+        锛堣€屼负浠€涔堝悜瀛樺湪鐨勫彟涓€涓猧p锛堝彟涓€鍙颁富鏈猴級鍙戦€佷笉浼氭姤閿欙紵鍥犱负闃茬伀澧欙紒win10鐨勯槻鐏��鎶婂�閮ㄥ洖澶嶇殑ICMP port unreachable缁欏睆钄芥帀浜嗐€傜粡瀹為獙锛屾妸闃茬伀澧欏叧鎺夊悗锛屽氨浼氬嚭鐜颁笂杩版姤閿欎簡銆傦級
+        锛堣€屼负浠€涔堝悜涓€涓�笉瀛樺湪鐨刬p鍦板潃鍙戦€佷笉浼氭姤閿欙紵鍥犱负涓绘満涓嶅瓨鍦�紝鎵€浠ヤ笉浼氬洖澶岻CMP port unreachable锛�
+         */
+        //閰嶇疆socket鍙傛暟锛岄伩鍏岻CMP鍖呭�鑷寸殑寮傚父
+        uint IOC_IN = 0x80000000;
+        uint IOC_VENDOR = 0x18000000;
+        uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
+        client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
+
         Thread threadReciveMsg = new Thread(ReciveMsg);
         threadReciveMsg.Start();
     }
@@ -111,7 +124,7 @@ public class BleUDP : MonoBehaviour
             EndPoint point = new IPEndPoint(IPAddress.Any, 0);
             byte[] buffer = new byte[1024];
             int length = client.ReceiveFrom(buffer, ref point);
-            if (!point.ToString().EndsWith(targetPort.ToString())) return;
+            if (!point.ToString().EndsWith(targetPort.ToString())) continue;
             if (length <= 0) continue;
             byte[] bytes = new byte[length];
             Array.Copy(buffer, bytes, length);

+ 76 - 0
Assets/Test9Axis/MultipleCrossHair.cs

@@ -0,0 +1,76 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+
+//多准心渲染姿态角-测试用
+public class MultipleCrossHair : MonoBehaviour
+{
+    List<RectTransform> crossHairList = new List<RectTransform>();
+
+    void Start()
+    {
+        BowCamera.ins.onAfterLateUpdate += MyUpdate;
+
+        crossHairList.Add(CreateNewCrossHair(Color.green));
+        crossHairList.Add(CreateNewCrossHair(Color.yellow));
+    }
+
+    readonly Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
+
+    void MyUpdate()
+    {
+        UpdateCrossHairByRotation(crossHairList[0], Quaternion.identity);
+        UpdateCrossHairByRotation(crossHairList[0], Quaternion.identity);
+    }
+
+    public void UpdateCrossHairByRotation(RectTransform rectTF, Quaternion rotation)
+    {
+        Vector3 theAngles = rotation.eulerAngles;
+        float bowRotateConvertRate = UserSettings.ins.bowRotateConvert.GetRate();
+        theAngles.x = Mathf.Clamp((theAngles.x > 180 ? theAngles.x - 360 : theAngles.x) * bowRotateConvertRate,
+            -80, 80);
+        theAngles.y = Mathf.Clamp((theAngles.y > 180 ? theAngles.y - 360 : theAngles.y) * bowRotateConvertRate,
+            -80, 80);
+
+        Transform TF = BowCamera.ins.transform;
+        Vector3 forward = GameMgr.ins.transform.rotation * (Quaternion.Euler(theAngles) * Vector3.forward);
+        Ray ray = new Ray(TF.position, forward);
+        RaycastHit hitInfo;
+        Vector3 targetPosition;
+        if (Physics.Raycast(ray, out hitInfo, 100))
+        {
+            targetPosition = hitInfo.point;
+        }
+        else
+        {
+            targetPosition = TF.position + forward * 100f;
+        }
+        RectTransform parentRTF = CrossHair.ins.transform.parent.GetComponent<RectTransform>();
+        Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
+        v3.x *= parentRTF.rect.width;
+        v3.y *= parentRTF.rect.height;
+        v3.z = 0;
+        rectTF.localPosition = v3;
+    }
+
+    //创建一个新的准心
+    RectTransform CreateNewCrossHair(Color color)
+    {
+        Transform parentTF = CrossHair.ins.transform.parent;
+        RectTransform RTF = new GameObject().AddComponent<RectTransform>();
+        RTF.SetParent(parentTF);
+        RTF.localRotation = Quaternion.identity;
+        RTF.localPosition = Vector3.zero;
+        RTF.localScale = new Vector3(1, 1, 1);
+        RTF.gameObject.AddComponent<CanvasRenderer>().cullTransparentMesh = false;
+        Image imgComp = RTF.gameObject.AddComponent<Image>();
+        imgComp.sprite = CrossHair.ins.GetComponent<Image>().sprite;
+        imgComp.SetNativeSize();
+        Material mat = new Material(Shader.Find("Custom/Outlight"));
+        mat.SetColor("_LightColor", color);
+        mat.SetInt("_Size", 30);
+        imgComp.material = mat;
+        return RTF;
+    }
+}

+ 11 - 0
Assets/Test9Axis/MultipleCrossHair.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 34ce7db7d6e8553499652be3765ff55f
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: