Version: 2023.2
언어: 한국어
Unity 웹의 메모리
웹 그래픽스

Cache behavior in Web

In the Unity Web platform, the Cache API lets you store the asset data cached in .data files and AssetBundles within the browser cache. Storage limits for the browser cache such as maximum file size, maximum overall cache size, and eviction criteria are dependent on the browser and platform that you’re using. For more information, see Browser storage limits and eviction criteria.

데이터 캐싱

To access Data Caching, open the Publishing Setings for Web from File > Build Settings > Player Settings. This enables the browser to cache the main data files into the IndexedDB database.

기본 브라우저 HTTP 캐시를 사용한다고 해서 브라우저가 특정 리스폰스를 캐싱한다는 보장은 없습니다.이는 브라우저 HTTP 캐시의 공간이 제한되어 있어 브라우저가 너무 큰 파일을 캐싱할 수 없을 수도 있기 때문입니다.

로드 속드를 개선하기 위해 IndexedDB를 사용하면 브라우저 제한을 초과하는 파일을 캐싱할 수 있습니다.더 많은 파일을 캐싱하면 다음 빌드를 실행할 때 다운로드한 콘텐츠를 사용자의 컴퓨터에서 사용할 수 있는 가능성이 높아집니다.

데이터 캐싱은 HTTP 리스폰스에 대해 IndexedDB 캐시에 있는 .data 파일만 캐싱합니다.에셋 번들을 캐싱하려면 데이터 캐싱을 활성화하고 unityInstance.Module.cacheControl()을 오버라이드해야 합니다. 이렇게 하려면 Module.cacheControl(url)이 요청한 에셋 번들 URL에 대해 must-revalidate를 반환하는지 확인해야 합니다.예를 들어 createUnityInstance()가 반환하는 Promise의 이행 콜백에서 unityInstance.Module.cacheControl()함수를 오버라이드할 수 있습니다. createUnityInstance()에 대한 자세한 내용은 압축 빌드 및 서버 설정을 참조하십시오.

Customize Web Cache behavior

By default, the Web Cache stores the asset data file .data and AssetBundle files .bundle, and revalidates them before loading them from the cache. You can change this behavior by adding a new Web Template that changes the UnityLoader configuration.

다음 예시는 커스텀 cacheControl 함수를 index.html 파일 내의 UnityLoader 설정에 추가하는 것을 보여줍니다.

var config = {
   // ...
#if USE_DATA_CACHING
   cacheControl: function (url) {
     // Caching enabled for .data and .bundle files.
     // Revalidate if file is up to date before loading from cache
     if (url.match(/\.data/) || url.match(/\.bundle/)) {
         return "must-revalidate";
     }

     // Caching enabled for .mp4 and .custom files
     // Load file from cache without revalidation.
     if (url.match(/\.mp4/) || url.match(/\.custom/)) {
         return "immutable";
     }

     // Disable explicit caching for all other files.
     // Note: the default browser cache may cache them anyway.
     return "no-store";
   },
#endif
   // ...
}

cacheControl 함수는 요청한 URL을 파라미터로 받아 다음 중 하나를 반환합니다.

  • must-revalidate - 함수가 must-revalidate를 반환하면 캐시가 활성화된 상태로 돌아가고 캐시에서 파일을 로드하기 전에 파일을 다시 확인합니다.

  • immutable - 함수가 immutable을 반환하면 캐시가 활성화되고 파일을 다시 확인하지 않고 캐시에서 로드합니다.

  • no-store - 함수가 no-store를 반환하면 캐시가 비활성화됩니다.

The browser automatically stores (caches) certain file types such as .html, .js, .css, .json, .jpg, .png, so they don’t need to be explicitly stored in the Web Cache. Typical candidates for the Web cache include large files and files that use a custom file format.

Unity 웹의 메모리
웹 그래픽스