• Unity 合理跳过启动LOGO动画 多平台官方API


    目录

    关键说明

    Android和PC平台

    原理

    完整脚本

    WebGL

    原理

    代码修改参考


    关键说明

    在2019.2后的Unity可使用官方加入的停止启动动画的API

    unityAPI 官方代码文献

    立即停止渲染 SplashScreen。

    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4. // This example shows how the Splash Screen can be canceled early via the user pressing any key.
    5. public class Example : MonoBehaviour
    6. {
    7. public SplashScreen.StopBehavior stopBehavior;
    8. void Start()
    9. {
    10. StartCoroutine(SplashScreenController());
    11. }
    12. IEnumerator SplashScreenController()
    13. {
    14. SplashScreen.Begin();
    15. while (!SplashScreen.isFinished)
    16. {
    17. // Fade to the background if the user presses any key during the Splash Screen rendering.
    18. if (Input.anyKeyDown)
    19. {
    20. SplashScreen.Stop(stopBehavior);
    21. break;
    22. }
    23. yield return null;
    24. }
    25. }
    26. }

    当运行时启动并加载第一个场景时,使用此属性获取回调。

    使用RuntimeInitializeLoadType的各种选项来控制在启动序列中何时调用该方法。

    下面的列表显示了RuntimeInitializeLoadType回调的执行顺序:首先初始化各种低级系统(窗口,程序集,gfx等),然后调用子系统注册和AfterAssembliesLoaded回调。更多的设置(输入系统等),然后beforeplashscreen回调被调用。现在第一个场景开始加载。然后调用beforecenload回调。这里场景的对象被加载,但是Awake()还没有被调用。这里所有对象都被认为是非活动的。现在Awake()和OnEnable()在monobehaviour上被调用。然后调用AfterSceneLoad回调。这里的场景对象被认为是完全加载和设置的。活动对象可以通过FindObjectsByType找到。以上细节是在启动玩家构建时。当在编辑器中进入播放模式时,确保相同的调用。

    默认的回调调用时间是RuntimeInitializeLoadType.AfterSceneLoad。每个RuntimeInitializeLoadType回调中的执行顺序不能得到保证。

    1. // Demonstration of the RuntimeInitializeOnLoadMethod attribute
    2. using UnityEngine;
    3. class MyClass
    4. {
    5. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
    6. static void OnBeforeSplashScreen()
    7. {
    8. Debug.Log("Before SplashScreen is shown and before the first scene is loaded.");
    9. }
    10. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    11. static void OnBeforeSceneLoad()
    12. {
    13. Debug.Log("First scene loading: Before Awake is called.");
    14. }
    15. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    16. static void OnAfterSceneLoad()
    17. {
    18. Debug.Log("First scene loaded: After Awake is called.");
    19. }
    20. [RuntimeInitializeOnLoadMethod]
    21. static void OnRuntimeInitialized()
    22. {
    23. Debug.Log("Runtime initialized: First scene loaded: After Awake is called.");
    24. }
    25. }

    Android和PC平台

    原理

    1. 使用  RuntimeInitializeOnLoadMethod 标 记方法,传入  RuntimeInitializeLoadType.BeforeSplashScreen 可在场景初始化前的动画加载前进行相关操作。
    2. 然后调用
    1. //停止启动LOGO动画的播放
    2. SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate)

     注意:这个动画是个同步过程,并且不会因为加载完成而自动关闭,动画本身会浪费1~2秒

    完整脚本

    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. public class SkipLogo
    4. {
    5. // 指定此方法在加载时立即调用,在显示启动动画前之前执行
    6. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
    7. private static void BeforeSplashScreen()
    8. {
    9. //平台适配
    10. #if UNITY_WEBGL
    11. // 如果在 WebGL 平台上运行,立即停止启动动画
    12. SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
    13. #else
    14. // 如果不是在 WebGL 平台上,启动一个异步任务来停止启动动画
    15. System.Threading.Tasks.Task.Run(()=>
    16. {
    17. SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
    18. });
    19. #endif
    20. }
    21. }

    注意:不用挂载到物体上,直接创建脚本,方法加上[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)] 后会自动在动画启动前执行

    亲测Android和PC平台有效

    WebGL

    原理

    在WebGL平台中Unity的画面是通过 createUnityInstance 渲染到canvs上的 并且这个方法有个回调,回调的参数就是加载的状态 从0到1 如果为1就是资源加载完成,所以可以很简单的在 canvs 上盖一个自己加载动画的图片或者 div 在加载回调中判断状态为1的时候直接隐藏盖在 canvs 上面的画面即可。

    要找到对应的提示代码 从而在上面进行修改 关键代码

    代码修改参考

    1. createUnityInstance(document.querySelector("#unity-canvas"),{
    2. dataUrl:"XXXXXXXXXXXXXXXX",
    3. frameworkUrl:"XXXXXXXXXXXXXXXX",
    4. codeUrl:"XXXXXXXXXXXXXXXX",
    5. streamingAssetsUrl:"XXXXXXXXXXXXXXXX",
    6. companyName:"XXXXXXXXXXXXXXXX",
    7. productName:"XXXXXXXXXXXXXXXX",
    8. productVersion:"XXXXXXXXXXXXXXXX",
    9. },(e)=>{
    10. if(e<1)
    11. {
    12. loadingGIF.style.display = "xxxxx"
    13. }
    14. else
    15. {
    16. loadingGIF.style.display = "none"
    17. }
    18. }).then(res=>{
    19. unityGameobj = res
    20. })

  • 相关阅读:
    使用Docker Compose运行Elasticsearch
    常见的LED显示屏拼接优缺点解析
    【Vue】
    第29课 绘制原理图——放置电源端口
    如何在Linux快速搭建一套ADB环境
    学习记忆——宫殿篇——记忆宫殿——记忆桩——火车+外院+客厅+卧室
    【docker desktop】MongoDB配置并用NodeJS连接
    有依次对应关系的数组X、Y、Z,如何排序其中一个X数组,使得另外的数组还与排序完成后的数组相对应(C语言实现)
    Linux crontabs定时执行任务
    经典设计原则 - SOLID
  • 原文地址:https://blog.csdn.net/q1295006114/article/details/139622320