目标:读取Hololens本地的文件,并且再不改变安装包的前提下,能修改该文件,继续读取修改后的内容(我的应用场景: Hololens程序运行,会去连接电脑开启的Sharing Service,电脑的IP想存进Txt里面,保存再Hololens上面。这样换一个网络环境,只需要修改txt里面的IP,就可以保证hololens还是可以继续连上电脑端unity开启的service。)
坑:不得不说,微软在这儿挖了一个大坑,Hololens没法用我们熟悉的方式操作文件!
网上查了很多次,只有StorageFolder类和Fi le类静态方法才能操作,经过多次尝试终于有点眉目了,特别写点东西记录下来。
Hololens应用程序(UWP,具体做过开发的都知道,hololens再unity中做好之后,最终要打包成UWP工程,进而打包成安装包)的文件结构似乎和windows有很大不同,没有盘符的概念,个人对这个研究不深,不敢乱说。 以下是网上查到的,感觉很有帮助:
http://blog.csdn.net/xxxhhhyxy/article/details/72870875 点击打开链接
http://blog.csdn.net/lindexi_gd/article/details/49007841 点击打开链接
具体:
1.首先unity创建好工程,打开读写文件读写权限(file-->build setting --> publishing setting -->capabilities -->removableStorage)。
2.创建脚本,并且绑定到任意物体上,再场景中添加一个 Text ("Canvas/Text")
#if NETFX_CORE //UWP下编译
using Windows.Storage;
using Windows.Storage.Streams;
using System.IO;
#endif
public class Test : MonoBehaviour
{
string str = "Null";
// Use this for initialization
void Start()
{
ReadData();
//GameObject.Find("Canvas/Text").GetComponent<Text>().text = ReadData();
}
// Update is called once per frame
void Update()
{
}
#if !NETFX_CORE
private void ReadData()
{
GameObject.Find("Canvas/Text").GetComponent<Text>().text = "Test";
}
#else
private async void ReadData()
{
StorageFolder docLib = ApplicationData.Current.LocalFolder;
// var docFile = docLib.OpenStreamForReadAsync("\\Test.txt");
// 获取应用程序数据存储文件夹
Stream stream = await docLib.OpenStreamForReadAsync("\\Test.txt");
// 获取指定的文件的文本内容
byte[] content = new byte[stream.Length];
await stream.ReadAsync(content, 0, (int)stream.Length);
GameObject.Find("Canvas/Text").GetComponent<Text>().text = Encoding.UTF8.GetString(content, 0, content.Length);
}
#endif
}
3.浏览器中输入Hololens IP,进入Hololens管理面板右侧,选中“File Explorer-->LocalAppData-->你的应用名称-->LocalState” ,点击浏览,上传电脑本地写好的Test.Txt。
4.下次修改IP,找到上述目录,删除该txt,重新上传一份改过ip的txt,然后重启Hololens上你得应用程序就行。
仅供参考,Hololens才开始学,有很多坑需要踩。