前言
- 众所周知,使用Unity引擎打包的工程在启动时都带有Unity的默认启动Logo。
- 这个问题可以通过购买Unity专业版以及零元购解决,但是对于多数人来说一般不会使用这种方法。
- 之前已经写过一篇文章使用aar的方式从安卓端去掉Unity的启动Logo:
- 【Unity终极奥义】Unity打包去掉启动画面Logo,无需破解,一学就会
- 那本篇文章就来使用一种更简单的方法来直接去掉启动Logo,只需要一个脚本即可完成。
【Unity实战篇 】 | 一个步骤跳过 Unity Logo 界面 | 多平台适用 | 官方API支持
使用方法
在Unity工程中创建一个脚本SkipSplash.cs
,然后将该脚本放到除了Editor
以外的文件夹就可以了,不需要挂载。
脚本代码如下:
#if !UNITY_EDITOR
using UnityEngine;
using UnityEngine.Rendering;
public class SkipSplash
{
[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
核心 API
该脚本主要用到了两个API: RuntimeInitializeOnLoadMethodAttribute
与SplashScreen
。
- API:RuntimeInitializeOnLoadMethodAttribute
- API:SplashScreen
1. RuntimeInitializeOnLoadMethodAttribute
RuntimeInitializeOnLoadMethodAttribute
一般会配合RuntimeInitializeLoadType
进行使用。
RuntimeInitializeLoadType 有以下类型:
类型 | 介绍 |
---|---|
AfterSceneLoad | 在场景加载后 |
BeforeSceneLoad | 在场景加载前 |
AfterAssembliesLoaded | 加载完所有程序集并初始化预加载资源时的回调 |
BeforeSplashScreen | 在显示启动画面之前 |
SubsystemRegistration | 用于子系统注册的回调 |
在之前写过的一篇小知识文章中用到过这个RuntimeInitializeOnLoadMethodAttribute:
RuntimeInitializeOnLoadMethodAttribute
主要负责的是在显示启动画面之前调用这个静态方法,也就是执行跳过Logo方法的时间。
2. SplashScreen
SplashScreen
是负责跳过Logo的核心方法,与上面的RuntimeInitializeOnLoadMethodAttribute进行配合,在在显示启动画面之前停止 SplashScreen 渲染即可完成Unity启动Logo的去除!
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate)
可以看到非常简单的就实现了去掉Unity的启动Logo,只需要一个脚本放到工程中就好了,限制是需要 Unity2019.4 或更高版本。
实测了Unity2021、2020及2019.4版本发现都可以正常使用。
优点:
该方法非常简单方便,一个脚本可以实现多平台去掉启动Logo。
缺点:
当工程比较大时,此方法去除Logo的效果可能会很差,可能会出现Logo一闪而过或者卡出几帧Logo的画面。
还可能会出现长达4、5秒的黑屏时间,这是因为应用程序正在加载,即使我们停止了Logo,但是并不能影响这个加载的流程时间。
这个时候跳过启动Logo的意义就不大了,正确的方法应该是在此空挡时间换成自己的启动画面,这样就需要另外写方法进行操作了。
暂无评论内容