当前位置:   article > 正文

unity webgl自定义启动进度条动画_u3dwebgl 加载动画

u3dwebgl 加载动画

多多少少在unity官网webgl模板看到了Template自定义介绍,今天分享一个超级实用的

添加自定义的模板有两种方式:

1.在你-----unity安装位置\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates

在这目录下创建自定义模板

2.直接在unity的 Assets下创建WebGLTemplates文件夹 再到里面创建自己的文件(必须这个文件夹下名字不能打错)

然后playersettings->Resolution and presentation->webgl Template 就可以看到你自定义的模板了

在这文件下创建一个index.html文件和一张你需要的logo图片

直接看h5代码:

unity自带的:

  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Unity WebGL Player | %UNITY_WEB_NAME%</title>
  7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
  8. <link rel="stylesheet" href="TemplateData/style.css">
  9. <script src="TemplateData/UnityProgress.js"></script>
  10. <script src="%UNITY_WEBGL_LOADER_URL%"></script>
  11. <script>
  12. var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress});
  13. </script>
  14. </head>
  15. <body>
  16. <div class="webgl-content">
  17. <div id="gameContainer" style="width: %UNITY_WIDTH%px; height: %UNITY_HEIGHT%px"></div>
  18. <div class="footer">
  19. <div class="webgl-logo"></div>
  20. <div class="fullscreen" onclick="gameInstance.SetFullscreen(1)"></div>
  21. <div class="title">%UNITY_WEB_NAME%</div>
  22. </div>
  23. </div>
  24. </body>
  25. </html>

自定的:

  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>%UNITY_WEB_NAME%</title>
  6. <style>
  7. html {
  8. box-sizing: border-box;
  9. }
  10. *, *:before, *:after {
  11. box-sizing: inherit;
  12. }
  13. body {
  14. margin: 0;
  15. background: #444;
  16. }
  17. #gameContainer {
  18. width: 100vw;
  19. height: 100vh;
  20. }
  21. canvas {
  22. width: 100%;
  23. height: 100%;
  24. display: block;
  25. }
  26. .logo {
  27. display: block;
  28. width: max-width: 80vw;
  29. height: max-height: 60vh;
  30. }
  31. .progress {
  32. margin: 1.5em;
  33. border: 1px solid white;
  34. width: 50vw;
  35. display: none;
  36. }
  37. .progress .full {
  38. margin: 2px;
  39. background: white;
  40. height: 1em;
  41. transform-origin: top left;
  42. }
  43. #loader {
  44. position: absolute;
  45. left: 0;
  46. top: 0;
  47. width: 100vw;
  48. height: 100vh;
  49. display: flex;
  50. flex-direction: column;
  51. align-items: center;
  52. justify-content: center;
  53. }
  54. .spinner,
  55. .spinner:after {
  56. border-radius: 50%;
  57. width: 5em;
  58. height: 5em;
  59. }
  60. .spinner {
  61. margin: 10px;
  62. font-size: 10px;
  63. position: relative;
  64. text-indent: -9999em;
  65. border-top: 1.1em solid rgba(255, 255, 255, 0.2);
  66. border-right: 1.1em solid rgba(255, 255, 255, 0.2);
  67. border-bottom: 1.1em solid rgba(255, 255, 255, 0.2);
  68. border-left: 1.1em solid #ffffff;
  69. transform: translateZ(0);
  70. animation: spinner-spin 1.1s infinite linear;
  71. }
  72. @keyframes spinner-spin {
  73. 0% {
  74. transform: rotate(0deg);
  75. }
  76. 100% {
  77. transform: rotate(360deg);
  78. }
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div id="gameContainer"></div>
  84. <div id="loader">
  85. <img class="logo" src="logo.png">
  86. <div class="spinner"></div>
  87. <div class="progress"><div class="full"></div></div>
  88. </div>
  89. </body>
  90. <script src="%UNITY_WEBGL_LOADER_URL%"></script>
  91. <script>
  92. var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress});
  93. function UnityProgress(gameInstance, progress) {
  94. if (!gameInstance.Module) {
  95. return;
  96. }
  97. const loader = document.querySelector("#loader");
  98. if (!gameInstance.progress) {
  99. const progress = document.querySelector("#loader .progress");
  100. progress.style.display = "block";
  101. gameInstance.progress = progress.querySelector(".full");
  102. loader.querySelector(".spinner").style.display = "none";
  103. }
  104. gameInstance.progress.style.transform = `scaleX(${progress})`;
  105. if (progress === 1 && !gameInstance.removeTimeout) {
  106. gameInstance.removeTimeout = setTimeout(function() {
  107. loader.style.display = "none";
  108. }, 2000);
  109. }
  110. }
  111. </script>
  112. </html>

看效果:

对应默认模板里的h5代码来修改的, 对比可看出我还自适应屏幕了,好了拿去试试吧

  • %UNITY_WEB_NAME% - 在unity设置下定义的产品名称
  • %UNITY_HEIGHT% - 来自WebGL分辨率和unity设置中的演示的高度
  • %UNITY_WIDTH% - 来自WebGL分辨率和unity设置中的演示文稿的宽度
  • %UNITY_WEBGL_LOADER_GLUE% - 加载构建的代码,通常在正文结束标记之前。
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号