美文网首页
场景打包AssetBundle注意事项

场景打包AssetBundle注意事项

作者: pawn_c | 来源:发表于2020-02-27 11:41 被阅读0次

    先上打包代码

    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

    相关文章

      网友评论

          本文标题:场景打包AssetBundle注意事项

          本文链接:https://www.haomeiwen.com/subject/ialthhtx.html