using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.XR.WSA.Persistence;
private const string MARKERS_FILENAME = "markers";
private List<string> PersistedAnchorMarkersIds;
public void SaveMarkersToFile()
{
string path = string.Format("{0}/{1}.json", Application.persistentDataPath, MARKERS_FILENAME);
string json = JsonConvert.SerializeObject(PersistedAnchorMarkersIds, Formatting.Indented);
byte[] data = Encoding.ASCII.GetBytes(json);
UnityEngine.Windows.File.WriteAllBytes(path, data);
Debug.Log("Data Saved to file " + path);
}
public List<string> ReadMarkersFromFile()
{
string path = string.Format("{0}/{1}.json", Application.persistentDataPath, MARKERS_FILENAME);
if (UnityEngine.Windows.File.Exists(path)) {
byte[] data = UnityEngine.Windows.File.ReadAllBytes(path);
string json = Encoding.ASCII.GetString(data);
Debug.Log("Data Loaded from file " + path);
return JsonConvert.DeserializeObject<List<string>>(json);
} else {
Debug.Log("No existing data file found, initialize empty marker list");
return new List<string>();
}
}
//存数据的调用方法:
SaveMarkersToFile();
//读的调用方法:
PersistedAnchorMarkersIds = ReadMarkersFromFile();