static void OnMapUploaded(ref PNTransferStatusUnity status, IntPtr contextPtr) { GCHandle handle = GCHandle.FromIntPtr(contextPtr); SaveLoadContext context = handle.Target as SaveLoadContext; Action <bool, bool, float> progressCb = context.progressCb; PNTransferStatusUnity statusClone = status; Debug.Log(String.Format("mapId {0} completed {1} faulted {2} bytesTransferred {3} bytesTotal {4}", status.mapId, status.completed, status.faulted, status.bytesTransferred, status.bytesTotal) ); MainThreadTaskQueue.InvokeOnMainThread(() => { if (statusClone.completed) { Debug.Log("Uploaded map!"); progressCb(true, false, 1); handle.Free(); } else if (statusClone.faulted) { Debug.Log("Failed to upload map!"); progressCb(false, true, 0); handle.Free(); } else { Debug.Log("Uploading map!"); progressCb(false, false, (float)(statusClone.bytesTransferred) / statusClone.bytesTotal); } }); }
/// <summary> /// Saves the map being created by the running mapping session /// </summary> /// <param name="savedCb">Callback to publish a event upon the map being saved.</param> /// <param name="progressCb">Callback to publish the progress of the map upload.</param> public void SaveMap(Action <String> savedCb, Action <bool, bool, float> progressCb) { SaveLoadContext context = new SaveLoadContext(); context.savedCb = savedCb; context.progressCb = progressCb; IntPtr cSharpContext = GCHandle.ToIntPtr(GCHandle.Alloc(context)); #if !UNITY_EDITOR PNAddMap(OnMapSaved, cSharpContext); #else savedCb("123456789"); progressCb(true, false, 1.0f); #endif }
/// <summary> /// Saves the map being created by the running mapping session /// </summary> /// <param name="savedCb">Callback to publish a event upon the map being saved.</param> /// <param name="progressCb">Callback to publish the progress of the map upload.</param> public void SaveMap(Action <String> savedCb, Action <bool, bool, float> progressCb) { SaveLoadContext context = new SaveLoadContext(); context.savedCb = savedCb; context.progressCb = progressCb; #if !UNITY_EDITOR IntPtr cSharpContext = GCHandle.ToIntPtr(GCHandle.Alloc(context)); PNAddMap(OnMapSaved, cSharpContext); #else /// Setting map Id simMap.placeId = Guid.NewGuid().ToString(); /// Setting saved camera poses simMap.userData = JsonConvert.SerializeObject(simCameraPoses.cameraPoses); string jsonMap = JsonConvert.SerializeObject(simMap); /// The file does not exist yet OR The file exists but does not contain '[]' if (!File.Exists(Application.dataPath + simMapFileName) || File.ReadAllText(Application.dataPath + simMapFileName).ToString() == "") { File.WriteAllText(Application.dataPath + simMapFileName, "[" + jsonMap + "]"); } else { string currMapData = File.ReadAllText(Application.dataPath + simMapFileName); var mapInfoList = JsonConvert.DeserializeObject <List <MapInfo> >(currMapData); /// The file exists but has no maps if (mapInfoList == null) { File.WriteAllText(Application.dataPath + simMapFileName, jsonMap); } else /// If there is already more than 1 item in the file { mapInfoList.Add(simMap); var convertedJson = JsonConvert.SerializeObject(mapInfoList); File.WriteAllText(Application.dataPath + simMapFileName, convertedJson); } } savedCb(simMap.placeId); progressCb(true, false, 1.0f); #endif }
static void OnMapSaved(ref PNCallbackResultUnity result, IntPtr contextPtr) { GCHandle handle = GCHandle.FromIntPtr(contextPtr); SaveLoadContext context = handle.Target as SaveLoadContext; Action <String> savedCb = context.savedCb; PNCallbackResultUnity resultClone = result; MainThreadTaskQueue.InvokeOnMainThread(() => { if (resultClone.success) { String mapId = resultClone.msg; Debug.Log("Added a record to map db with id " + mapId); PNSaveMap(mapId, OnMapUploaded, contextPtr); savedCb(mapId); } else { Debug.Log(String.Format("Failed to add the map! Error msg: %s", resultClone.msg)); savedCb(null); handle.Free(); } }); }