Hololens 读写本地配置文件
用Unity开发适用于Hololens的UWP平台应用,会发现Application.streamingAssetsPath路径是不存在的,其他几个路径也是类似,如果需要进行读写配置文件,我们应该怎么做呢?
既然开发的UWP应用,就必须要遵循UWP读写文件的规则,在UWP里多用StorageFile来读写文件
StorageFile没有直接的打开一个文件的做法,而是通过StorageFolder创建,打开文件来进行
下面的代码是一个简单的读取Hololens硬件码并保存在本地,每次启动时进行比对,不一样就退出程序的脚本
using UnityEngine;
using Windows.Storage;
public class SimpleAuthorization : MonoBehaviour
{
private string _systemInfo;
private void Start ()
{
string deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
string graphicsDeviceID = SystemInfo.graphicsDeviceID.ToString();
_systemInfo = deviceUniqueIdentifier + " : " + graphicsDeviceID;
WriteFileAsync();
ReadFileAsync();
}
// 读取
private async System.Threading.Tasks.Task ReadFileAsync()
{
Windows.Storage.StorageFolder folder;
folder = Windows.Storage.ApplicationData.Current.RoamingFolder;
Windows.Storage.StorageFile file = await folder.TryGetItemAsync("SystemInfo.xml") as Windows.Storage.StorageFile;
if (file != null)
{
string positionstring = await Windows.Storage.FileIO.ReadTextAsync(file);
if (_systemInfo != positionstring)
{
Application.Quit();
}
}
else
{
Application.Quit();
}
}
// 写入
private async System.Threading.Tasks.Task WriteFileAsync()
{
Windows.Storage.StorageFolder folder;
folder = Windows.Storage.ApplicationData.Current.RoamingFolder;
Windows.Storage.StorageFile file = await folder.CreateFileAsync("SystemInfo_backup.xml", Windows.Storage.CreationCollisionOption.ReplaceExisting);
using (Windows.Storage.StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
{
using (Windows.Storage.Streams.DataWriter dataWriter = new Windows.Storage.Streams.DataWriter(transaction.Stream))
{
dataWriter.WriteString(_systemInfo);
transaction.Stream.Size = await dataWriter.StoreAsync();
await transaction.CommitAsync();
}
}
}
}
可以进入Hololens控制台, System -> File explorer -> LocalAppdata -> 应用程序包名 -> RoamingState 中查看写入的配置文件