MRTK AzureSpatialAnchors 教程打包报错 Reference Rewriter found some errors while running with command &quo
Azure 空间定位点入门 - Mixed Reality | Microsoft Docs
按照教程,导入下面的包后,打包会报错
- MRTK.HoloLens2.Unity.Tutorials.Assets.AzureSpatialAnchors.2.4.0.unitypackage
这因为这个包中使用了"RestSharp.dll"引起的。
Build errors when attempting multiplayer · Issue #246 · MicrosoftDocs/mixed-reality (github.com)
在这个issue中官方说会更新这个包,用unity的api替换resetsharp。
按照这个思路,查了一下是在"Assets\MRTK.Tutorials.AzureSpatialAnchors\Scripts\AnchorModuleScript.cs"中引用了这个dll,实现了一个文件上传的功能。
把这部分自己用unitywebrequest实现一下就可以了。
直接修改这一部分代码:
1 public void ShareAzureAnchorIdToNetwork() 2 { 3 Debug.Log("\nAnchorModuleScript.ShareAzureAnchorID()"); 4 5 string filename = "SharedAzureAnchorID." + publicSharingPin; 6 string path = Application.persistentDataPath; 7 8 #if WINDOWS_UWP 9 StorageFolder storageFolder = ApplicationData.Current.LocalFolder; 10 path = storageFolder.Path + "/"; 11 #endif 12 13 string filePath = Path.Combine(path, filename); 14 File.WriteAllText(filePath, currentAzureAnchorID); 15 16 Debug.Log($"Current Azure anchor ID '{currentAzureAnchorID}' successfully saved to path '{filePath}'"); 17 18 //try 19 //{ 20 // var client = new RestClient("http://167.99.111.15:8090"); 21 22 // Debug.Log($"Connecting to network client '{client}'... please wait..."); 23 24 // var request = new RestRequest("/uploadFile.php", Method.POST); 25 // request.AddHeader("Accept", "application/json"); 26 // request.AddHeader("Content-Type", "multipart/form-data"); 27 // request.AddFile("the_file", filePath); 28 // request.AddParameter("replace_file", 1); // Only needed if you want to upload a static file 29 30 // var httpResponse = client.Execute(request); 31 32 // Debug.Log("Uploading file... please wait..."); 33 34 // string json = httpResponse.Content.ToString(); 35 //} 36 //catch (Exception ex) 37 //{ 38 // Debug.Log(string.Format("Exception: {0}", ex.Message)); 39 // throw; 40 //} 41 42 var form = new WWWFormHelper(); 43 form.AddFile("the_file", filePath); 44 form.AddField("replace_file", 1); 45 var header = new Dictionary<string, string>(); 46 header.Add("Accept", "application/json"); 47 header.Add("Content-Type", "multipart/form-data"); 48 UnityWebRequestHelper.Instance.Post("http://167.99.111.15:8090/uploadFile.php", form, str => 49 { 50 Debug.Log("Uploading file... please wait..."); 51 52 string json = str; 53 }, header); 54 55 Debug.Log($"Current Azure anchor ID '{currentAzureAnchorID}' shared successfully"); 56 }
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.IO; 5 using UnityEngine; 6 using UnityEngine.Networking; 7 8 public class UnityWebRequestHelper : MonoBehaviour 9 { 10 #region SingleTon 11 private static UnityWebRequestHelper instance; 12 public static UnityWebRequestHelper Instance { get => instance; private set { } } 13 #endregion 14 15 private void Awake() 16 { 17 instance = this; 18 } 19 20 public void Post(string url, WWWFormHelper form, Action<string> callBack, Dictionary<string, string> header = null) 21 { 22 StartCoroutine(DoPost(url, form, callBack, header)); 23 } 24 25 IEnumerator DoPost(string url, WWWFormHelper form, Action<string> callBack, Dictionary<string, string> header) 26 { 27 UnityWebRequest webRequest = 28 UnityWebRequest.Post(url, form); 29 //使用UnitywebRequest将表单数据发送到服务器 30 SetHeader(webRequest, header); 31 yield return webRequest.SendWebRequest();//开始发送数据 32 //异常处理 33 callBack?.Invoke(webRequest.downloadHandler.text); 34 } 35 36 private void SetHeader(UnityWebRequest webRequest, Dictionary<string, string> header) 37 { 38 if (header == null) return; 39 foreach (var key in header.Keys) 40 { 41 webRequest.SetRequestHeader(key, header[key]); 42 } 43 } 44 }
using System.IO; using UnityEngine; public class WWWFormHelper : WWWForm { public void AddFile(string fieldName, string filePath) { byte[] file = File.ReadAllBytes(filePath);//读取本地文件转换为二进制流 this.AddBinaryData(fieldName, file);//添加二进制字节流到表单 } }
最后一步,删掉"Assets\MRTK.Tutorials.AzureSpatialAnchors\Plugins\RestSharp.dll"