반응형

에셋 번들에서 오브젝트를 로드, 언로드

다운로드 한 데이터에서 만든 에셋 번들에서 오브젝트가 있으면 세 가지 방법으로 오브젝트를 로드할 수 있습니다 :

  • [AssetBundle.LoadAsset](ScriptRef : AssetBundle.LoadAsset.html)에서 이름 식별자를 파라미터로 사용하여 오브젝트를 로드합니다. 이름은 프로젝트 뷰에 표시되는 것입니다. 선택적으로 오브젝트 타입을 파라미터로 Load 메소드에 전달하여 로드된 오브젝트가 특정 타입인지 여부를 확인할 수 있습니다.

  • [AssetBundle.LoadAssetAsync](ScriptRef : AssetBundle.LoadAssetAsync.html)는 위의 Load 메소드처럼 작동하지만, 에셋이 로드될 때 메인 스레드를 차단하지 않습니다. 따라서 이 메소드는 큰 에셋이나 여러 에셋을 동시에 로드할 때 응용 프로그램이 정지하는 것을 피하는 데에 유용합니다.

  • [AssetBundle.LoadAllAssets](ScriptRef : AssetBundle.LoadAllAssets.html)에 의해 에셋 번들에 포함된 모든 오브젝트를 로드합니다. AssetBundle.LoadAsset과 마찬가지로 오브젝트를 타입에 따라 필터링 할 수 있습니다.

에셋을 언로드하려면 [AssetBundle.Unload](ScriptRef : AssetBundle.Unload.html)를 사용해야 합니다. 이 메소드는 boolean 인수로 Unity에서 모든 데이터를 언로드 할 지(로드된 에셋의 오브젝트를 포함), 다운로드 한 번들에서 압축된 데이터만 언로드 할 지 여부를 지정합니다. 만약 응용프로그램에서 에셋 번들의 어떤 오브젝트를 사용하고 있고, 메모리를 확보하려는 경우, False를 전달하여 메모리에서 압축된 데이터를 언로드 할 수 있습니다. 만약 에셋 번들에서 완전히 모두를 언로드하려면 True를 전달하여 에셋 번들에서 로드된 에셋을 삭제합니다.

에셋 번들로부터 오브젝트를 비동기 로드

[AssetBundle.LoadAssetAsync](ScriptRef : AssetBundle.LoadAssetAsync.html) 메소드를 사용하여 오브젝트를 비동기 로드함으로써 응용 프로그램에 끊기는 현상(hiccup)이 생길 가능성을 줄입니다.

using UnityEngine;

// Note: This example does not check for errors. Please look at the example in the DownloadingAssetBundles section for more information
IEnumerator Start () {
    while (!Caching.ready)
        yield return null;
    // Start a download of the given URL
    WWW www = WWW.LoadFromCacheOrDownload (url, 1);

    // Wait for download to complete
    yield return www;

    // Load and retrieve the AssetBundle
    AssetBundle bundle = www.assetBundle;

    // Load the object asynchronously
    AssetBundleRequest request = bundle.LoadAssetAsync ("myObject", typeof(GameObject));

    // Wait for completion
    yield return request;

    // Get the reference to the loaded object
    GameObject obj = request.asset as GameObject;

        // Unload the AssetBundles compressed contents to conserve memory
        bundle.Unload(false);

        // Frees the memory from the web stream
        www.Dispose();
}







인터넷에 아래처럼 에셋번들 빌드하는게 돌아 다니는데 이걸로 하면 최근 버전에선 사용되지 않는 옵션들로 변경되어 있다

BuildAssetBundleOptions.CollectDependencies |  BuildAssetBundleOptions.CompleteAssets |


이거 대신에 


BuildAssetBundleOptions.None 을 쓰면 됨



BuildPipeline.BuildAssetBundles("저장위치", BuildAssetBundleOptions.None, BuildTarget.Android);



반응형

+ Recent posts