ODR(온디맨드 리소스)은 iOS 및 tvOS에서 사용할 수 있는 기능으로, 애플리케이션의 크기를 줄일 수 있게 합니다. 애플리케이션 시작 시 필요한 핵심 에셋과 선택 사항 또는 게임의 후반부에 나타나는 에셋을 분리하여 크기를 줄일 수 있습니다. 이러한 추가 에셋은 에셋 번들로 알려져 있으며, 모든 Unity 빌드 타겟에서 사용할 수 있습니다. 하지만 앱 스토어에서 호스팅하려면 추가 단계를 수행하여 이를 활성화해야 합니다.
에셋 번들은 모델, 머티리얼, 텍스처, 씬과 같은 에셋 파일을 포함할 수 있지만 스크립트는 포함할 수 없습니다. 따라서 스크립팅 로직은 메인 애플리케이션에 저장해야 합니다. 권장되는 베스트 프랙티스에서는 에셋 번들의 크기가 최대 64MB입니다. 이렇게 하면 로드 시간을 줄이고 기기에서 사용되는 스토리지 공간을 최소화할 수 있습니다.
ODR을 사용하도록 프로젝트를 설정하려면 다음 단계를 따르십시오.
에셋 번들을 생성하려면 다음 단계를 따르십시오.
Bundles
, ODR
, Editor
라는 이름의 폴더 3개를 추가로 생성합니다.에셋 번들을 생성할 때는 에셋 번들 다운로드 요청 시 식별할 수 있는 태그를 할당해야 합니다. 번들의 파일 이름과 일치하는 레이블을 생성하는 것이 좋습니다.
태그를 생성하고 할당하려면 다음 단계를 따르십시오.
새 에셋 번들 파일을 생성하려면 에디터 스크립트에서 새 레이블을 참조해야 합니다. 에디터 스크립트를 생성하려면 다음 단계를 따르십시오.
1. Editor 폴더를 오른쪽 클릭하고 Create > C# Script를 선택합니다.
1. 새 스크립트의 이름을 BuildiOSAssetBundles
로 지정합니다.
BuildiOSAssetBundles.cs
를 열고 다음 코드 예시를 복사합니다. 이 예시에서는 textures
레이블을 사용합니다. 레이블 이름을 전체적으로 소문자로 변경합니다.
참고: 이 예시에서는 압축되지 않은 번들을 사용하지만, 앱 감량을 사용하기 위한 필수 조건은 아닙니다.
using UnityEngine;
using UnityEditor;
public class BuildiOSAssetBundles : MonoBehaviour
{
[InitializeOnLoadMethod]
static void SetupResourcesBuild( )
{
UnityEditor.iOS.BuildPipeline.collectResources += CollectResources;
}
static UnityEditor.iOS.Resource[] CollectResources( )
{
return new UnityEditor.iOS.Resource[]
{
new UnityEditor.iOS.Resource( "textures", "Assets/ODR/textures" ).AddOnDemandResourceTags( "textures" ),
new UnityEditor.iOS.Resource( "bundle", "Assets/Bundles/bundle.unity3d" ).AddOnDemandResourceTags( "bundle" ),
};
}
[MenuItem( "Bundle/Build iOS AssetBundle" )]
static void BuildAssetBundles( )
{
var options = BuildAssetBundleOptions.None;
bool shouldCheckODR = EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS;
#if UNITY_TVOS
shouldCheckODR |= EditorUserBuildSettings.activeBuildTarget == BuildTarget.tvOS;
#endif
if( shouldCheckODR )
{
#if ENABLE_IOS_ON_DEMAND_RESOURCES
if( PlayerSettings.iOS.useOnDemandResources )
options |= BuildAssetBundleOptions.UncompressedAssetBundle;
#endif
#if ENABLE_IOS_APP_SLICING
options |= BuildAssetBundleOptions.UncompressedAssetBundle;
#endif
}
BuildPipeline.BuildAssetBundles( "Assets/ODR", options, EditorUserBuildSettings.activeBuildTarget );
}
}
이전 코드 샘플의 다음 줄은 textures 레이블로 태그된 파일을 가져다가 textures라는 이름의 에셋 번들 파일을 Assets/ODR 폴더에 생성합니다.
new UnityEditor.iOS.Resource( "textures", "Assets/ODR/textures" ).AddOnDemandResourceTags( "textures" )
이전 코드 예시에서는 다음과 같은 줄을 의도적으로 추가하였습니다. 이는 다른 프로젝트나 서드 파티 판매자가 이미 빌드한 bundle이라는 이름을 가지는 에셋 번들을 추가하는 역할을 합니다.
new UnityEditor.iOS.Resource( "bundle", "Assets/Bundles/bundle.unity3d" ).AddOnDemandResourceTags( "bundle" )
이 전체 코드 샘플은 Unity 에디터 메뉴 바에 새 메뉴를 생성합니다. Bundle > Build iOS AssetBundle로 이동하여 ODR 폴더 내에 에셋 번들을 생성합니다.
에셋 번들을 생성하고 나면 애플리케이션에 로드해야 합니다.
Assets 폴더에 LoadBundle.cs
라는 스크립트를 생성하고 다음 코드 예시에 복사합니다. 이 예시 스크립트는 textures ODR 에셋 번들을 다운로드하고 TextureBundle의 공용 구성원에 할당합니다.
using UnityEngine;
using UnityEngine.iOS;
using System;
using System.Collections;
public class LoadBundle : MonoBehaviour
{
public AssetBundle TextureBundle;
void Start( )
{
LoadAssetAsync( "textures", "textures" );
}
public async Awaitable LoadAssetAsync( string resourceName, string odrTag )
{
// Create the request
using(OnDemandResourcesRequest request = OnDemandResources.PreloadAsync( new string[] { odrTag } ))
{
// Wait until request is completed
await request;
// Check for errors
if( request.error != null )
throw new Exception( "ODR request failed: " + request.error );
TextureBundle = AssetBundle.LoadFromFile( "res://" + resourceName );
}
}
}
에셋 번들을 생성 및 로드하고 나면 다음 단계를 완료해야 합니다.
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.