https://blog.csdn.net/qq_15697801/article/details/80003488
打包
using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; public class AssetBundleTest : MonoBehaviour { [MenuItem("Custom Editor/AssetBundle/Bundle Scene")] static void CreateSceneALL() { //清空一下缓存 Caching.CleanCache(); List<BundleData> strmes = GetSelectionFile(".unity"); Debug.Log(strmes.Count); if (strmes.Count <= 0) return; string Path=string.Empty; string filepath = PlayerPrefs.GetString("Path"); foreach (var item in strmes) { //获得用户选择的路径的方法,可以打开保存面板(推荐) if(string.IsNullOrEmpty(Path)) { if(string.IsNullOrEmpty(filepath)) { Path = EditorUtility.SaveFilePanel("保存资源", "SS", "" + item.Name, "unity3d"); filepath = Path.Substring(0, Path.LastIndexOf('/')); } else { Path = filepath+ "/"+item.Name+ ".unity3d"; } PlayerPrefs.SetString("Path", filepath); } //另一种获得用户选择的路径,默认把打包后的文件放在Assets目录下 //string Path = Application.dataPath + "/MyScene.unity3d"; //选择的要保存的对象 string[] levels = {item.LevelsPath }; //打包场景 BuildPipeline.BuildPlayer(levels, Path, BuildTarget.StandaloneWindows64, BuildOptions.BuildAdditionalStreamedScenes); } // 刷新,可以直接在Unity工程中看见打包后的文件 AssetDatabase.Refresh(); if(!string.IsNullOrEmpty(filepath)) { System.Diagnostics.Process.Start(filepath); Debug.Log("Bundle Success!!!"); } } [MenuItem("Custom Editor/AssetBundle/Clear FilePath")] static public void ClearFilePath() { PlayerPrefs.DeleteKey("Path"); Debug.Log("默认地址清除成功!" + PlayerPrefs.GetString("Path")); } //得到鼠标选中的文件,只返回文件名及后缀 static public string[] GetSelectionFile() { //SelectionMode.Unfiltered 返回整个选择。 //SelectionMode.TopLevel 仅返回最顶层选择的变换;其他选择的子对象将被过滤出。 //SelectionMode.Deep 返回选择和所有选择的子变换。 //SelectionMode.ExcludePrefab 从选择中排除任何预制。 //SelectionMode.Editable 排除任何对象不得修改。 //SelectionMode.Assets 仅返回资源目录中的资源对象。 //SelectionMode.DeepAssets 如果选择包含文件夹,也包含所有资源和子目录。 //SelectionMode.OnlyUserModifiable 仅用户可修改?? UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel); string[] str = new string[arr.Length]; for (int i = 0; i < arr.Length; ++i) { string s = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[i]); Debug.Log(s); str[i] = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1); Debug.Log(str[i]); } return str; } //得到鼠标选中的文件,验证后缀返回文件名 static public List<BundleData> GetSelectionFile(string suffix) { UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel); // string[] str = new string[arr.Length]; List<BundleData> liststr = new List<BundleData>(); for (int i = 0; i < arr.Length; ++i) { string s = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[i]); Debug.Log(s); var strName = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1); if (strName.EndsWith(suffix)) { // var file = AssetDatabase.GetAssetPath(arr[i]); // str[i] = GetString(strName, suffix); // Debug.Log(str[i]); BundleData strdata = new BundleData(); strdata.AllName = strName; strdata.Name = GetString(strName, suffix); strdata.Suffix = suffix; strdata.FilePath = s; strdata.LevelsPath= AssetDatabase.GetAssetPath(arr[i]); liststr.Add(strdata); } } return liststr; } ///<summary> /// 截前后字符(串) ///</summary> ///<param name="val">原字符串</param> ///<param name="str">要截掉的字符串</param> ///<param name="all">是否贪婪</param> ///<returns></returns> private static string GetString(string val, string str, bool all=true) { return Regex.Replace(val, @"(^(" + str + ")" + (all ? "*" : "") + "|(" + str + ")" + (all ? "*" : "") + "$)", ""); } } public class BundleData{ public string AllName;//名字带后缀 public string Name;//名字不带后缀 public string Suffix;//后缀 public string FilePath;//全路径 public string LevelsPath;//工程内路径 }
加载
/// <summary> /// 真的场景加载方式 /// </summary> /// <returns></returns> public IEnumerator LoadReallyScene( string path) { // path= path.Replace("/","\\"); // !isnewload ? (AppDefine.Bundle_Root_Env + GlobalConfig.Instance.SceneName + ".prefab") : (ModelManager.Instance.GetRelativeDirUrl(RelativeDirEnum.Model) + GlobalConfig.Instance.SceneName + ".prefab"); WWW bundle = new WWW(path); yield return bundle; if (bundle.error == null) { AssetBundle ab = bundle.assetBundle; //将场景通过AssetBundle方式加载到内存中 AsyncOperation asy = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); //sceneName不能加后缀,只是场景名称 yield return asy; OnSceneLoadEnd(); //执行回调 } else { Debug.LogError(bundle.error); } }