Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        protected override async Task AsyncRun()
        {
            PrefabResource prefabResource = new PrefabResource(Url);

            try
            {
                if (IsDev)
                {
                    prefabResource = LoadInEditor(Url, prefabResource);
                    Progress       = 1f;
                }
                else
                {
                    string bundleName        = Url + ".prefab" + PathRouter.AssetBundleSuffix;
                    var    assetBundleHandle = await AssetBundleLoader.AsyncLoad(
                        bundleName, v => Progress = v);

                    prefabResource.prefabObject = assetBundleHandle.Bundle.LoadAsset(
                        Path.GetFileNameWithoutExtension(Url));
                    if (null == prefabResource.prefabObject)
                    {
                        throw new ApplicationException(string.Format(
                                                           "AssetBundle.LoadAsset({0}) => null, Bundle:{1}",
                                                           Path.GetFileNameWithoutExtension(Url),
                                                           assetBundleHandle.Path));
                    }
                    prefabResource.AddDependency(assetBundleHandle);
                }
                AssetSystem.Instance.AddAsset(prefabResource);
                Finish(prefabResource);
            }
            catch (Exception e)
            {
                Finish(null);
                prefabResource.Dispose();
                throw new ApplicationException("Error when loading Prefab:" + Url, e);
            }
        }