본문 바로가기
Unity

[Unity] 간단한 에셋 번들 리소스 빌드, 패치하는 법

by 소리쿤 2022. 7. 16.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

#if UNITY_EDITOR
public class AssetBundleBuilder
{
	[MenuItem("Tools/Build AssetBundle")]
	public static void Build()
	{
		string outputPath = "Assets/Bundles";

		BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
	}
}
#endif

1. 빌드용 코드 작성

2. 번들 그룹 네이밍
3. 클릭
4. 뿅

public class AssetBundleLoader : MonoBehaviour
{
    AssetBundle bundle = null;

    void Start()
    {
        StartCoroutine(GetAssetBundle(() => 
        {
            Debug.Log(bundle);

            var obj = bundle.LoadAsset("Button") as GameObject;
            Instantiate(obj);
        }));
    }

    IEnumerator GetAssetBundle(Action callback)
    {
        var path = System.IO.Path.Combine(Application.dataPath, "Bundles/button");
        UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(path);
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else
        {
            bundle = DownloadHandlerAssetBundle.GetContent(www);
        }

        callback?.Invoke();
    }
}

5. 로드용 코드 작성

6. 성공