Created
May 22, 2015 11:35
-
-
Save garrynewman/7ac9a67a4541be6c6563 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class FileSystem_AssetBundles : IFileSystem | |
{ | |
public static bool isError = false; | |
public static string loadingError = ""; | |
public static FileSystem_AssetBundles Instance = null; | |
AssetBundle rootBundle; | |
AssetBundleManifest manifest; | |
Dictionary< string, AssetBundle > bundles = new Dictionary<string, AssetBundle>( System.StringComparer.OrdinalIgnoreCase ); | |
Dictionary< string, AssetBundle > files = new Dictionary<string, AssetBundle>( System.StringComparer.OrdinalIgnoreCase ); | |
string assetPath; | |
public FileSystem_AssetBundles() | |
{ | |
Instance = this; | |
} | |
public void Init( string assetRoot ) | |
{ | |
isError = false; | |
assetPath = System.IO.Path.GetDirectoryName( assetRoot ) + System.IO.Path.DirectorySeparatorChar; | |
rootBundle = AssetBundle.CreateFromFile( assetRoot ); | |
if ( rootBundle == null ) | |
{ | |
LoadError( "Couldn't load root AssetBundle - " + assetRoot ); | |
return; | |
} | |
var manifestList = rootBundle.LoadAllAssets<AssetBundleManifest>(); | |
if ( manifestList.Length != 1 ) | |
{ | |
LoadError( "Couldn't find AssetBundleManifest - " + manifestList.Length ); | |
return; | |
} | |
manifest = manifestList[0]; | |
foreach ( var ab in manifest.GetAllAssetBundles() ) | |
{ | |
LoadBundle( ab ); | |
if ( isError ) | |
return; | |
} | |
BuildFileIndex(); | |
} | |
~FileSystem_AssetBundles() | |
{ | |
manifest = null; | |
foreach ( var bundle in bundles ) | |
{ | |
bundle.Value.Unload( false ); | |
Object.DestroyImmediate( bundle.Value ); | |
} | |
bundles.Clear(); | |
if ( rootBundle ) | |
{ | |
rootBundle.Unload( false ); | |
Object.DestroyImmediate( rootBundle ); | |
rootBundle = null; | |
} | |
} | |
void LoadError( string err ) | |
{ | |
Debug.LogError( err ); | |
loadingError = err; | |
isError = true; | |
} | |
void LoadBundle( string bundleName ) | |
{ | |
// Don't load more than once | |
if ( bundles.ContainsKey( bundleName ) ) | |
return; | |
// | |
// Load bundle from file | |
// | |
var fileLocation = assetPath + bundleName; | |
var asset = AssetBundle.CreateFromFile( fileLocation ); | |
if ( asset == null ) | |
{ | |
LoadError( "Couldn't load AssetBundle - " + fileLocation ); | |
return; | |
} | |
bundles.Add( bundleName, asset ); | |
} | |
void BuildFileIndex() | |
{ | |
files.Clear(); | |
foreach ( var bundle in bundles ) | |
{ | |
if ( bundle.Key.StartsWith( "content", System.StringComparison.InvariantCultureIgnoreCase ) ) | |
continue; | |
foreach ( var filename in bundle.Value.GetAllAssetNames() ) | |
{ | |
files.Add( filename, bundle.Value ); | |
} | |
} | |
} | |
public T[] LoadAll<T>( string folder ) where T : Object | |
{ | |
var list = new List<T>(); | |
foreach ( var file in files.Where( x => x.Key.StartsWith( folder, System.StringComparison.InvariantCultureIgnoreCase ) ) ) | |
{ | |
Profiler.BeginSample( "bundle.LoadAsset<T>" ); | |
var pf = file.Value.LoadAsset<T>( file.Key ); | |
Profiler.EndSample(); | |
if ( pf == null ) continue; | |
list.Add( pf ); | |
} | |
return list.ToArray(); | |
} | |
public T Load<T>( string filePath ) where T : Object | |
{ | |
AssetBundle bundle = null; | |
if ( !files.TryGetValue( filePath, out bundle ) ) | |
{ | |
Debug.LogWarning( "[BUNDLE] Not found: " + filePath ); | |
return null; | |
} | |
Profiler.BeginSample( "bundle.LoadAsset<T>" ); | |
var pf = bundle.LoadAsset<T>( filePath ); | |
Profiler.EndSample(); | |
if ( pf == null ) | |
{ | |
Debug.LogWarning( "[BUNDLE] Not found in bundle: " + filePath ); | |
} | |
return pf; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment