Browse Source

添加消息组件和实现下载Item

DGJ 1 year ago
parent
commit
742ac02d0a

+ 78 - 1
Assets/2.0/Tools/DownLoadItem.cs

@@ -1,8 +1,85 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Text;
 using UnityEngine;
+using UnityEngine.Networking;
 
 public class DownLoadItem : MonoBehaviour
 {
-  
+    /// <summary>
+    ///  下载地址
+    /// </summary>
+    public string downLoadPath;
+    /// <summary>
+    /// 是否下载成功
+    /// </summary>
+    public bool isDownLoad;
+    /// <summary>
+    /// 下载成功后的文件
+    /// </summary>
+    public byte[] downLoadData;
+    /// <summary>
+    /// 进度条
+    /// </summary>
+    public float progress;
+    /// <summary>
+    /// 下载后状态
+    /// </summary>
+    public string downLoadState;
+
+    private UnityWebRequest webRequest;
+    private string baseurl = "http://office.ghz-tech.com:9904/api";
+
+    public void Init(string downLoadPath, Action<bool> CallBacke)
+    {
+        this.downLoadPath = downLoadPath;
+        StartCoroutine(DownloadFile(CallBacke));
+    }
+
+    private System.Collections.IEnumerator DownloadFile(Action<bool> CallBacke)
+    {
+        string url = baseurl + "/file/download";
+        using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
+        {
+            byte[] bodyRaw = Encoding.UTF8.GetBytes(downLoadPath);
+            webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
+            webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
+            webRequest.SendWebRequest();
+
+            while (!webRequest.isDone)
+            {
+                // 此处可以显示下载进度条等UI操作
+                progress = webRequest.downloadProgress;
+               // Debug.Log("Download Progress: " + progress);
+                yield return null;
+            }
+
+            if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
+            {
+                Debug.LogError("Download Failed: " + webRequest.error);
+                // 下载失败 
+                downLoadState = webRequest.error;
+                isDownLoad = false;              
+            }
+            else
+            {
+                downLoadState = "下载成功";
+                downLoadData = webRequest.downloadHandler.data;
+                isDownLoad = true;  
+               // MsgHandler.AddListener("",)
+            }
+            CallBacke(isDownLoad);
+        }
+    }
+
+    private void OnDestroy()
+    {
+        // 断开下载连接
+        if (webRequest != null && !webRequest.isDone)
+        {
+            webRequest.Abort();
+        }
+    }
+
 }

+ 84 - 0
Assets/2.0/Tools/MsgHandler.cs

@@ -0,0 +1,84 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class MsgHandler : MonoBehaviour
+{
+    public delegate void DelMsgHandler(Msg msg);
+
+    private static Dictionary<string, DelMsgHandler> mDicMsgs = new Dictionary<string, DelMsgHandler>();
+
+
+    /// <summary>
+    /// 添加监听者
+    /// </summary>
+    /// <param name="msgType"></param>
+    /// <param name="handler"></param>
+    public static void AddListener(string msgType, DelMsgHandler handler)
+    {
+        //判空
+        if (mDicMsgs == null) mDicMsgs = new Dictionary<string, DelMsgHandler>();
+        if (!mDicMsgs.ContainsKey(msgType)) mDicMsgs.Add(msgType, null);
+        //增加监听
+        mDicMsgs[msgType] += handler;
+
+    }
+    /// <summary>
+    /// 去除对参数handler的监听
+    /// </summary>
+    /// <param name="msgType">消息类型</param>
+    /// <param name="handler">被监听方法</param>
+    public static void RemoveListener(string msgType, DelMsgHandler handler)
+    {
+        if (mDicMsgs != null && mDicMsgs.ContainsKey(msgType)) mDicMsgs[msgType] -= handler;
+    }
+
+    
+    /// <summary>
+    /// 清除所有的监听者
+    /// </summary>
+    public static void ClearAllListeners()
+    {
+        if (mDicMsgs != null) mDicMsgs.Clear();
+    }
+
+    /// <summary>
+    /// 分发消息
+    /// </summary>
+    /// <param name="msgType">消息类型</param>
+    /// <param name="msg">分发的内容</param>
+    public static void SendMsg(string msgType, Msg msg)
+    {
+        DelMsgHandler handler;
+
+        if (mDicMsgs != null && mDicMsgs.TryGetValue(msgType, out handler))
+        {
+
+            if (handler != null)
+                handler(msg);
+        }
+    }
+
+    /// <summary>
+    /// 群发消息
+    /// </summary>
+    /// <param name="msg">消息内容</param>
+    public static void AllSendMsg(Msg msg)
+    {
+        foreach (var item in mDicMsgs)
+        {
+            item.Value(msg);
+        }
+    }
+}
+
+public class Msg
+{
+    public string Key { get; private set; }
+    public object Value { get; private set; }
+    public Msg(string key, object value)
+    {
+        this.Key = key;
+        this.Value = value;
+    }
+}

+ 11 - 0
Assets/2.0/Tools/MsgHandler.cs.meta

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

+ 1 - 1
Assets/Scripts/BaseManager/EventManager/EventManager.cs

@@ -1,4 +1,4 @@
-using SC.XR.Unity;
+using SC.XR.Unity;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

+ 24 - 0
Packages/packages-lock.json

@@ -200,6 +200,30 @@
       },
       "url": "https://packages.unity.cn"
     },
+    "com.unity.xr.core-utils": {
+      "version": "2.0.0",
+      "depth": 2,
+      "source": "registry",
+      "dependencies": {
+        "com.unity.modules.xr": "1.0.0"
+      },
+      "url": "https://packages.unity.cn"
+    },
+    "com.unity.xr.interaction.toolkit": {
+      "version": "2.0.4",
+      "depth": 1,
+      "source": "registry",
+      "dependencies": {
+        "com.unity.inputsystem": "1.3.0",
+        "com.unity.ugui": "1.0.0",
+        "com.unity.xr.core-utils": "2.0.0",
+        "com.unity.xr.legacyinputhelpers": "2.1.8",
+        "com.unity.modules.audio": "1.0.0",
+        "com.unity.modules.imgui": "1.0.0",
+        "com.unity.modules.physics": "1.0.0"
+      },
+      "url": "https://packages.unity.cn"
+    },
     "com.unity.xr.interactionsubsystems": {
       "version": "1.0.1",
       "depth": 0,