public static void LoadScene(string name) { #if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR SceneManager.LoadScene(name); #else Debug.LogFormat("XLoader.LoadScene loading: {0}", name); AssetBundleManager.Instance.ReleaseSceneCachedBundleOnSceneSwitch(); XManifest.Scene info = XManifest.Instance.GetScene(name); if (info != null) { XManifest.Pack packInfo = XManifest.Instance.GetPack(info.bundle); switch (packInfo.location) { case XManifest.Location.Resource: Debug.LogErrorFormat("XLoader.LoadScene can't load bundled scene {0} in resource!", name); break; default: Debug.LogErrorFormat("[XLoader] can't load scene in sync load {0}", name); break; } } else { SceneManager.LoadScene(name); } #endif }
private static IEnumerator _DoLoadSceneAsync(string name, System.Action <bool> callback) { #if UNITY_EDITOR && !USE_BUNDLE_IN_EDITOR yield return(SceneManager.LoadSceneAsync(name)); #else Debug.LogFormat("XLoader.LoadScene loading: {0}", name); XManifest.Scene info = XManifest.Instance.GetScene(name); if (info != null) { Debug.LogFormat("XLoader.LoadScene bundle loading: {0}", name); XManifest.Pack packInfo = XManifest.Instance.GetPack(info.bundle); switch (packInfo.location) { case XManifest.Location.Resource: Debug.LogErrorFormat("XLoader.LoadScene can't load bundled scene {0} in resource!", name); break; default: yield return(AssetBundleManager.Instance.LoadScene(info)); break; } } else { Debug.LogFormat("XLoader.LoadScene native loading: {0}", name); yield return(SceneManager.LoadSceneAsync(name)); } #endif string loadedName = SceneManager.GetActiveScene().name; bool success = loadedName == name ? true : false; Debug.LogFormat("XLoader.LoadScene {0} loaded", loadedName); callback(success); }
private IEnumerator _DoLoadScene(XManifest.Scene info) { AssetBundleInfo bundleInfo = GetAssetBundleInfo(info.bundle); if (!bundleInfo.isDone) { yield return(CoroutineHelper.Run(_LoadAssetBundleAsync(bundleInfo))); } if (bundleInfo.isDone) { bundleInfo.IncRef(Time.time); yield return(SceneManager.LoadSceneAsync(info.name)); bundleInfo.DecRef(); } }
public static void GenerateFileAllocationTable(BuildTarget buildTarget) { string targetName = XManifest.GetPlatformFolder(buildTarget); string bundlePath = assetBundleOutputPath; EditorUtility.DisplayProgressBar("Generate Manifest", "loading bundle manifest", 1 / 2); AssetBundle bundle = AssetBundle.LoadFromFile(Path.Combine(bundlePath, targetName)); if (bundle == null) { Debug.LogError("Build Resrouce First."); EditorUtility.ClearProgressBar(); return; } XManifest.Instance.Clear(); try { string manifestSavePath = XManifest.manifestOutputPath; // gen packs AssetBundleManifest manifest = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); string[] bundles = manifest.GetAllAssetBundles(); EditorUtility.DisplayProgressBar("Generate Manifest", "compute bundle hash", 0); for (int i = 0; i < bundles.Length; i++) { string bundleName = bundles[i]; XManifest.Pack pack = new XManifest.Pack(); pack.name = bundleName; uint checksum = 0; uint size = 0; GetFileInfo(Path.Combine(bundlePath, bundleName), out checksum, out size); pack.checksum = checksum; pack.size = size; pack.location = XManifest.Location.Streaming; //pack.preloadType = XManifest.PreloadType.Loading; List <string> dependencies = new List <string>(manifest.GetAllDependencies(bundleName)); dependencies.Remove(bundleName); pack.dependencies = dependencies.ToArray(); XManifest.Instance.Add(pack); foreach (string file in AssetDatabase.GetAssetPathsFromAssetBundle(bundleName)) { if (Path.GetExtension(file) == ".unity") // gen scenes { XManifest.Scene scene = new XManifest.Scene(); scene.name = Path.GetFileNameWithoutExtension(file); scene.bundle = bundleName; XManifest.Instance.Add(scene); } else if (Path.GetExtension(file) == ".spriteatlas") // gen sporitepack { var name = Path.GetFileNameWithoutExtension(file); if (XManifest.Instance.GetSpritePack(name) != null) { throw new System.Exception("can't have mutip spriteatlas named:" + file); } List <string> allPackable = new List <string>(); var sa = AssetDatabase.LoadAssetAtPath <UnityEngine.U2D.SpriteAtlas>(file); var textureNameMap = new Dictionary <string, bool>(); foreach (var pa in UnityEditor.U2D.SpriteAtlasExtensions.GetPackables(sa)) { var assetPath = AssetDatabase.GetAssetPath(pa.GetInstanceID()); assetPath = assetPath.Replace(Defines.AssetBundleSourcePath + "/", null); assetPath = Path.ChangeExtension(assetPath, null); allPackable.Add(assetPath); } XManifest.SpritePack spritePack = new XManifest.SpritePack(); spritePack.name = name; spritePack.packable = allPackable.ToArray(); var realName = file.Replace(Defines.AssetBundleSourcePath + "/", null).ToLower().Replace("\\", "/"); if (System.IO.Path.HasExtension(realName)) { realName = System.IO.Path.ChangeExtension(realName, null); } spritePack.realName = realName; XManifest.Instance.Add(spritePack); } } EditorUtility.DisplayProgressBar("Generate Manifest", "compute bundle hash", i / (float)bundles.Length); } // record luas List <string> luaFiles = new List <string>(); GetAllFile(new DirectoryInfo(Defines.LuaByteCodeOutPath), luaFiles); foreach (string fileName in luaFiles) { string name = Path.GetFileNameWithoutExtension(fileName); if (!XManifest.Instance.Exists(name)) { XManifest.File file = new XManifest.File(); file.name = name; file.location = XManifest.Location.Streaming; uint checksum = 0; uint size = 0; GetFileInfo(fileName, out checksum, out size); file.checksum = checksum; file.size = size; XManifest.Instance.Add(file); } else { Debug.LogError("[GenerateFileAllocationTable] repeat name:" + name); } } // gen rules BundleNameHelper.initPackRules(); foreach (BundlePackRule rule in BundleNameHelper.rules) { XManifest.Instance.AddBundleRule(rule); } GameConfig.VersionInfo version = new GameConfig.VersionInfo(VersionCode); XManifestSave(manifestSavePath, true); Debug.Log("[GenerateFileAllocationTable] Mainfest 生成成功:" + manifestSavePath); } catch (System.Exception e) { Debug.LogException(e); } EditorUtility.DisplayProgressBar("Generate Manifest", "success!", 1.0f); bundle.Unload(true); EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); }
public Coroutine LoadScene(XManifest.Scene info) { return(CoroutineHelper.Run(_DoLoadScene(info))); }