可以通过代码访问 HoloLens 设备中的 Documents 文件夹,而无需使用文件选择器。这是我为读取文件创建的帮助文件,我没有测试过写入但它应该是相似的。
在 Visual Studio 中,您可能会看到 #if WINDOWS_UWP 中的部分变灰,以修复从 Assembly-CSharp 到 Assembly-CSharp.Player 的更改
(How to Change It)
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;
}
}
这不会单独工作,因为您必须在构建时编辑包清单文件以包含访问文档文件夹的正确权限。要在 MRTK 构建窗口中完成此操作,首先单击“构建 Unity 项目”。
在我们构建 APPX 包之前构建项目后,我们必须编辑清单文件,该文件位于:
您的项目/Temp/StagingArea/Package.appxmanifest
使用 Visual Studio Code 之类的代码编辑器打开文件,我们必须添加文档库的功能并包含我们将使用的文件类型作为扩展名。要添加扩展,您必须将其放在 Application 选择中的 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>
现在您已更新清单,您可以返回 Unity 并单击 MRTK 构建窗口中的“构建 APPX”按钮。