C# Unity用AssetBundle加载本地资源 (1)
2016-09-12 14:00
591 查看
/// <summary>
/// AssetBundle加载器
/// </summary>
public class AssetBundleLoger
{
/// <summary>
/// 更新文件下载目录
/// </summary>
private const string StreamingAssetPath = "/StreamingAssets/";
private static string _ApplicationStreamingAssets = Application.streamingAssetsPath;
public static string ApplicationDataPath = Application.dataPath;
public static string ApplicationPersistentAssets = Application.platform == RuntimePlatform.Android ? Application.persistentDataPath : _ApplicationStreamingAssets;
/// <summary>
/// assetbundle内存列表
/// </summary>
private static Dictionary<string, AssetBundle> AssetBundleArray = new Dictionary<string, AssetBundle>();
/// <summary>
/// object内存列表, 用来缓存assetbundle加载出的object对象加快加载速度
/// </summary>
private static Dictionary<string, object> _ObjectMemory = new Dictionary<string, object>();
/// <summary>
/// 根据平台区分路径间隔
/// </summary>
/// <returns></returns>
public static string GetPathGap()
{
var gapPath = StreamingAssetPath;
if (Application.platform == RuntimePlatform.WindowsEditor)
{
gapPath = "/";
}
return gapPath;
}
/// <summary>
/// 加载AssetBundle(<可能会有加载时间过长的问题, 测试版本>)
/// </summary>
/// <param name="path">assetbundle地址</param>
/// <returns>assetbundle对象</returns>
private static AssetBundle LoadAssetBundle(string path)
{
return AssetBundle.CreateFromFile(path);
}
/// <summary>
/// AssetBundle加载
/// </summary>
/// <param name="path">名字</param>
/// <param name="loadType">加载的类型</param>
/// <returns></returns>
public static Object Load(string path, Type loadType)
{
try
{
// 资源Assetbundle
AssetBundle source = null;
Object result = null;
Stopwatch sw = new Stopwatch();
sw.Start();
if (!AssetBundleArray.ContainsKey(path))
{
var dep = LoadAssetBundle(ApplicationPersistentAssets + GetPathGap() + path + ".assetbundle");
AssetBundleArray.Add(path, dep);
source = dep;
}
else
{
source = AssetBundleArray[path];
}
result = source.Load(path, loadType);
sw.Stop();
Debug.Log("耗时" + sw.ElapsedMilliseconds);
return result;
}
catch (Exception)
{
throw;
}
}
/// AssetBundle加载器
/// </summary>
public class AssetBundleLoger
{
/// <summary>
/// 更新文件下载目录
/// </summary>
private const string StreamingAssetPath = "/StreamingAssets/";
private static string _ApplicationStreamingAssets = Application.streamingAssetsPath;
public static string ApplicationDataPath = Application.dataPath;
public static string ApplicationPersistentAssets = Application.platform == RuntimePlatform.Android ? Application.persistentDataPath : _ApplicationStreamingAssets;
/// <summary>
/// assetbundle内存列表
/// </summary>
private static Dictionary<string, AssetBundle> AssetBundleArray = new Dictionary<string, AssetBundle>();
/// <summary>
/// object内存列表, 用来缓存assetbundle加载出的object对象加快加载速度
/// </summary>
private static Dictionary<string, object> _ObjectMemory = new Dictionary<string, object>();
/// <summary>
/// 根据平台区分路径间隔
/// </summary>
/// <returns></returns>
public static string GetPathGap()
{
var gapPath = StreamingAssetPath;
if (Application.platform == RuntimePlatform.WindowsEditor)
{
gapPath = "/";
}
return gapPath;
}
/// <summary>
/// 加载AssetBundle(<可能会有加载时间过长的问题, 测试版本>)
/// </summary>
/// <param name="path">assetbundle地址</param>
/// <returns>assetbundle对象</returns>
private static AssetBundle LoadAssetBundle(string path)
{
return AssetBundle.CreateFromFile(path);
}
/// <summary>
/// AssetBundle加载
/// </summary>
/// <param name="path">名字</param>
/// <param name="loadType">加载的类型</param>
/// <returns></returns>
public static Object Load(string path, Type loadType)
{
try
{
// 资源Assetbundle
AssetBundle source = null;
Object result = null;
Stopwatch sw = new Stopwatch();
sw.Start();
if (!AssetBundleArray.ContainsKey(path))
{
var dep = LoadAssetBundle(ApplicationPersistentAssets + GetPathGap() + path + ".assetbundle");
AssetBundleArray.Add(path, dep);
source = dep;
}
else
{
source = AssetBundleArray[path];
}
result = source.Load(path, loadType);
sw.Stop();
Debug.Log("耗时" + sw.ElapsedMilliseconds);
return result;
}
catch (Exception)
{
throw;
}
}
相关文章推荐
- unity AssetBundle 加载资源 笔记
- Unity异步加载AssetBundle方案,4.x版本资源加载(5.x版本资源可以参考插件AssetBundleManager)
- Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
- Unity_AssetBundle 动态资源加载
- 关于unity 中使用AssetBundle加载资源,shader偶尔会丢失的问题解决办法
- Unity—AssetBundle的打包及四种加载资源方式
- Unity 关于AssetBundle(资源束)打包和加载(一)
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- unity之Asset Bundle资源打包加载
- Unity的资源加载以及AssetBundle的一些坑
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- Unity中 BundleAsset资源的打包和解析加载
- Unity的动态加载AssetBundle资源笔记。
- Unity3d发布Android版通过WWW加载本地AssetBundle资源
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- Unity中AssetBundle资源打包
- [Unity AssetBundle]Asset资源处理
- [Unity Asset]AssetBundle系列——游戏资源打包
- Unity3D之AssetBundle资源加载封装
- Unity资源Assetbundle