【问题标题】:How to write file to Hololens from UWP app如何从 UWP 应用程序将文件写入 Hololens
【发布时间】:2021-01-22 21:25:25
【问题描述】:

我正在使用 Unity 为 HoloLens 2 开发 UWP 应用。我想写入日志文件(记录用户交互),然后再检索该文件。我不希望用户必须选择文件位置。

以下代码适用于桌面 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);
        }
    }
}

【问题讨论】:

  • 请确保您的应用程序符合UWP File system 策略并具有访问设备资源的权限。另外,“build\bin\x64\Release\AppX”这个目录路径看起来像是一个从Unity构建的输出项目,与Visual Studio部署在HoloLens上的应用安装目录不同。
  • 我们建议您使用 Unity API Application.persistentDataPath 提供的文件目录路径来存储数据,或者使用 UWP API Windows.Storage 来管理文件。
  • 我以后有办法手动检索保存在那里的文件吗?例如,通过文件资源管理器?该项目用于学术研究,我只需要该应用程序在实验/学习期间运行时记录数据。然后,我将手动获取该日志数据并稍后使用它。
  • Karina Rigby 的回答将满足您的需求,这也是我们推荐的保存文件的方式之一。请随时告诉我们您的反馈意见。

标签: c# unity3d uwp hololens file-writing


【解决方案1】:

您为什么不尝试将文件路径设置为写入Application.persistentDataPath?在Unity's docs 中,它表示路径指向的 Windows 应用程序

%userprofile%\AppData\Local\Packages\<productname>\LocalState

这就是您以后在桌面上查找文件的路径。

当我使用 Hololens 时,我曾经在那里写入持久数据。之后,通过桌面上的 Hololens 门户,我可以导航到我编写的那个文件。

我没有使用过 Hololens 2,但我认为这个过程不会有很大不同。

【讨论】:

  • 这适用于桌面。我现在遇到的问题是 Hololens 上的“ApplicationData”文件夹在尝试在桌面上查看时显示为空,而我似乎无法在 Hololens 本身上查看它。
  • @MichaelKintscher 您是否正在使用 Hololens 开发者门户来尝试访问它?要查看该特定路径中的数据,您需要通过 Hololens Developer 门户访问它。查看此链接以获取 Microsoft 的 file viewing on the Hololens through the Dev portal
【解决方案2】:

可以通过代码访问 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”按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多