본문 바로가기
개발/C#

유니티 C# 에셋 번들 저장 불러오기 간단 구현 Asset Bundle

by SPNK 2024. 2. 20.
반응형
  • 코드 작성
using UnityEngine;
using System.Collections;
using System.IO;

public class AssetBundleManager : MonoBehaviour
{
    // 저장된 에셋 번들 파일의 경로
    private string bundlePath = "Assets/StreamingAssets/myAssetBundle";

    // 에셋 번들을 저장하는 함수
    public void SaveAssetBundle()
    {
        // 에셋 번들을 생성할 경로
        string outputPath = "Assets/StreamingAssets/myAssetBundle";

        // 에셋 번들 생성
        BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);

        // 생성된 에셋 번들을 저장할 경로
        string destination = Path.Combine(Application.streamingAssetsPath, "myAssetBundle");

        // 만약 기존에 파일이 있다면 삭제
        if (File.Exists(destination))
        {
            File.Delete(destination);
        }

        // 생성된 에셋 번들을 저장
        File.Move(Path.Combine(outputPath, "myassetbundle"), destination);
    }

    // 에셋 번들을 로드하는 함수
    public void LoadAssetBundle()
    {
        // 저장된 에셋 번들 파일의 경로
        string bundlePath = Path.Combine(Application.streamingAssetsPath, "myAssetBundle");

        // 에셋 번들 로드
        AssetBundle assetBundle = AssetBundle.LoadFromFile(bundlePath);

        if (assetBundle == null)
        {
            Debug.LogError("에셋 번들을 로드하는데 실패했습니다.");
            return;
        }

        // 에셋 번들에서 원하는 에셋을 로드하거나 사용
        // 예시: GameObject prefab = assetBundle.LoadAsset<GameObject>("MyPrefab");

        // 에셋 번들 사용이 끝나면 언로드
        assetBundle.Unload(false);
    }
}
반응형

댓글