[英]How to write file to Hololens from UWP app
我正在使用 Unity 为 HoloLens 2 开发一个 UWP 应用程序。 我想写入一个日志文件(记录用户交互),然后再检索该文件。 我不希望用户必须 select 一个文件位置。
以下代码适用于桌面 UWP 应用程序(来自相同代码和相同 Unity 项目的输出)。 它在 HoloLens 上不起作用,而是抛出“未经授权”的异常。 如何写入 HoloLens 上的文件? 以后我在哪里可以找到该文件?
public class Logger : MonoBehaviour
{
StreamWriter logFileWriter;
// Start is called before the first frame update
void Start()
{
string fileName = "mydatalog.csv";
// Creates a file in the default location:
// "build\bin\x64\Release\AppX" on Desktop
this.logFileWriter = new StreamWriter(fileName);
}
void Update()
{
string record = // Stuff here...
if (this.logFileWriter != null)
{
this.logFileWriter.WriteLine(record);
}
}
}
为什么不尝试将文件路径设置为写入Application.persistentDataPath
? 在Unity 的文档中,它表示路径指向的 Windows 应用程序
%userprofile%\AppData\Local\Packages\<productname>\LocalState
所以这就是您稍后在桌面上查找文件的路径。
当我使用 Hololens 时,我曾经在那里写持久化数据。 之后,通过桌面上的 Hololens 门户,我可以导航到我编写的那个文件。
我没有使用过 Hololens 2,但我认为这个过程不会有很大不同。
无需使用文件选择器即可通过代码访问 HoloLens 设备中的文档文件夹。 这是我为读取文件创建的帮助文件,我没有测试过写入但它应该是相似的。
在 visual studio 中,您可能会看到 #if WINDOWS_UWP 中的部分变灰,以修复从 Assembly-CSharp 到 Assembly-CSharp.Player的更改(如何更改)
using UnityEngine;
using System.IO;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class FileHelper : MonoBehaviour
{
public static string path;
private void Start()
{
path = Application.streamingAssetsPath;
#if WINDOWS_UWP
Task pathTask = new Task(
async () =>
{
try
{
var folders = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFoldersAsync();
if (folders != null && folders.Count > 0) FileHelper.path = folders[0].Path.Substring(0, folders[0].Path.LastIndexOfAny(new char[2] { '/', '\\' }));
}
catch (Exception e)
{
Debug.LogError("Failed to locate documents folder!");
}
});
pathTask.Start();
#endif
}
public static string ReadFile(string filename)
{
string content = string.Empty;
if (System.IO.File.Exists(Path.Combine(path, filename)))
{
try
{
StreamReader reader = new StreamReader(Path.Combine(path, filename));
if (reader == null) return string.Empty;
content = reader.ReadToEnd();
reader.Close();
}
catch (Exception) { }
}
return content;
}
}
这不会单独工作,因为您必须在构建时编辑 package 清单文件以包含访问文档文件夹的正确权限。 要在 MRTK build window 中完成此操作,首先单击“Build Unity Project”。
在我们构建 APPX package 之前构建项目后,我们必须编辑清单文件,该文件位于:
你的项目/Temp/StagingArea/Package.appxmanifest
使用 Visual Studio Code 等代码编辑器打开文件,我们必须添加 docuentsLibrary 的功能,并包括我们将用作扩展名的文件类型。 要添加扩展,您必须将它放在应用程序选择中的 VisualElements 结束标记下。 您可以根据需要添加任意数量的文件类型,目前我包括“.json”和“.data”文件。
这是在添加功能和 filetpye 说明符之前我将编辑的 mainifest 文件的部分:(第 16 行到第 35 行)
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="ARIVE.App">
<uap:VisualElements DisplayName="HoloLens File Reader" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Template_3D" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF" />
<uap:InitialRotationPreference>
<uap:Rotation Preference="landscape" />
<uap:Rotation Preference="landscapeFlipped" />
<uap:Rotation Preference="portrait" />
<uap:Rotation Preference="portraitFlipped" />
</uap:InitialRotationPreference>
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<uap2:Capability Name="spatialPerception" />
<DeviceCapability Name="microphone" />
<DeviceCapability Name="location" />
<DeviceCapability Name="gazeinput" />
</Capabilities>
这是更新后的版本:(第 16 行到第 47 行)
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="ARIVE.App">
<uap:VisualElements DisplayName="HoloLens File Reader" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Template_3D" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF" />
<uap:InitialRotationPreference>
<uap:Rotation Preference="landscape" />
<uap:Rotation Preference="landscapeFlipped" />
<uap:Rotation Preference="portrait" />
<uap:Rotation Preference="portraitFlipped" />
</uap:InitialRotationPreference>
</uap:VisualElements>
<Extensions>
<uap:Extension Category="windows.fileTypeAssociation">
<uap:FileTypeAssociation Name="waypoint">
<uap:DisplayName>waypoint list</uap:DisplayName>
<uap:SupportedFileTypes>
<uap:FileType>.json</uap:FileType>
<uap:FileType>.data</uap:FileType>
</uap:SupportedFileTypes>
</uap:FileTypeAssociation>
</uap:Extension>
</Extensions>
</Application>
</Applications>
<Capabilities>
<uap:Capability Name="documentsLibrary" />
<uap2:Capability Name="spatialPerception" />
<DeviceCapability Name="microphone" />
<DeviceCapability Name="location" />
<DeviceCapability Name="gazeinput" />
</Capabilities>
现在您已经更新了清单,您可以 go 回到 Unity 并单击 MRTK build window 中的“Build APPX”按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.