Ejemplo n.º 1
0
        public static AssetBundleHandle Load(string path)
        {
            path = path.ToLower();
            using (var timer = LoadTimer.Start <AssetBundleLoader>(path))
            {
                var handle = AssetSystem.Instance.FindAsset <AssetBundleHandle>(path);
                if (null == handle)
                {
                    handle = new AssetBundleHandle(path);
                    try
                    {
                        LoadDependencies(handle, path);

                        string loadUrl = GetLoadUrl(path);
                        var    bundle  = AssetBundle.LoadFromFile(loadUrl);
                        if (null == bundle)
                        {
                            throw new ApplicationException(string.Format(
                                                               "Failed to load AssetBundle:{0}",
                                                               path));
                        }

                        handle.Bundle = bundle;
                        AssetSystem.Instance.AddAsset(handle);
                    }
                    catch (Exception e)
                    {
                        handle.Dispose();
                        throw new ApplicationException("Error when loading " + path, e);
                    }
                }
                return(handle);
            }
        }
Ejemplo n.º 2
0
        public static LoadTimer Start <T>(string url)
        {
            var timer = new LoadTimer(typeof(T), url);

            timer.Start();
            return(timer);
        }
Ejemplo n.º 3
0
        public static PrefabResource Load(string path)
        {
            if (!IsPlaying)
            {
                return(LoadInEditor(path));
            }

            using (var timer = LoadTimer.Start <PrefabLoader>(path))
            {
                var res = AssetSystem.Instance.FindAsset <PrefabResource>(path);
                if (null == res)
                {
                    res = new PrefabResource(path);
                    try
                    {
                        if (IsDev)
                        {
                            res = LoadInEditor(path, res);
                        }
                        else
                        {
                            string bundleName = string.Format(
                                "{0}/{1}.prefab{2}",
                                PathRouter.NoPrefix(PathRouter.Res),
                                path,
                                PathRouter.AssetBundleSuffix);
                            var assetBundleHandle = AssetBundleLoader.Load(bundleName);

                            res.prefabObject = assetBundleHandle.Bundle.LoadAsset(
                                Path.GetFileNameWithoutExtension(path));
                            if (null == res.prefabObject)
                            {
                                throw new ApplicationException(string.Format(
                                                                   "AssetBundle.LoadAsset({0}) => null, Bundle:{1}",
                                                                   Path.GetFileNameWithoutExtension(path),
                                                                   assetBundleHandle.Path));
                            }
                            res.AddDependency(assetBundleHandle);
                        }
                        AssetSystem.Instance.AddAsset(res);
                    }
                    catch (Exception e)
                    {
                        res.Dispose();
                        throw new ApplicationException("Error when loading Prefab:" + path, e);
                    }
                }
                return(res);
            }
        }
Ejemplo n.º 4
0
 public static byte[] Load(string path)
 {
     using (var timer = LoadTimer.Start <FileLoader>(path))
     {
         if (IsDev)
         {
             #if UNITY_EDITOR
             path = string.Format("{0}/{1}", PathRouter.Res, path);
             var asset = UnityEditor.AssetDatabase.LoadAssetAtPath <TextAsset>(path);
             return(null != asset ? asset.bytes : null);
             #else
             throw new ApplicationException("IsDev is not allowed unless in Editor");
             #endif
         }
         else
         {
             return(BytesLoader.Load(path));
         }
     }
 }
Ejemplo n.º 5
0
        public static void Load(string name)
        {
            string path = PathRouter.ScenesFolder + name;

            using (var timer = LoadTimer.Start <SceneLoader>(path))
            {
                if (null == SceneResource.Current || SceneResource.Current.Path != path)
                {
                    SceneResource sceneResource = new SceneResource(path);
                    try
                    {
                        if (Application.isEditor && IsDev)
                        {
                            BuildSettingsCheck(path);
                            SceneManager.LoadScene(path);
                        }
                        else
                        {
                            string bundleName        = path + ".unity" + PathRouter.AssetBundleSuffix;
                            var    assetBundleHandle = AssetBundleLoader.Load(bundleName);
                            SceneManager.LoadScene(path);
                            sceneResource.AddDependency(assetBundleHandle);
                        }

                        if (null != SceneResource.Current)
                        {
                            SceneResource.Current.Dispose();
                            AssetSystem.Instance.GarbageCollect();
                        }

                        SceneResource.Current = sceneResource;
                        AssetSystem.Instance.AddAsset(sceneResource);
                    }
                    catch (Exception e)
                    {
                        sceneResource.Dispose();
                        throw new ApplicationException("Error when loading Scene:" + path, e);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static byte[] Load(string path)
        {
            using (var timer = LoadTimer.Start <BytesLoader>(path))
            {
                string fullpath;
                var    loc = PathRouter.GetFullPath(path, false, out fullpath);
                if (loc == PathLocation.NotFound)
                {
                    throw new FileNotFoundException("BytesLoader", path);
                }

                ResLog.VerboseFormat("BytesLoader.Load, loc:{0}, fullpath:{1}", loc, fullpath);

                if (loc == PathLocation.InApp && Application.platform == RuntimePlatform.Android)
                {
                    return(AndroidPlugin.GetAssetBytes(path));
                }
                else
                {
                    return(File.ReadAllBytes(fullpath));
                }
            }
        }
Ejemplo n.º 7
0
        protected static T AutoNew <T>(string url, OnProgress onProgress)
            where T : ResourceLoader, new()
        {
            var            typeDict = GetTypeDict(typeof(T));
            ResourceLoader loader;

            if (!typeDict.TryGetValue(url, out loader))
            {
                loader       = new T();
                loader.Url   = url;
                loader.timer = LoadTimer.Start <T>(url);
                typeDict.Add(url, loader);
            }
            ++loader.RefCount;
            if (null != onProgress)
            {
                loader.onProgress += onProgress;
            }
            if (null == loader.task)
            {
                loader.task = loader.AsyncRun();
            }
            return((T)loader);
        }