您的位置:首页 > 移动开发 > Unity3D

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;

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: