Unity에서 iOS용 인앱 구매(IAP) 애플리케이션 준비하기
이 문서에서는 Unity를 사용하여 Apple의 StoreKit API와 통합하여 iOS 애플리케이션을 설정하는 방법을 설명합니다. 기본적으로 사용자가 네이티브 코드 플러그인을 통해 StoreKit과 통합했다고 가정합니다.
인앱 구매 제품 종류
Apple StoreKit 문서에 따르면 인앱 구매를 통해 판매할 수 있는 제품 유형은 다음 네 가지입니다. 1. 콘텐츠 2. 기능 3. 서비스 4. 구독
이 문서에서는 첫 번째 유형인 콘텐츠에 주로 초점을 맞추고, 다운로드 가능한 콘텐츠의 개념에 대해 설명합니다.
에셋 번들이란?
Unity에서 다운로드 가능한 콘텐츠를 구현하기 위해 에셋 번들을 사용하는 것이 권장됩니다. 에셋 번들은 Unity 프로젝트에서 특정 리소스를 패키징하는 방법입니다.
에셋 번들 생성하기
에셋 번들은 에디터 스크립트를 사용하여 생성할 수 있습니다. 아래는 에셋 번들을 생성하는 간단한 예시입니다.
using UnityEngine;
using UnityEditor;
public class ExportBundle : MonoBehaviour {
[MenuItem ("Assets/Build AssetBundle From Selection - Track dependencies")]
static void DoExport() {
string str = EditorUtility.SaveFilePanel("Save Bundle...", Application.dataPath, Selection.activeObject.name, "assetbundle");
if (str.Length != 0) {
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, str, BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
}
}
}
위 코드를 ExportBundle
파일로 저장하고 Editor
폴더에 넣어야 합니다.
단계 | 설명 |
---|---|
1 | Editor 폴더를 생성하고 ExportBundle 스크립트를 추가합니다. |
2 | 프로젝트 뷰에서 프리팹을 선택한 후 메뉴에서 Build AssetBundle From Selection - Track dependencies 를 선택합니다. |
3 | 에셋 번들 파일의 이름과 위치를 지정하여 저장합니다. |
iOS에서 에셋 다운로드
다운로드한 에셋 번들은 iOS 애플리케이션 샌드박스의 Library 폴더에 저장되며, No Backup 플래그가 설정되어 iCloud에 백업되지 않습니다. 다운로드를 수행하기 위해 WWW
클래스를 사용할 수 있습니다. 아래는 에셋 번들을 다운로드하는 코드 예시입니다.
IEnumerator GetAssetBundle() {
WWW download;
string url = "https://somehost/somepath/someassetbundle.assetbundle";
while (!Caching.ready)
yield return null;
download = WWW.LoadFromCacheOrDownload(url, 0);
yield return download;
AssetBundle assetBundle = download.assetBundle;
if (assetBundle != null) {
Object go = assetBundle.mainAsset;
if (go != null)
Instantiate(go);
else
Debug.Log("Couldn't load resource");
} else {
Debug.Log("Couldn't load resource");
}
}
파일 저장과 No Backup 플래그
파일을 저장할 때는 Application.temporaryCachePath
또는 Application.persistentDataPath
를 사용할 수 있습니다. No Backup 플래그를 설정하여 iCloud에 백업되지 않도록 해야 합니다.
string cachedAssetBundle = Application.temporaryCachePath + "/savedassetbundle.assetbundle";
System.IO.FileStream cache = new System.IO.FileStream(cachedAssetBundle, System.IO.FileMode.Create);
cache.Write(download.bytes, 0, download.bytes.Length);
cache.Close();
iOS.Device.SetNoBackupFlag(cachedAssetBundle);
Debug.Log("Cache saved: " + cachedAssetBundle);
참고사항
UIFileSharingEnabled
를true
로 설정하면 Documents 폴더에 저장된 파일에 대해 읽기 테스트를 수행할 수 있습니다.- 애플리케이션을 App Store에 제출할 때 No Backup 플래그를 설정하지 않으면 거부될 수 있습니다.
이 문서는 Unity에서 iOS 플랫폼의 인앱 구매 기능을 설정하는 기본을 다루었습니다. 이러한 기능을 통해 더욱 풍부한 사용자 경험을 제공할 수 있습니다.