에셋 번들
에셋 번들을 위한 에셋 준비

AssetBundle workflow

에셋 번들을 시작하려면 다음 단계를 따르십시오. 각 워크플로 단계에 대한 자세한 내용을 보려면 문서에서 본 섹션의 다른 페이지를 참조하십시오.

에셋 번들에 에셋 할당

해당 에셋을 에셋 번들에 할당하려면 다음 단계를 따르십시오.

  1. Select the Asset you want to assign to a bundle from your Project View.
  2. Examine the object in the Inspector.
  3. At the bottom of the Inspector, there is a section to assign AssetBundles and Variants. Use the left-hand drop down to assign the AssetBundle, and the right-hand drop down to assign the variant.
  4. Click None on the left-hand drop to reveal the currently registered AssetBundle names.
  5. Click New to create a new AssetBundle
  6. Type in the desired AssetBundle name. Note: AssetBundle names support a type of folder structure depending on what you type. To add sub-folders, separate folder names by a /. For example, use the AssetBundle name environment/forest to create a bundle named forest under an environment sub-folder
  7. Once you’ve selected or created an AssetBundle name, you can repeat this process for the right hand drop down to assign or create a Variant name, if you desire. Variant names are not required to build the AssetBundles

To read more information on AssetBundle assignments and accompanying strategies, see documentation on Preparing Assets for AssetBundles.

에셋 번들 빌드

Assets 폴더에서 Editor 폴더를 생성하고, 폴더에 다음과 같은 콘텐츠의 스크립트를 입력합니다.

using UnityEditor;
using System.IO;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/AssetBundles";
        if(!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, 
                                        BuildAssetBundleOptions.None, 
                                        BuildTarget.StandaloneWindows);
    }
}

This script creates a menu item at the bottom of the Assets menu called Build AssetBundles that executes the code in the function associated with that tag. When you click Build AssetBundles a progress bar appears with a build dialog. This takes all the Assets you labeled with an AssetBundle name and places them in a folder at the path assetBundleDirectory defines.

For more details about this, see documentation on Building AssetBundles.

에셋 번들 및 에셋 로딩

If you want to load from local storage, use the AssetBundles.LoadFromFile API, which looks like this:

public class LoadFromFileExample : MonoBehaviour {
    function Start() {
        var myLoadedAssetBundle 
            = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
        if (myLoadedAssetBundle == null) {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("MyObject");
        Instantiate(prefab);
    }
}

LoadFromFile은 번들 파일의 경로를 가져옵니다.

If you’re hosting your AssetBundles yourself and need to download them into your application, use the UnityWebRequest API. Here’s an example:

IEnumerator InstantiateObject()
{
    string url = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;        
    UnityEngine.Networking.UnityWebRequest request 
        = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(url, 0);
    yield return request.Send();
    AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
    GameObject cube = bundle.LoadAsset<GameObject>("Cube");
    GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
    Instantiate(cube);
    Instantiate(sprite);
}

GetAssetBundle(string, int) takes the URL of the location of the AssetBundle and the version of the bundle you want to download. This example still points to a local file but string url could point to any URL you have your AssetBundles hosted at.

UnityWebRequest에는 요청 시 에셋 번들을 가져오는 에셋 번들인 DownloadHandlerAssetBundle을 처리하는 특별한 방법이 있습니다.

Regardless of the method you use, you now have access to the AssetBundle object. From that object you need to use LoadAsset<T>(string) which takes the type, T, of the asset you’re attempting to load and the name of the object as a string that’s inside the bundle. This returns whatever object you’re loading from the AssetBundle. You can use these returned objects just like any object inside of Unity. For example, if you want to create a GameObject in the scene, you just need to call Instantiate(gameObjectFromAssetBundle).

에셋 번들을 로드하는 API에 대한 자세한 내용을 보려면 에셋 번들의 전문적인 활용을 참조하십시오.


  • 2017–05–15 Page published
에셋 번들
에셋 번들을 위한 에셋 준비