var PROGRESS_BAR_FILL_START_BACKGROUND_X = -619; var PROGRESS_BAR_FILL_END_BACKGROUND_X = -12; var PROGRESS_CLOSE_INTERVAL_TIME_OUT = 200; var PROGRESS_INTERVAL_TIME_OUT = 10; var PROGRESS_SAME_ADD_COUNT = 0.00016; var targetUnityInstance = null; var targetProgress = 0; var countUpProgress = 0; var progressInterval = null; var unityLoaderComplete = false; var unityLoaderInstance = []; // プログレスバーの更新処理開始 progressInterval = setInterval(updateProgress, PROGRESS_INTERVAL_TIME_OUT); function UnityProgress(container, splashScreenStyle, progress) { if (!container) return; if (splashScreenStyle == null) return; if (!unityLoaderInstance.logo) { unityLoaderInstance.logo = document.createElement("div"); unityLoaderInstance.logo.className = "logo " + splashScreenStyle; container.appendChild(unityLoaderInstance.logo); } if (!unityLoaderInstance.progress) { unityLoaderInstance.progress = document.createElement("div"); unityLoaderInstance.progress.className = "progress " + splashScreenStyle; unityLoaderInstance.progress.empty = document.createElement("div"); unityLoaderInstance.progress.empty.className = "empty"; unityLoaderInstance.progress.appendChild(unityLoaderInstance.progress.empty); unityLoaderInstance.progress.full = document.createElement("div"); unityLoaderInstance.progress.full.className = "full"; unityLoaderInstance.progress.appendChild(unityLoaderInstance.progress.full); unityLoaderInstance.progress.fullBase = document.createElement("div"); unityLoaderInstance.progress.fullBase.className = "fullBase"; unityLoaderInstance.progress.appendChild(unityLoaderInstance.progress.fullBase); container.appendChild(unityLoaderInstance.progress); } if (!unityLoaderInstance.progressString) { unityLoaderInstance.progressString = document.createElement("div"); unityLoaderInstance.progressString.className = "progressString " + splashScreenStyle; unityLoaderInstance.progressString.nowLoadingString = document.createElement("div"); unityLoaderInstance.progressString.nowLoadingString.textContent = "Now Loading..."; unityLoaderInstance.progressString.nowLoadingString.className = "nowLoadingString"; unityLoaderInstance.progressString.appendChild(unityLoaderInstance.progressString.nowLoadingString); unityLoaderInstance.progressString.nowLoadingCount = document.createElement("div"); unityLoaderInstance.progressString.nowLoadingCount.className = "nowLoadingCount"; unityLoaderInstance.progressString.appendChild(unityLoaderInstance.progressString.nowLoadingCount); container.appendChild(unityLoaderInstance.progressString); } targetUnityInstance = unityLoaderInstance; targetProgress = progress < targetProgress?targetProgress:progress; // progressが1の場合はUnityLoaderの読み込みが完了した if (progress == 1) { unityLoaderComplete = true; } } /** * プログレスバーを更新する */ function updateProgress() { // 初期化されていないなら抜ける if (targetUnityInstance == null) return; // 同数の場合は徐々にプログレスバーを進ませる if (countUpProgress == targetProgress) { targetProgress += PROGRESS_SAME_ADD_COUNT; } // 100%を超えないための制限 if (1 < targetProgress){ targetProgress = 1; } // 実際に読み込まれた進捗よりも進んでいないならプログレスバーを進ませる if (countUpProgress < targetProgress) { countUpProgress += Math.floor(Math.random()*1000)/200000; if (targetProgress < countUpProgress) countUpProgress = targetProgress; } // 読み込みが完了しているなら100%を一瞬見せるための if (unityLoaderComplete){ countUpProgress = targetProgress; clearInterval(progressInterval); progressInterval = null; setTimeout(closeProgress, PROGRESS_CLOSE_INTERVAL_TIME_OUT); } // プログレスバー更新 var progressRatio = (100 * countUpProgress); targetUnityInstance.progress.full.style.backgroundPositionX = ( PROGRESS_BAR_FILL_START_BACKGROUND_X + PROGRESS_BAR_FILL_END_BACKGROUND_X ) - ((progressRatio * ( PROGRESS_BAR_FILL_START_BACKGROUND_X - PROGRESS_BAR_FILL_END_BACKGROUND_X ) / 100) + PROGRESS_BAR_FILL_END_BACKGROUND_X ) + "px"; // 進捗率更新 targetUnityInstance.progressString.nowLoadingCount.textContent =progressRatio.toFixed(2) + "%"; } /** * UnityLoaderのプログレスバー画面を閉じる */ function closeProgress() { targetUnityInstance.logo.style.display = unityLoaderInstance.progress.style.display = unityLoaderInstance.progressString.style.display = "none"; countUpProgress = 0; targetProgress = 0; targetUnityInstance = null; }