Hololens读取本地文件 string[] content = File.ReadAllLines("setup.ini"); foreach (va

参考https://blog.csdn.net/zq1564171310/article/details/77528936

读取ini中的ip地址和零件位置:

private async void ReadData()
        {
            StorageFolder docLib = ApplicationData.Current.LocalFolder;
            Stream stream = await docLib.OpenStreamForReadAsync("\\setupforhololens.ini");
            byte[] content = new byte[stream.Length];
            await stream.ReadAsync(content, 0, (int)stream.Length);
            string str = Encoding.UTF8.GetString(content, 0, content.Length);

            string[] sArray = str.Split(new string[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//遇到空格和换行就分割,然后从所有结果中取出ip地址和零件位置
            for (int i = 0; i < sArray.Length; i++)
            {
                if (sArray[i] == "ServerAddress")
                {
                    ServerAddress = sArray[i + 1];
                }
                if (sArray[i] == "PrefabAddress")
                {
                    PrefabAddress = sArray[i + 1];
                }
            }
        }

Hololens中的存取位置:

浏览器中输入Hololens IP,进入Hololens管理面板右侧,选中“File Explorer-->LocalAppData-->你的应用名称-->LocalState”  ,点击浏览,上传电脑本地写好的ini文件。

待读取ini文件内容:

ServerAddress 192.168.1.102
PrefabAddress http://192.168.1.102//MRCollaboration/Puzzle/

对应的unity中的读取方法:

   void Read Date()
{
            string[] content = File.ReadAllLines("setup.ini");
            foreach (var item in content)
            {
                string line = item.ToString();
                string[] vars = line.Split(' ');
                if (vars[0].ToString() == "Prefabs")
                {
                    for (int i = 1; i < vars.Length; i++)
                    {
                        GameObject hp_bar = (GameObject)Resources.Load(vars[i].ToString());
                        hp_bar = Instantiate(hp_bar);
                        hp_bar.name = vars[i].ToString();
                    }
                }
                else if (vars[0].ToString() == "ServerAddress")
                {
                    ServerAddress = vars[1].ToString();
                }
                else if (vars[0].ToString() == "PrefabAddress")
                {
                    PrefabAddress = vars[1].ToString();
                }
            }
}

 

你可能感兴趣的:(Hololens)