场景打包AssetBundle注意事项

先上打包代码

public class BuildAssetBundle
{


    [MenuItem("打包/场景")]
    public static void BuildScenesWindows()
    {
        BuildScenes();
       
    }

    // 打包Scenes
    private static void BuildScenes()
    {
        // 指定场景文件夹和输出路径
        string scenePath = Application.dataPath + "/scene";
        string outPath = Application.streamingAssetsPath + "/";

        if (Directory.Exists(scenePath))
        {
            // 创建输出文件夹
            if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);

            // 查找指定目录下的场景文件
            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("打包场景{0}", scene);
                EditorUtility.DisplayProgressBar("信息", msg, 0f);
                scene = scene.Replace(".unity", ".scene");
                Debug.Log(string.Format("打包场景{0}到{1}", url, outPath + scene));
                BuildPipeline.BuildPlayer(scenes, outPath + scene, EditorUserBuildSettings.activeBuildTarget, BuildOptions.BuildAdditionalStreamedScenes);
                AssetDatabase.Refresh();
            }
            EditorUtility.ClearProgressBar();
            Debug.Log("所有场景打包完毕");
        }
    }

    /// <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()
    {
        
    }
}

过程中遇到的问题

  1. 天空盒子丢失
    原因:场景打包成assetbundle的时候,unity不会打包一些自带相关的资源
    解决办法:使用第三方天空盒子

2.shader丢失
原因:场景打包成assetbundle的时候,unity不会打包一些自带相关的资源
解决办法:在project settings>Graphics下设置添加要使用的shader

image.png

3.场景一些烘培的贴图丢失
原因:场景打包成assetbundle的时候,unity不会打包一些自带相关的资源
解决办法:在project settings>Graphics下设置

image.png

最后感谢大佬
https://www.jianshu.com/p/dcd27cceaac7

https://www.jb51.net/article/157064.htm

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 翻译:莫铭原文地址:AssetBundle usage patterns 本系列中的上一篇文章覆盖了AssetBu...
    莫铭阅读 5,604评论 1 12
  • 这部分主要讨论AssetBundle在具体使用中会遇到的一些常见问题和对应的解决办法。 1. 管理已经加载的Ass...
    Wenchao阅读 1,199评论 0 6
  • 原文地址:http://gad.qq.com/article/detail/7180936作者:Loki+XUni...
    重装机霸阅读 8,515评论 0 41
  • Asset Bundle的作用:1.AssetBundle是一个压缩包包含模型、贴图、预制体、声音、甚至整个场景,...
    _凉笙阅读 63,601评论 3 41
  • 张芝若原创分享232天 几天下来,一直关注着,2020新冠状病毒,的疫情。事情已经发展到如今形式,严重的势头已经呈...
    感恩123阅读 181评论 0 0

友情链接更多精彩内容