본문 바로가기

develop

[Unity] AssetBundle 사용하기 - Web, Server

- 서버가 있을 때는 서버를 사용하지만, 이번 예시에서는 드롭박스를 사용하겠습니다.

이전 포스트에서 생성한 AssetBundle을 드롭박스의 public 폴더에 올리고, 우클릭으로 링크를 얻습니다.

그 링크를 path에 삽입하고 AssetBundle을 로드해 보겠습니다.

using UnityEngine;
using System.Collections;

public class LoadAssetBundleFromWeb : MonoBehaviour
{
    // url은 드롭박스의 public 폴더에 올린 AssetBundle의 링크 주소다.
    // 서버에서 로드하기 위해서는 http://로 시작하는 주소를 사용한다.
    private string path = "url";

    // 버튼 이벤트가 발생하면 코루틴을 시작한다.
    // 이때 함수 인자로 path를 사용한다.
    void OnGUI ()
    {
        if ( GUILayout.Button ( "LoadAssetBundle" ) )
        {
            StartCoroutine ( LoadAssetBundle ( path ) );
        }
    }

    // 인자로 받은 path로 로드를 시도하고, yield로 로드가 완료되기까지 양보한다.
    // 에러가 발생하지 않는다면, Plane 오브젝트를 만들고 회전 후 assetBundle의 텍스쳐를 입힌다.
    IEnumerator LoadAssetBundle ( string path )
    {
        WWW assetBundle = new WWW ( path );
        yield return assetBundle;

        if ( assetBundle.error != null )
            Debug.Log ( "error: " + asserBundle.error.ToString() );
        else
        {
            GameObject go = GameObject.CreatePrimitive ( PrimitiveType.Plane );
            go.transform.rotation = Quaternion.Euler ( new Vector3 ( 90f, 180f, 0f ) );

            go.renderer.material.mainTexture = assetBundle.assetBundle.mainAsset as Texture2D;
        }
    }
}

 

- 다운로드가 되지 않을 경우가 있습니다.

... crossdomain.xml polcity ... 이런 에러가 나오면, 다운로드 받을 웹의 경로를 설정해 줘야 합니다.

유니티 엔진 에디터에서 "Edit > Project Settings > Editor"를 누릅니다.

그리고 인스펙터에서 "WWW Security Emulation > Host URL"에 "http://dl.dropbox.com/"을 입력합니다.

 

- 로컬에서 로드하는 방법이나 서버에서 로드하는 방법에 별 차이가 없습니다.

- 플렛폼 별로 AssetBundle의 호환이 되기도 하고 안 되기도 합니다.

* http://korea.unity3d.com/board/index.php?db=knowhow&no=2196&mari_mode=view%40view&cate=&page=1&listURL=http%3A%2F%2Fkorea.unity3d.com%2Fboard%2F%3Fdb%3Dknowhow&search=&search_str=&temp=

 

반응형