目录
在2019.2后的Unity可使用官方加入的停止启动动画的API
unityAPI 官方代码文献
立即停止渲染 SplashScreen。
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Rendering;
-
- // This example shows how the Splash Screen can be canceled early via the user pressing any key.
- public class Example : MonoBehaviour
- {
- public SplashScreen.StopBehavior stopBehavior;
-
- void Start()
- {
- StartCoroutine(SplashScreenController());
- }
-
- IEnumerator SplashScreenController()
- {
- SplashScreen.Begin();
- while (!SplashScreen.isFinished)
- {
- // Fade to the background if the user presses any key during the Splash Screen rendering.
- if (Input.anyKeyDown)
- {
- SplashScreen.Stop(stopBehavior);
- break;
- }
- yield return null;
- }
- }
- }
当运行时启动并加载第一个场景时,使用此属性获取回调。
使用RuntimeInitializeLoadType的各种选项来控制在启动序列中何时调用该方法。
下面的列表显示了RuntimeInitializeLoadType回调的执行顺序:首先初始化各种低级系统(窗口,程序集,gfx等),然后调用子系统注册和AfterAssembliesLoaded回调。更多的设置(输入系统等),然后beforeplashscreen回调被调用。现在第一个场景开始加载。然后调用beforecenload回调。这里场景的对象被加载,但是Awake()还没有被调用。这里所有对象都被认为是非活动的。现在Awake()和OnEnable()在monobehaviour上被调用。然后调用AfterSceneLoad回调。这里的场景对象被认为是完全加载和设置的。活动对象可以通过FindObjectsByType找到。以上细节是在启动玩家构建时。当在编辑器中进入播放模式时,确保相同的调用。
默认的回调调用时间是RuntimeInitializeLoadType.AfterSceneLoad。每个RuntimeInitializeLoadType回调中的执行顺序不能得到保证。
- // Demonstration of the RuntimeInitializeOnLoadMethod attribute
- using UnityEngine;
-
- class MyClass
- {
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
- static void OnBeforeSplashScreen()
- {
- Debug.Log("Before SplashScreen is shown and before the first scene is loaded.");
- }
-
- [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.");
- }
- }
RuntimeInitializeOnLoadMethod 标 记方法,传入 RuntimeInitializeLoadType.BeforeSplashScreen 可在场景初始化前的动画加载前进行相关操作。- //停止启动LOGO动画的播放
- SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate)
注意:这个动画是个同步过程,并且不会因为加载完成而自动关闭,动画本身会浪费1~2秒
- using UnityEngine;
- using UnityEngine.Rendering;
-
- public class SkipLogo
- {
- // 指定此方法在加载时立即调用,在显示启动动画前之前执行
- [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.BeforeSplashScreen)] 后会自动在动画启动前执行
亲测Android和PC平台有效
在WebGL平台中Unity的画面是通过 createUnityInstance 渲染到canvs上的 并且这个方法有个回调,回调的参数就是加载的状态 从0到1 如果为1就是资源加载完成,所以可以很简单的在 canvs 上盖一个自己加载动画的图片或者 div 在加载回调中判断状态为1的时候直接隐藏盖在 canvs 上面的画面即可。
要找到对应的提示代码 从而在上面进行修改 关键代码
- createUnityInstance(document.querySelector("#unity-canvas"),{
- dataUrl:"XXXXXXXXXXXXXXXX",
- frameworkUrl:"XXXXXXXXXXXXXXXX",
- codeUrl:"XXXXXXXXXXXXXXXX",
- streamingAssetsUrl:"XXXXXXXXXXXXXXXX",
- companyName:"XXXXXXXXXXXXXXXX",
- productName:"XXXXXXXXXXXXXXXX",
- productVersion:"XXXXXXXXXXXXXXXX",
- },(e)=>{
- if(e<1)
- {
- loadingGIF.style.display = "xxxxx"
- }
- else
- {
- loadingGIF.style.display = "none"
- }
-
- }).then(res=>{
- unityGameobj = res
- })