Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created August 29, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/8ae46cbb496c6c105d4e5990621d0e5b to your computer and use it in GitHub Desktop.
Save tsubaki/8ae46cbb496c6c105d4e5990621d0e5b to your computer and use it in GitHub Desktop.
アセットバンドルシミュレーターもどき
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class AssetBundleSimulator
{
private AssetBundleSimulator (){}
private static AssetBundleSimulator instance = new AssetBundleSimulator ();
public static AssetBundleSimulator Instance {
get {
return instance;
}
}
// assetBundle name / assetbundle
// 一度読み込んだAssetBundleはキャッシュしておく(複数回呼出しても大丈夫なように)
Dictionary<string, AssetBundle> assetBundleCache = new Dictionary<string, AssetBundle> ();
public void Init ()
{
// AssetBundleの一覧を用意(有無チェック面倒)
#if ASSETBUNDLE_SIMULATOR && UNITY_EDITOR
foreach( var assetBundleName in UnityEditor.AssetDatabase.GetAllAssetBundleNames())
{
assetBundleCache.Add(assetBundleName, null);
}
#else
var ab = AssetBundle.LoadFromFile(string.Format("{0}/{1}",Application.streamingAssetsPath, "StreamingAssets"));
var manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
foreach( var assetBundleName in manifest.GetAllAssetBundles())
{
assetBundleCache.Add(assetBundleName, null);
}
ab.Unload(false);
#endif
}
public void LoadScene (string assetBundleName, string sceneName, LoadSceneMode sceneMode)
{
#if ASSETBUNDLE_SIMULATOR && UNITY_EDITOR
string[] levelPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName (assetBundleName, sceneName);
if (sceneMode == LoadSceneMode.Additive)
UnityEditor.EditorApplication.LoadLevelAdditiveInPlayMode (levelPaths [0]);
else
UnityEditor.EditorApplication.LoadLevelInPlayMode (levelPaths [0]);
#else
GetAssetBundle(assetBundleName);
SceneManager.LoadScene(sceneName, sceneMode);
#endif
}
public IEnumerator LoadSceneAsync (string assetBundleName, string sceneName, LoadSceneMode sceneMode)
{
#if ASSETBUNDLE_SIMULATOR && UNITY_EDITOR
string[] levelPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName (assetBundleName, sceneName);
if (sceneMode == LoadSceneMode.Additive)
yield return UnityEditor.EditorApplication.LoadLevelAdditiveAsyncInPlayMode (levelPaths [0]);
else
yield return UnityEditor.EditorApplication.LoadLevelAsyncInPlayMode (levelPaths [0]);
#else
GetAssetBundle(assetBundleName);
yield return SceneManager.LoadSceneAsync(sceneName, sceneMode);
#endif
}
public T LoadAsset<T> (string assetBundleName, string assetName) where T : UnityEngine.Object
{
#if ASSETBUNDLE_SIMULATOR && UNITY_EDITOR
string[] assetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName (assetBundleName, assetName);
var obj = UnityEditor.AssetDatabase.LoadAssetAtPath<T> (assetPaths [0]);
return obj;
#else
return GetAssetBundle(assetBundleName).LoadAsset<T>(assetName);
#endif
}
public T[] LoadAllAssets<T>(string assetBundleName) where T : UnityEngine.Object
{
#if ASSETBUNDLE_SIMULATOR && UNITY_EDITOR
string[] assetPaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle (assetBundleName);
List<T> assetList = new List<T>();
for(int i=0; i<assetPaths.Length; i++){
var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(assetPaths[i]);
if( asset != null ){
assetList.Add(asset);
}
}
return (T[])assetList.ToArray();
#else
return GetAssetBundle(assetBundleName).LoadAllAssets<T>();
#endif
}
public AssetBundle GetAssetBundle(string assetBundleName)
{
var assetBundle = assetBundleCache[assetBundleName];
if( assetBundle == null){
var path = string.Format("{0}/{1}",Application.streamingAssetsPath, assetBundleName);
assetBundle = AssetBundle.LoadFromFile(path);
assetBundleCache [assetBundleName] = assetBundle;
}
return assetBundle;
}
public void UnloadAssetBundle(string assetBundleName)
{
var assetBndle = assetBundleCache [assetBundleName];
if (assetBndle != null) {
assetBndle.Unload (false);
}
assetBundleCache [assetBundleName] = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment