Unity WebGL 跳过启动LOGO动画
发布时间:2025-11-30 01:43:40 浏览量:27
在 Assets 的 Scripts 目录下创建一个代码文件 SplashScreenManager,其内容如下:
using System.Collections;using UnityEngine;using UnityEngine.Rendering;public class SplashScreenManager : MonoBehaviour{public SplashScreen.StopBehavior stopBehavior;void Start{StartCoroutine(SplashScreenController);}IEnumerator SplashScreenController{SplashScreen.Begin;while (!SplashScreen.isFinished){if (Input.anyKeyDown){SplashScreen.Stop(stopBehavior);break;}yield return null;}}// 指定此方法在加载时立即调用,在显示启动动画前之前执行[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]private static void BeforeSplashScreen{//平台适配#if UNITY_WEBGL// 如果在 WebGL 平台上运行,立即停止启动动画SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);#else// 如果不是在 WebGL 平台上,启动一个异步任务来停止启动动画System.Threading.Tasks.Task.Run( =>{SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);});#endif}[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]static void OnBeforeSceneLoad{Debug.Log("First scene loading: Before Awake is called.");}[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]static void OnAfterSceneLoad{Debug.Log("First scene loaded: After Awake is called.");}[RuntimeInitializeOnLoadMethod]static void OnRuntimeInitialized{Debug.Log("Runtime initialized: First scene loaded: After Awake is called.");}}无需挂载,打包即可。
