Unity跳过闪屏页
1.Unity读取xml文件
2.Unity跳过闪屏页
Unity API文档,Unity暴露了SplashScreen.Stop() 停止启动屏的API
只需要写个静态方法,使用[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]即在在显示启动画面之前调用这个静态方法,在静态方法中调用SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate)来立即停止启动屏。
RuntimeInitializeLoadType变量
AfterSceneLoad 在场景加载后。
BeforeSceneLoad 在场景加载前。
AfterAssembliesLoaded 加载完所有程序集并初始化预加载资源时的回调。
BeforeSplashScreen 在显示启动画面之前。
SubsystemRegistration 用于子系统注册的回调
使用方法:
- 将下面脚本文件直接作为Runtime代码放到项目里(注意,不是Editor代码,是运行时代码)
- 打包->运行。非常好用,无需破解,官方支持,全平台适用。
#if !UNITY_EDITOR
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Scripting;
[Preserve]
public class SkipUnityLogo
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void BeforeSplashScreen()
{
#if UNITY_WEBGL
Application.focusChanged += Application_focusChanged;
#else
System.Threading.Tasks.Task.Run(AsyncSkip);
#endif
}
#if UNITY_WEBGL
private static void Application_focusChanged(bool obj)
{
Application.focusChanged -= Application_focusChanged;
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#else
private static void AsyncSkip()
{
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#endif
}
#endif
Tips:
- [Preserver]可以显式声明在构件中保留的代码,防止程序打包后,需要的代码没有打包进程序中(代码在构建过程中会删除未使用或不可访问的代码,减少程序的最终大小)
当使用XR项目时
当使用XR项目时,需要在上述的基础上做以下处理:
- 在Editor -> Project Settings -> XR Plug-in Management中,取消勾选Initial XR On SetUp
- 这时XR相关的东西不会在进入程序时自动初始化,经测试是因为XR相关的东西自动初始化造成的跳过闪屏页失败,所以现在需要在跳过闪屏页后手动初始化XR
- 手动初始化XR脚本如下:
using System.Collections;
using UnityEngine;
using UnityEngine.XR.Management;
public class XRInitializer : MonoBehaviour
{
private IEnumerator Start()
{
yield return new WaitForSeconds(2f);
// 检查XR管理器是否存在
if (XRGeneralSettings.Instance != null)
{
// 获取XR管理器
var xrManagerSettings = XRGeneralSettings.Instance.Manager;
// 如果XR管理器没有启动,就启动它
if (xrManagerSettings != null && !xrManagerSettings.isInitializationComplete)
{
Debug.Log("Initializing XR...");
xrManagerSettings.InitializeLoaderSync();
if (xrManagerSettings.activeLoader == null)
{
Debug.LogError("Failed to initialize XR loader.");
}
else
{
Debug.Log("XR loader initialized successfully.");
xrManagerSettings.StartSubsystems();
}
}
}
else
{
Debug.LogError("XRGeneralSettings instance is null.");
}
yield return null;
}
private void OnDisable()
{
// 停止XR子系统
var xrManagerSettings = XRGeneralSettings.Instance.Manager;
if (xrManagerSettings != null && xrManagerSettings.isInitializationComplete)
{
xrManagerSettings.StopSubsystems();
xrManagerSettings.DeinitializeLoader();
}
}
}
将脚本挂载到场景中即可(注意:在OnDisable中停止了XR,所以最好挂载到不会被销毁的对象身上,或者自行更改脚本)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?