Asseet Bundle 샘플을 만들 때의 노트

10560 단어 Unity

사용된 Unity 버전


5.3.2f

AssetBundle 소개

  • Asset(리소스) 관리 메커니즘
  • 인터넷을 통해 자원을 다운로드할 수 있음
  • 버전 관리 가능
  • AsseetBundle 만들기

  • AssetBundle의 이름을 표시
  • Editor Script
  • 실행
  • 서버에 업로드
  • 1. AssetBundle 이름 표시


    AssetBundle에 포함된 디렉토리에 이름을 표시합니다.

    2. Editor Script 실행


    AsseetBundle 생성을 위한 스크립트 만들기
    EDITOr 폴더에 배치됩니다.
    ExportAssetBundle.cs
    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using System.IO;
    
    
    public class ExportAssetBundle
    {
        [MenuItem("Export/AssetBundle")]
        static void Export()
        {
            Directory.CreateDirectory(Application.streamingAssetsPath);
            BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath);
        }
    }
    
    이 스크립트가 EDITOr 폴더에 있는 경우
    Unity 메뉴에 Export 항목이 추가되었습니다.

    Export->AsseetBund를 선택한 후
    AssetBundle 생성 처리를 수행합니다.

    3. 서버에 업로드


    위 스크립트를 실행한 후
    StreamingAsset 폴더가 생성되었기 때문에
    이 폴더를 서버에 업로드합니다.

    AssetBundle(서버) 로드


    서버의 AssetBundle에서 리소스를 로드하는 절차는 다음과 같습니다.
  • WW.LoadFrom Cache OrDownload에서 AssetBundle
  • 다운로드
  • LoadAsset 방법으로 다운로드한 AssetBundle에서 목적 자원을 읽기
  • 소스는 아래와 같다.
    CachingLoadExample.cs
    using UnityEngine;
    using System.Collections;
    
    public class CachingLoadExample : MonoBehaviour
    {
        string bundleURL = "http://localhost:9000/assets/StreamingAssets/sprites";
        string assetName = "Test";
        int version = 0;
    
        void Start()
        {
            StartCoroutine(DownloadAndCache());
        }
    
        IEnumerator DownloadAndCache()
        {
            while(!Caching.ready)
                yield return null;
    
            using(WWW www = WWW.LoadFromCacheOrDownload(bundleURL, version)) {
                yield return www;
    
                if (www.error != null) {
                    throw new UnityException("WWW download had an error" + www.error);
                }
    
                AssetBundle bundle = www.assetBundle;
    
                Instantiate(bundle.LoadAsset(assetName));
    
                bundle.Unload(false);
            }
        }
    }
    

    AssetBundle(로컬) 로드


    Unity 프로젝트 내의 StreamingAssets에서 에셋을 읽을 수도 있습니다.
    AsseetBundle의 읽기는 AssetBundle입니다.LoadFrom FileAsync 방법을 사용합니다.
    LoadAssetBundle.cs
    using UnityEngine;
    using System.Collections;
    
    public class LoadAssetBundle : MonoBehaviour
    {
        string assetName = "Test";
    
        string AssetPath {
            get {
                return Application.streamingAssetsPath + "/sprites";
            }
        }
    
        IEnumerator Start()
        {
            var resultAssetBundle = AssetBundle.LoadFromFileAsync(AssetPath);
    
            yield return new WaitWhile(() => resultAssetBundle.isDone == false);
    
            var assetbundle = resultAssetBundle.assetBundle;
            var resultObject = assetbundle.LoadAssetAsync<GameObject>(assetName);
    
            yield return new WaitWhile(() => resultObject.isDone == false);
    
            GameObject.Instantiate(resultObject.asset);
            assetbundle.Unload(false);
        }
    }
    

    참고 자료


    WWW
    http://docs.unity3d.com/ja/current/ScriptReference/WWW.html
    AssetBundle
    http://docs.unity3d.com/ja/current/ScriptReference/AssetBundle.html
    Unity 5의 Asseet Bund에 대해서 짧게 정리를 해봤어요.
    http://www.slideshare.net/monry84/20150522-31-unity
    [Unity] Unity 5.3에서 Asseet Bundle까지의 변화... 정리.
    http://tsubakit1.hateblo.jp/entry/2015/12/16/233336
    Unity 5의 새로운 AssetBundle 기능을 사용하여 서버에서 AssetBundle 다운로드
    http://kazuooooo.hatenablog.com/entry/2015/06/14/150019

    좋은 웹페이지 즐겨찾기