Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Last active June 15, 2019 13:18
Show Gist options
  • Save kyubuns/6daa83d2386de19dc56534dc3492b09d to your computer and use it in GitHub Desktop.
Save kyubuns/6daa83d2386de19dc56534dc3492b09d to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace AssetManager.Editor
{
public static class AssetBuilder
{
[MenuItem("Build/AssetBundles")]
public static void BuildAll()
{
var outputPath = Path.Combine(Application.dataPath, "StreamingAssets", "AssetBundles");
Debug.Log($"Build AssetBundles to {outputPath}");
const BuildAssetBundleOptions options = BuildAssetBundleOptions.StrictMode;
var manifest = BuildPipeline.BuildAssetBundles(outputPath, options, EditorUserBuildSettings.activeBuildTarget);
if (manifest == null) throw new Exception("BuildAssetBundle Error");
Debug.Log("Build AssetBundles Complete!");
}
}
}
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
namespace AssetManager
{
public static class AssetLoader
{
private static ILoader loader;
public static void Initialize()
{
loader = new DefaultAssetLoader();
#if UNITY_EDITOR
if (!File.Exists(Path.Combine(DefaultAssetLoader.GetStreamingAssetsPath(), "AssetBundles")))
{
Debug.Log("use EditorAssetLoader");
loader = new EditorAssetLoader();
}
#endif
}
public static Task Fetch(string assetBundleName)
{
return loader.Fetch(assetBundleName);
}
public static T Load<T>(string assetBundleName, string assetName) where T : UnityEngine.Object
{
return loader.Load<T>(assetBundleName, assetName);
}
public static T[] LoadAll<T>(string assetBundleName) where T : UnityEngine.Object
{
return loader.LoadAll<T>(assetBundleName);
}
}
public interface ILoader
{
Task Fetch(string assetBundleName);
T Load<T>(string assetBundleName, string assetName) where T : UnityEngine.Object;
T[] LoadAll<T>(string assetBundleName) where T : UnityEngine.Object;
}
}
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UniRx.Async;
namespace AssetManager
{
public class DefaultAssetLoader : ILoader
{
private readonly Dictionary<string, AssetBundle> loaded = new Dictionary<string, AssetBundle>();
public async Task Fetch(string assetBundleName)
{
var url = "file://" + Path.Combine(GetStreamingAssetsPath(), assetBundleName);
Debug.Log($"Download AssetBundle from {url}");
var webRequest = UnityWebRequestAssetBundle.GetAssetBundle(url);
await webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.LogError(webRequest.error);
}
loaded.Add(assetBundleName, DownloadHandlerAssetBundle.GetContent(webRequest));
}
public T Load<T>(string assetBundleName, string assetName) where T : Object
{
return loaded[assetBundleName].LoadAsset<T>(assetName);
}
public T[] LoadAll<T>(string assetBundleName) where T : Object
{
return loaded[assetBundleName].LoadAllAssets<T>();
}
public static string GetStreamingAssetsPath()
{
return Path.Combine(Application.streamingAssetsPath, "AssetBundles");
}
}
}
using System.Linq;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;
namespace AssetManager
{
public class EditorAssetLoader : ILoader
{
private readonly List<string> loaded = new List<string>();
public Task Fetch(string assetBundleName)
{
if (!AssetDatabase.GetAllAssetBundleNames().Contains(assetBundleName))
{
throw new Exception($"{assetBundleName} is not found.");
}
loaded.Add(assetBundleName);
return Task.Delay(TimeSpan.FromSeconds(0.1));
}
public T Load<T>(string assetBundleName, string assetName) where T : Object
{
if (!loaded.Contains(assetBundleName))
{
throw new Exception($"{assetBundleName} is not loaded.");
}
var path = AssetDatabase
.GetAssetPathsFromAssetBundle(assetBundleName)
.Single(x => Path.GetFileNameWithoutExtension(x) == assetName);
return AssetDatabase.LoadAssetAtPath<T>(path);
}
public T[] LoadAll<T>(string assetBundleName) where T : Object
{
if (!loaded.Contains(assetBundleName))
{
throw new Exception($"{assetBundleName} is not loaded.");
}
var path = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName).First();
return AssetDatabase.LoadAllAssetsAtPath(path).Cast<T>().ToArray();
}
private static string GetStreamingAssetsPath()
{
return Path.Combine(Application.streamingAssetsPath, "AssetBundles");
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment