Environment.userName 在 Hololens2 中未知

Environment.userName is Unknown in Hololens2

我有一个在 Unity 中构建的 hololens2 应用程序。 Environment.Username 在 Unity 编辑器中正常工作,但是当我部署到 HL2 时,Environment.UserName 是未知的。在播放器设置下,我应用了 Enterprise Application、WebCam、Microphone、HumanInterfaceDevice、UserAccountInformation、Bluetooth、SystemManagement、UserDataTasks 和 Contacts。

它构建得很好并且可以毫无问题地部署到 HL2,但是用户名是未知的。当我进入 HL2 开始菜单时,它会正确显示我的名字。

请帮忙,

马丁 F.

请尝试以下代码,它应该显示登录到 HoloLens2 的帐户名:

using System;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
#if WINDOWS_UWP
using Windows.System;
#endif
public class UserName : MonoBehaviour
{
    [SerializeField]
    private TMP_Text userNameText = null;
    // Start is called before the first frame update
    async void Start()
    {
#if WINDOWS_UWP
        IReadOnlyList<User> users = await User.FindAllAsync();
        StringBuilder sb = new StringBuilder();
        foreach (User u in users)
        {
            string name = (string)await u.GetPropertyAsync(KnownUserProperties.AccountName);
            if (string.IsNullOrWhiteSpace(name))
            {
                name = "unknown account name";
            }
            sb.AppendLine(name);
        }
        userNameText.text = sb.ToString();
#endif
    }
}