在Hololens 2上,Application.Quit()函数不一定会关闭应用程序,因为Hololens 2操作系统会自动管理应用程序的生命周期。如果需要关闭应用程序,可以使用以下代码示例:
#if UNITY_WSA && !UNITY_EDITOR using UnityEngine; using UnityEngine.XR.WSA;
public class AppCloser : MonoBehaviour { public void CloseApp() { Application.Quit(); if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Core.CoreWindow")) { var coreWindow = Windows.UI.Core.CoreWindow.GetForCurrentThread(); coreWindow.Close(); } else { var view = Windows.ApplicationModel.Core.CoreApplication.GetCurrentView(); view?.CoreWindow?.Close(); } } } #endif
将以上代码示例放置在需要关闭应用程序的脚本中,然后调用CloseApp()函数即可关闭Hololens 2上的应用程序。