에셋 번들을 시작하려면 다음 단계를 따르십시오. 각 워크플로 단계에 대한 자세한 내용을 보려면 문서에서 본 섹션의 다른 페이지를 참조하십시오.
해당 에셋을 에셋 번들에 할당하려면 다음 단계를 따르십시오.
/
를 이용해 폴더 이름을 구분합니다. 예를 들어 에셋 번들 이름을 environment/forest
라고 명명한 경우 environment
하위 폴더에 forest
라는 이름의 번들이 생성됩니다.에셋 번들 할당에 대한 자세한 내용 및 관련 정보는 에셋 번들용 에셋 준비 문서를 참조하십시오.
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);
}
}
이 스크립트는 해당 태그와 관련된 함수에서 코드를 실행하는 Build AssetBundles 라는 에셋 메뉴의 하단에 메뉴 아이템을 생성합니다. Build AssetBundles 를 클릭하면 빌드 다이얼로그와 함께 진행 표시줄이 표시됩니다. 이렇게 하면 에셋 번들 이름으로 레이블이 지정된 모든 에셋을 가져와서 assetBundleDirectory
에 정의된 경로의 폴더에 배치합니다.
이에 관한 자세한 내용은 에셋 번들 빌드 문서를 참조하십시오.
로컬 스토리지에서 로드하려는 경우 다음과 같은 AssetBundles.LoadFromFile
API를 사용하십시오.
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
은 번들 파일의 경로를 가져옵니다.
에셋 번들을 직접 호스팅하고 애플리케이션에 다운로드해야 하는 경우에는 UnityWebRequest
API를 사용하십시오. 다음은 그 예제입니다.
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)
은 에셋 번들의 위치와 다운로드하려는 번들 버전의 URL을 가져옵니다. 이 예제에서는 여전히 로컬 파일을 가리키고 있지만, string url
은 에셋 번들을 호스팅하고 있는 URL을 가리킬 수 있습니다.
UnityWebRequest에는 요청 시 에셋 번들을 가져오는 에셋 번들인 DownloadHandlerAssetBundle
을 처리하는 특별한 방법이 있습니다.
사용하는 메서드에 관계없이 이제 에셋 번들 오브젝트에 액세스할 수 있습니다. 오브젝트에서 LoadAsset<T> (string)
을 사용하면 로드하려는 에셋의 타입 T
와 오브젝트의 이름을 번들 안에 있는 문자열로 가져오게 됩니다. 이렇게 되면 에셋 번들에서 로드하는 오브젝트는 모두 반환됩니다. 이 반환된 오브젝트는 Unity의 모든 오브젝트와 마찬가지로 사용할 수 있습니다. 예를 들어, 씬에서 게임 오브젝트를 만들려는 경우 Instantiate(gameObjectFromAssetBundle)
를 호출하면 됩니다.
에셋 번들을 로드하는 API에 대한 자세한 내용을 보려면 에셋 번들의 전문적인 활용을 참조하십시오.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.