"In App Purchases"를 위한 프로그램 준비
이 장은 애플의 "StoreKit"과 어떻게 게임을 연계시키는지를 다루기 위한 것이 _아닙니다_. 이것은 사용자가 이미 native code plugin를 통해 "StoreKit"과 연계하였음을 가정합니다.
애플의 "StoreKit"문서는 "In App Purchase" 과정을 통해 판매될 수 있는 네가지의 _상품_에 대해 정의합니다:
- 콘텐츠(Content)
- 기능(Functionality)
- 서비스
- 등록(Subscriptions)
이 장은 첫번째 경우만을 다르며 다운로드할 수 있는 콘텐트 개념에만 집중합니다. AssetBundles는 다운로드할 수 있는 콘텐츠의 가장 적합한 후보이며 두가지 경우가 다루어질 것입니다:
- iOS에서의 사용을 위해 어떻게 에셋들을 내보니는지
- 어떻게 다운로드하고 iOS에 저장하는지
iOS에서 에셋 내보내기
다운로드 가능한 콘텐츠를 위한 개별 프로젝트를 가지는 것은 주 프로그램과 같이 오는 다운로드 가능한 컨텐츠와 나중에 다운로드 받는 콘텐츠를 잘 구분하게 해주는 좋은 생각입니다.
다운로드 가능한 콘텐츠에 포함되어 있는 어떤 게임 스크립트도 주 실행프로그램안에 존재해야 합니다.
- Project View 안에 에디터 폴더를 만듭니다.
- ExportBundle.js 스크립트를 그곳에 만들고 다음 코드를 입력합니다:
@MenuItem ("Assets/Build AssetBundle From Selection - Track dependencies") static function ExportBundle(){ var str : String = 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); } }
- 다운로드 가능한"prefabs"인 객체를 디자인하세요
- 내보내져야하는 prefab를 선택 후 오른쪽 클릭하세요
첫 두 스텝이 잘 끝나면 Build AssetBundle From Selection - Track dependencies 메뉴 아이템이 보일 것입니다.
- 이 에셋이 사용하는 모든 것을 포함하고 싶다면 그것을 선택하십시오.
- 저장 대화창이 보일 것이고 원하는 에셋 파일 번들 이름을 입력합니다. _.assetbundle_확장자가 자동으로 생성됩니다. 유니티 iOS실행 시에는 오직 마지막 프로그램으로서의 같은 버전의 유니티 에디터와 같이 빌드된 에셋 번들만을 허락합니다. 좀더 자세한 사항은 BuildPipeline.BuildAssetBundle을 읽으십시오.
iOS에서 에셋 다운로드
- 에셋 번들은 WWW class을 사용해서 다운로드하고 로드할 수 있으며 주 에셋을 인스턴스화 합니다. 코드 샘플입니다:
var download : WWW; var url = "http://somehost/somepath/someassetbundle.assetbundle"; download = new WWW (url); yield download; assetBundle = download.assetBundle; if (assetBundle != null) { // Alternatively you can also load an asset by name (assetBundle.Load("my asset name")) var go : Object = assetBundle.mainAsset; if (go != null) instanced = Instantiate(go); else Debug.Log("Couldnt load resource"); } else { Debug.Log("Couldnt load resource"); }
- 게임 데이터 폴더 옆에 문서폴더에 필요한 파일을 저장할 수 있습니다.
public static string GetiPhoneDocumentsPath () { // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents // Application.dataPath returns // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data // Strip "/Data" from path string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5); // Strip application name path = path.Substring(0, path.LastIndexOf('/')); return path + "/Documents"; }
- NET파일 API를 이용해서 다운로드한 에셋을 캐쉬하고 나중에 다시 사용하기 위해서 WWW class와 _file:/pathtoyourapplication/Documents/savedassetbundle.assetbundle_.를 통해 로드합니다. 캐슁을 위한 샘플코드 입니다:
<file csharp> Code designed for caching on iPhone, cachedAssetBundle path must be different when running in Editor
See code snippet above for getting the path to your Documents folder private var cachedAssetBundle : "path to your Documents folder" + "/savedassetbundle.assetbundle"; var cache = new System.IO.FileStream(cachedAssetBundle, System.IO.FileMode.Create); cache.Write(download.bytes, 0, download.bytes.Length); cache.Close(); Debug.Log("Cache saved: " + cachedAssetBundle); </file> * 출처: 유니티코리아위키 (CC BY-NC-SA 2.0)