• cocos creator 3.6 发布web手机端 加载进度条添加


    cocos creator 升级到3.x之后加载进度条取消了,测试了多个3.x版本最终以creator 3.6.3版本,构建了简单的进度加载

    参考链接:

    https://forum.cocos.org/t/topic/137113

    打包web-mobile后,没有进度条。加载的时候只显示一个黑屏。 - Creator 3.x - Cocos中文社区

    cocos creator 3.6.x 版本H5下 启动页做进度条加载 - 掘金

     参考了多个实例,大部分之解决到了第一个场景的加载进度,但creater 中cc.js加载时还是会留下空白时间,所以做了个伪进度条,让进度更平滑

    主要对以下文件修改

    设计思路:添加个计时函数,让进度每秒都长,确保每个时间都在增长,然后在一些关键点,插入手动更新,让进度更真实平滑

    //------------------中间是新增加内容-----------------------

    //-------------------end-----------------

    1、 style.css中添加进度条及logo的布局
    1. html {
    2. -ms-touch-action: none;
    3. }
    4. body, canvas, div {
    5. display: block;
    6. outline: none;
    7. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    8. user-select: none;
    9. -moz-user-select: none;
    10. -webkit-user-select: none;
    11. -ms-user-select: none;
    12. -khtml-user-select: none;
    13. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    14. }
    15. /* Remove spin of input type number */
    16. input::-webkit-outer-spin-button,
    17. input::-webkit-inner-spin-button {
    18. /* display: none; <- Crashes Chrome on hover */
    19. -webkit-appearance: none;
    20. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
    21. }
    22. body {
    23. position: absolute;
    24. top: 0;
    25. left: 0;
    26. width: 100%;
    27. height: 100%;
    28. padding: 0;
    29. border: 0;
    30. margin: 0;
    31. cursor: default;
    32. color: #888;
    33. background-color: #333;
    34. text-align: center;
    35. font-family: Helvetica, Verdana, Arial, sans-serif;
    36. display: flex;
    37. flex-direction: column;
    38. }
    39. canvas {
    40. background-color: rgba(0, 0, 0, 0);
    41. }
    42. #GameDiv, #Cocos3dGameContainer, #GameCanvas {
    43. width: 100%;
    44. height: 100%;
    45. }
    46. /******************以下 进度条和logo **********/
    47. .progress-bar {
    48. background-color: #1a1a1a;
    49. position: absolute;
    50. left: 25%;
    51. top: 80%;
    52. height: 14px;
    53. padding: 5px;
    54. width: 50%;
    55. /*margin: 0 -175px; */
    56. border-radius: 5px;
    57. box-shadow: 0 1px 5px #000 inset, 0 1px 0 #444;
    58. }
    59. .progress-bar span {
    60. display: block;
    61. height: 100%;
    62. border-radius: 3px;
    63. box-shadow: 0 1px 0 rgba(255, 255, 255, .5) inset;
    64. transition: width .4s ease-in-out;
    65. background-color: #34c2e3;
    66. }
    67. .stripes span {
    68. background-size: 30px 30px;
    69. background-image: linear-gradient(135deg, rgba(255, 255, 255, .15) 25%, transparent 25%,
    70. transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%,
    71. transparent 75%, transparent);
    72. animation: animate-stripes 1s linear infinite;
    73. }
    74. #splash {
    75. position: absolute;
    76. top: 0;
    77. left: 0;
    78. width: 100%;
    79. height: 100%;
    80. background: #171717 url(./splash.png) no-repeat center;
    81. background-size: 45%;
    82. }
    83. /******************end**********/
    2、 index.html中添加节点,并且启动显示
    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>Cocos Creator | spidertitle>
    6. <meta name="viewport"
    7. content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1,minimal-ui=true"/>
    8. <meta name="apple-mobile-web-app-capable" content="yes">
    9. <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    10. <meta name="format-detection" content="telephone=no">
    11. <meta name="renderer" content="webkit"/>
    12. <meta name="force-rendering" content="webkit"/>
    13. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    14. <meta name="msapplication-tap-highlight" content="no">
    15. <meta name="full-screen" content="yes"/>
    16. <meta name="x5-fullscreen" content="true"/>
    17. <meta name="360-fullscreen" content="true"/>
    18. <meta name="x5-page-mode" content="app">
    19. <link rel="stylesheet" type="text/css" href="style.css"/>
    20. head>
    21. <body>
    22. <div id="GameDiv" cc_exact_fit_screen="true">
    23. <div id="Cocos3dGameContainer">
    24. <canvas id="GameCanvas" oncontextmenu="event.preventDefault()" tabindex="99">canvas>
    25. div>
    26. div>
    27. <div id="splash">
    28. <div class="progress-bar stripes">
    29. <span style="width: 0%">span>
    30. div>
    31. div>
    32. <script src="src/polyfills.bundle.js" charset="utf-8"> script>
    33. <script src="src/system.bundle.js" charset="utf-8"> script>
    34. <script src="src/import-map.json" type="systemjs-importmap" charset="utf-8"> script>
    35. <script>
    36. System.import('./index.js').catch(function(err) { console.error(err); })
    37. script>
    38. <script type="text/javascript">
    39. (function () {
    40. var splash = document.getElementById('splash');
    41. splash.style.display = 'block';
    42. })();
    43. script>
    44. body>
    45. html>
    3、 添加进度条更新onProgress,和自动一个计时更新函数,onProgress手动调用分的越细节,进度曲线越平滑
    1. System.register([], function (_export, _context) {
    2. "use strict";
    3. var cc, Application;
    4. //---------------计时函数 和更新进度函数--------------
    5. var percentJd=0;
    6. //每秒更新进度条
    7. var timer =setInterval(() => {
    8. const now = new Date();
    9. //console.log(now.toString());
    10. if (percentJd<90) {
    11. percentJd=percentJd+1;
    12. onProgress(percentJd);
    13. } else{
    14. clearInterval(timer);
    15. }
    16. }, 1000);
    17. //刷新ui 及进度
    18. function onProgress(percent){
    19. //用于手动跟新,比每秒进度大就更新
    20. if (percent>percentJd) {
    21. percentJd=percent;
    22. }
    23. var progressBar = splash.querySelector('.progress-bar span');
    24. // var percent = 100 * finish / total;
    25. if (progressBar) {
    26. progressBar.style.width = percent + '%';
    27. }
    28. }
    29. //---------------------------end-----------------
    30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
    31. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
    32. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
    33. return {
    34. setters: [],
    35. execute: function () {
    36. _export("Application", Application = /*#__PURE__*/function () {
    37. function Application() {
    38. _classCallCheck(this, Application);
    39. this.settingsPath = 'src/settings.json';
    40. this.showFPS = false;
    41. }
    42. _createClass(Application, [{
    43. key: "init",
    44. value: function init(engine) {
    45. cc = engine;
    46. cc.game.onPostBaseInitDelegate.add(this.onPostInitBase.bind(this));
    47. cc.game.onPostSubsystemInitDelegate.add(this.onPostSystemInit.bind(this));
    48. }
    49. }, {
    50. key: "onPostInitBase",
    51. value: function onPostInitBase() {// cc.settings.overrideSettings('assets', 'server', '');
    52. // do custom logic
    53. }
    54. }, {
    55. key: "onPostSystemInit",
    56. value: function onPostSystemInit() {// do custom logic
    57. }
    58. }, {
    59. key: "start",
    60. value: function start() {、
    61. //---------------手动更新进度条--------------
    62. //上面的其他函数中可以拆分更细致
    63. onProgress(50);,
    64. //game.init,初始化第一个场景,第一个场景比较大的话也会比较耗时,所以定在50%
    65. //----------------------------------------
    66. return cc.game.init({
    67. debugMode: false ? cc.DebugMode.INFO : cc.DebugMode.ERROR,
    68. settingsPath: this.settingsPath,
    69. overrideSettings: {
    70. // assets: {
    71. // preloadBundles: [{ bundle: 'main', version: 'xxx' }],
    72. // }
    73. profiling: {
    74. showFPS: this.showFPS
    75. }
    76. }
    77. }).then(function () {
    78. return cc.game.run();
    79. });
    80. }
    81. }]);
    82. return Application;
    83. }());
    84. }
    85. };
    86. });
    4、关闭进度条和logo的显示的监听
    1. System.register(["./application.js"], function (_export, _context) {
    2. "use strict";
    3. var Application, canvas, $p, bcr, application;
    4. //------注册第一个场景 是否加载完成监听-----------------
    5. function setLoadingDisplay () {
    6. console.log('Engine is initialized');
    7. cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
    8. console.log('game run over' );
    9. //关闭进度条显示
    10. splash.style.display = 'none';
    11. //关闭计时,但有时无效,未找到原因,有好的修改方案告知下
    12. if (application.timer) {
    13. clearInterval(application.timer);
    14. }
    15. });
    16. }
    17. //------ end-----------------
    18. function topLevelImport(url) {
    19. return System["import"](url);
    20. }
    21. return {
    22. setters: [function (_applicationJs) {
    23. Application = _applicationJs.Application;
    24. }],
    25. execute: function () {
    26. canvas = document.getElementById('GameCanvas');
    27. $p = canvas.parentElement;
    28. bcr = $p.getBoundingClientRect();
    29. canvas.width = bcr.width;
    30. canvas.height = bcr.height;
    31. application = new Application();
    32. topLevelImport('cc').then(function (engine) {
    33. //------cc 文件加载完后,添加监听-----------------
    34. setLoadingDisplay();
    35. //------end-----------------
    36. return application.init(engine);
    37. }).then(function () {
    38. return application.start();
    39. })["catch"](function (err) {
    40. console.error(err);
    41. });
    42. }
    43. };
    44. });
    以上就是全部修改内容,但由于每次发布都得修改,所以我将以上内容放入到引擎默认模板中,splash.png放在了项目模板下,就可以勾选md5 也不会更新不到了
    5、修改引擎模板

    路劲:F:\cocos-dashboard-editors\3.6.3\resources\resources\3d\engine\templates\web-mobile

    index.html-》index.ejs 

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title><%= projectName %>title>
    6. <meta name="viewport"
    7. content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1,minimal-ui=true"/>
    8. <meta name="apple-mobile-web-app-capable" content="yes">
    9. <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    10. <meta name="format-detection" content="telephone=no">
    11. <meta name="renderer" content="webkit"/>
    12. <meta name="force-rendering" content="webkit"/>
    13. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    14. <meta name="msapplication-tap-highlight" content="no">
    15. <meta name="full-screen" content="yes"/>
    16. <meta name="x5-fullscreen" content="true"/>
    17. <meta name="360-fullscreen" content="true"/>
    18. <meta name="x5-page-mode" content="app">
    19. <link rel="stylesheet" type="text/css" href="<%= cssUrl %>"/>
    20. head>
    21. <body>
    22. <div id="GameDiv" cc_exact_fit_screen="true">
    23. <div id="Cocos3dGameContainer">
    24. <canvas id="GameCanvas" oncontextmenu="event.preventDefault()" tabindex="99">canvas>
    25. div>
    26. div>
    27. <div id="splash">
    28. <div class="progress-bar stripes">
    29. <span style="width: 0%">span>
    30. div>
    31. div>
    32. <%- include(cocosTemplate, {}) %>
    33. <script type="text/javascript">
    34. (function () {
    35. var splash = document.getElementById('splash');
    36. splash.style.display = 'block';
    37. })();
    38. script>
    39. body>
    40. html>

     index.js-》index.js.ejs

    1. import { Application } from '<%= applicationJS %>';
    2. const canvas = document.getElementById('GameCanvas');
    3. const $p = canvas.parentElement;
    4. const bcr = $p.getBoundingClientRect();
    5. canvas.width = bcr.width;
    6. canvas.height = bcr.height;
    7. // 是否加载执行完成
    8. function setLoadingDisplay () {
    9. console.log('Engine is initialized');
    10. cc.director.once(cc.Director.EVENT_AFTER_SCENE_LAUNCH, function () {
    11. console.log('game run over' );
    12. splash.style.display = 'none';
    13. if (application.timer) {
    14. clearInterval(application.timer);
    15. }
    16. });
    17. }
    18. function topLevelImport (url) {
    19. return System.import(url);
    20. }
    21. const application = new Application();
    22. topLevelImport('cc')
    23. .then((engine) => {
    24. //监听
    25. setLoadingDisplay();
    26. return application.init(engine);
    27. }).then(() => {
    28. return application.start();
    29. }).catch((err) => {
    30. console.error(err);
    31. });

    style.css

    1. html {
    2. -ms-touch-action: none;
    3. }
    4. body, canvas, div {
    5. display: block;
    6. outline: none;
    7. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    8. user-select: none;
    9. -moz-user-select: none;
    10. -webkit-user-select: none;
    11. -ms-user-select: none;
    12. -khtml-user-select: none;
    13. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    14. }
    15. /* Remove spin of input type number */
    16. input::-webkit-outer-spin-button,
    17. input::-webkit-inner-spin-button {
    18. /* display: none; <- Crashes Chrome on hover */
    19. -webkit-appearance: none;
    20. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
    21. }
    22. body {
    23. position: absolute;
    24. top: 0;
    25. left: 0;
    26. width: 100%;
    27. height: 100%;
    28. padding: 0;
    29. border: 0;
    30. margin: 0;
    31. cursor: default;
    32. color: #888;
    33. background-color: #333;
    34. text-align: center;
    35. font-family: Helvetica, Verdana, Arial, sans-serif;
    36. display: flex;
    37. flex-direction: column;
    38. }
    39. canvas {
    40. background-color: rgba(0, 0, 0, 0);
    41. }
    42. #GameDiv, #Cocos3dGameContainer, #GameCanvas {
    43. width: 100%;
    44. height: 100%;
    45. }
    46. /********************进度条********************/
    47. .progress-bar {
    48. background-color: #1a1a1a;
    49. position: absolute;
    50. left: 25%;
    51. top: 80%;
    52. height: 14px;
    53. padding: 5px;
    54. width: 50%;
    55. /*margin: 0 -175px; */
    56. border-radius: 5px;
    57. box-shadow: 0 1px 5px #000 inset, 0 1px 0 #444;
    58. }
    59. .progress-bar span {
    60. display: block;
    61. height: 100%;
    62. border-radius: 3px;
    63. box-shadow: 0 1px 0 rgba(255, 255, 255, .5) inset;
    64. transition: width .4s ease-in-out;
    65. background-color: #34c2e3;
    66. }
    67. .stripes span {
    68. background-size: 30px 30px;
    69. background-image: linear-gradient(135deg, rgba(255, 255, 255, .15) 25%, transparent 25%,
    70. transparent 50%, rgba(255, 255, 255, .15) 50%, rgba(255, 255, 255, .15) 75%,
    71. transparent 75%, transparent);
    72. animation: animate-stripes 1s linear infinite;
    73. }
    74. #splash {
    75. position: absolute;
    76. top: 0;
    77. left: 0;
    78. width: 100%;
    79. height: 100%;
    80. background: #171717 url(./splash.png) no-repeat center;
    81. background-size: 45%;
    82. }

     application.js-》application.ejs

    F:\cocos-dashboard-editors\3.6.3\resources\resources\3d\engine\templates\launcher

    1. <%- include(versionCheckTemplate, { version: '1.0.0'}) %>
    2. let cc;
    3. //---------------自动 updata Progress--------------
    4. var percentJd=0;
    5. var timer =setInterval(() => {
    6. const now = new Date();
    7. //console.log(now.toString());
    8. if (percentJd<90) {
    9. percentJd=percentJd+1;
    10. onProgress(percentJd);
    11. } else{
    12. clearInterval(timer);
    13. }
    14. }, 1000);
    15. function onProgress(percent){
    16. if (percent>percentJd) {
    17. percentJd=percent;
    18. }
    19. var progressBar = splash.querySelector('.progress-bar span');
    20. // var percent = 100 * finish / total;
    21. if (progressBar) {
    22. progressBar.style.width = percent + '%';
    23. }
    24. }
    25. //------------end-----------------
    26. export class Application {
    27. constructor () {
    28. this.settingsPath = '<%= settingsJsonPath %>';
    29. this.showFPS = <%= showFPS %>;
    30. }
    31. init (engine) {
    32. cc = engine;
    33. cc.game.onPostBaseInitDelegate.add(this.onPostInitBase.bind(this));
    34. cc.game.onPostSubsystemInitDelegate.add(this.onPostSystemInit.bind(this));
    35. }
    36. onPostInitBase () {
    37. // cc.settings.overrideSettings('assets', 'server', '');
    38. // do custom logic
    39. }
    40. onPostSystemInit () {
    41. // do custom logic
    42. }
    43. start () {
    44. // 手动更新
    45. onProgress(50);
    46. return cc.game.init({
    47. debugMode: <%= debugMode %> ? cc.DebugMode.INFO : cc.DebugMode.ERROR,
    48. settingsPath: this.settingsPath,
    49. overrideSettings: {
    50. // assets: {
    51. // preloadBundles: [{ bundle: 'main', version: 'xxx' }],
    52. // }
    53. profiling: {
    54. showFPS: this.showFPS,
    55. }
    56. }
    57. }).then(() => cc.game.run());
    58. }
    59. }

  • 相关阅读:
    外卖好评回复模板
    CST仿真软件数据后处理--S参数
    Java面试题-Java核心基础-第八天(异常)
    【Git总结大全】git操作从入门到实战(总结篇)
    应对气候、经济双重影响,此‘链路’非彼链路
    文件上传绕过复现
    # NUSTCTF(校外赛道)2022-wp
    echarts封装 - 1
    Visual Studio 调试 F5断点执行
    RPC - gRPC简单的demo - 学习/实践
  • 原文地址:https://blog.csdn.net/weixin_41843959/article/details/138012457