先上打包代碼
public class BuildAssetBundle
{
[MenuItem("打包/場(chǎng)景")]
public static void BuildScenesWindows()
{
BuildScenes();
}
// 打包Scenes
private static void BuildScenes()
{
// 指定場(chǎng)景文件夾和輸出路徑
string scenePath = Application.dataPath + "/scene";
string outPath = Application.streamingAssetsPath + "/";
if (Directory.Exists(scenePath))
{
// 創(chuàng)建輸出文件夾
if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
// 查找指定目錄下的場(chǎng)景文件
string[] scenes = GetAllFiles(scenePath, "*.unity");
for (int i = 0; i < scenes.Length; I++)
{
string url = scenes[i].Replace("\\", "/");
Debug.Log(url);
int index = url.LastIndexOf("/");
string scene = url.Substring(index + 1, url.Length - index - 1);
string msg = string.Format("打包場(chǎng)景{0}", scene);
EditorUtility.DisplayProgressBar("信息", msg, 0f);
scene = scene.Replace(".unity", ".scene");
Debug.Log(string.Format("打包場(chǎng)景{0}到{1}", url, outPath + scene));
BuildPipeline.BuildPlayer(scenes, outPath + scene, EditorUserBuildSettings.activeBuildTarget, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh();
}
EditorUtility.ClearProgressBar();
Debug.Log("所有場(chǎng)景打包完畢");
}
}
/// <summary> 獲取文件夾和子文件夾下所有指定類型文件 </summary>
private static string[] GetAllFiles(string directory, params string[] types)
{
if (!Directory.Exists(directory)) return new string[0];
string searchTypes = (types == null || types.Length == 0) ? "*.*" : string.Join("|", types);
string[] names = Directory.GetFiles(directory, searchTypes, SearchOption.AllDirectories);
return names;
}
}
再上加載代碼
public class AppStart : MonoBehaviour
{
string AB_path = Application.streamingAssetsPath+"/"+ "test.scene";
// Start is called before the first frame update
void Start()
{
DontDestroyOnLoad(this.gameObject);
AssetBundle AB_PACK = AssetBundle.LoadFromFile(AB_path);
SceneManager.LoadScene("test");
}
// Update is called once per frame
void Update()
{
}
}
過(guò)程中遇到的問(wèn)題
- 天空盒子丟失
原因:場(chǎng)景打包成assetbundle的時(shí)候补箍,unity不會(huì)打包一些自帶相關(guān)的資源
解決辦法:使用第三方天空盒子
2.shader丟失
原因:場(chǎng)景打包成assetbundle的時(shí)候,unity不會(huì)打包一些自帶相關(guān)的資源
解決辦法:在project settings>Graphics下設(shè)置添加要使用的shader
3.場(chǎng)景一些烘培的貼圖丟失
原因:場(chǎng)景打包成assetbundle的時(shí)候,unity不會(huì)打包一些自帶相關(guān)的資源
解決辦法:在project settings>Graphics下設(shè)置