본문 바로가기

develop

[Unity] AssetBundle 사용하기 - 웹에서 모바일( 안드로이드 )로 로드하기

- AssetBundle을 제작할 때, 플랫폼끼리 호환이 되지 않습니다. 아래의 표에서 알 수 있듯이, 모바일로 AssetBundle을 사용할 경우에는 플렛폼 별로 따로 제작을 해야 합니다.

  Standalone Web Player iOS Android
Editor 👌 👌 👌 👌
Standalone 👌 👌    
Web Player 👌 👌    
iOS     👌  
Android       👌

 

- 이전에 AssetBundle을 생성하는 코드를 보겠습니다.

// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle

using UnityEngine;
using UnityEditor;
public class ExportAssetBundles
{
    [MenuItem ( "Assets/Build AssetBundle From Selection - Track dependencies" )]
    static void ExportResource ()
    {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ( "Save Resource", "", "New Resource", "unity3d" );

        if ( path.Length != 0 )
        {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered ( typeof ( Object ), SelectionMode.DeepAssets );

            BuildPipeline.BuildAssetBundle (
                Selection.activeObject,
                selection,
                path,
                BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets );

            Selection.objects = selection;
        }
    }

    [MenuItem ( "Assets/Build AssetBundle From Selection - No dependency tracking" )]
    static void ExportResourceNoTrack ()
    {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ( "Save Resource", "", "New Resource", "unity3d" );

        if ( path.Length != 0 )
        {
            // Build the resource file from the active selection.
            BuildPipeline.BuildAssetBundle ( Selection.activeObject, Selection.objects, path );
        }
    }
}

- 위의 코드를 플렛폼별로 따로 생성하도록 수정하겠습니다.

// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle

using UnityEngine;
using UnityEditor;
public class ExportAssetBundles
{
    [MenuItem ( "Assets/Build AssetBundle From Selection - Track dependencies" )]
    static void ExportResource ()
    {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ( "Save Resource", "", "New Resource", "unity3d" );

        if ( path.Length != 0 )
        {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered ( typeof ( Object ), SelectionMode.DeepAssets );

            BuildPipeline.BuildAssetBundle (
                Selection.activeObject,
                selection,
                path,
                BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets );

            Selection.objects = selection;
        }
    }

    [MenuItem ( "Assets/Build AssetBundle From Selection - To Android" )]
    static void ExportToAndroid ()
    {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ( "Save Resource", "", "New Resource", "unity3d" );

        if ( path.Length != 0 )
        {
             // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered ( typeof ( Object ), SelectionMode.DeepAssets );

            BuildPipeline.BuildAssetBundle (
                Selection.activeObject,
                selection,
                path,
                BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
                BuildTarget.Android );

            Selection.objects = selection;
        }
    }
}

위의 수정된 코드에서 바뀐 점은 ExportResourceNoTrack 함수가 ExportToAndroid 함수로 바뀌면서 BuildPipeline.BuildAssetBundle 함수의 마지막 인자로 BuildTarget.Android가 들어간 부분입니다.

반응형