IEnumerator UpdateAttachment(AttachmentsConfig config, string attachment_id) { var dict = new Dictionary <string, string>(); dict["id"] = config.id + ""; dict["itemType"] = "Skin"; dict["itemId"] = config.itemId; dict["name"] = config.name; dict["attachmentId"] = attachment_id; var argu = QF.SerializeHelper.ToJson <Dictionary <string, string> >(dict); var webRequest = UnityWebRequest.Put("https://gate.mongomath.com:8443/admin-course/item/attachment/" + config.id, argu); webRequest.SetRequestHeader("Authorization", Auth); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError) { Debug.Log(": Error: " + webRequest.error); } else { Debug.Log(webRequest.downloadHandler.text); var ret = QF.SerializeHelper.FromJson <Dictionary <string, bool> >(webRequest.downloadHandler.text); var isSuccess = ret["updated"]; if (isSuccess) { MessageBoxV2.AddMessage("更新资源成功", 4); StartCoroutine(GetArgu("Skin", config.itemId)); } } }
IEnumerator BackupConfig(AttachmentsConfig config, string type) { var url = string.Format("http://gate-static.97kid.com/{0}", config.attachments.uri); UnityWebRequest webRequest = UnityWebRequest.Get(url); yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError) { Debug.Log(": Error: " + webRequest.error); } else { DateTime date = DateTime.Now; var dateStr = date.ToString("yyyyMMdd-HH时mm分ss秒"); var zipPath = DirTools.GetBackupDir() + "/" + dateStr + "." + config.attachments.ext_name; Debug.Log(zipPath); var data = webRequest.downloadHandler.data; File.WriteAllBytes(zipPath, data); if (type == "replace") { StartCoroutine(UploadConfig(config)); } else if (type == "restore") { DirTools.DeleteFolder(DirTools.GetUnZipDir()); ZipUtil.UnZipFile(zipPath, DirTools.GetUnZipDir()); StartCoroutine(Unpacker(DirTools.GetUnZipDir() + "/default.plist", DirTools.GetUnZipDir() + "/default.png")); } } }
IEnumerator UploadConfig(AttachmentsConfig config) { var filePath = DirTools.GetOutPutDir() + "/ResConfig.zip"; if (!File.Exists(filePath)) { MessageBoxV2.AddMessage("请先导出资源!"); yield return(null); } else { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); WWWForm form = new WWWForm(); form.AddBinaryData("file", bytes, "ResConfig.zip"); var webRequest = UnityWebRequest.Post("https://gate.mongomath.com:8443/admin-course/asset/uploadSingle", form); webRequest.SetRequestHeader("Authorization", Auth); //webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError) { Debug.Log(": Error: " + webRequest.error); } else { Debug.Log(webRequest.downloadHandler.text); var dict = QF.SerializeHelper.FromJson <Dictionary <string, string> >(webRequest.downloadHandler.text); var attachment_id = dict["id"]; Debug.Log("上传成功" + attachment_id); StartCoroutine(UpdateAttachment(config, attachment_id)); } } }
IEnumerator initAttachment(AttachmentsConfig config) { var obj = Instantiate(AttachmentPrefab, Content); obj.transform.Find("Name").GetComponent <Text>().text = "资源名称: " + config.name; var uri = ""; if (config.attachments.uri.IndexOf(".zip") > -1) { uri = "https://s2.ax1x.com/2019/10/09/u50uEn.png"; } else { uri = "https://s2.ax1x.com/2019/10/09/u5DlXF.png"; } WWW www = new WWW(uri);//��WWW��������ͼƬ yield return(www); var image = obj.transform.Find("Image").GetComponent <Image>(); if (www != null && string.IsNullOrEmpty(www.error)) { //��ȡTexture Texture2D texture = www.texture; //��Ϊ���Ƕ������Image������������Ҫ��Texture2Dת��ΪSprite Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); image.sprite = sprite; } var btn_repleace = obj.transform.Find("Replace").GetComponent <Button>(); btn_repleace.onClick.AddListener(() => { StartCoroutine(BackupConfig(config, "replace")); }); var btn_download = obj.transform.Find("Download").GetComponent <Button>(); btn_download.onClick.AddListener(() => { //var url = string.Format("http://gate-static.97kid.com/{0}", config.attachments.uri); //Application.OpenURL(url); StartCoroutine(GetArgu("Skin", ItemId)); }); var btn_restore = obj.transform.Find("Restore").GetComponent <Button>(); btn_restore.onClick.AddListener(() => { StartCoroutine(BackupConfig(config, "restore")); }); }