Example #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);
            }
        }
Example #2
0
 static PrefabResource LoadInEditor(string path, PrefabResource res = null)
 {
     #if UNITY_EDITOR
     string assetPath = string.Format("{0}/{1}.prefab", PathRouter.Res, path);
     if (null == res)
     {
         res = new PrefabResource(path);
     }
     res.prefabObject = UnityEditor.AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(assetPath);
     if (null == res.prefabObject)
     {
         throw new ApplicationException(string.Format(
                                            "UnityEditor.AssetDatabase.LoadAssetAtPath(\"{0}\") => null", assetPath));
     }
     #else
     throw new ApplicationException("Invoke LoadInEditor in release game");
     #endif
     return(res);
 }
Example #3
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);
            }
        }