天天看点

Unity 构建时不导出StreamingAssets内容🍡Follow me

🐉降龙十八掌

  • 🍡Follow me

🍡Follow me

有时候不需要重复的打包StreamingAssets,因为那样太占用构建时间了…💡 我们可以通过Unity 提供的接口:

IPreprocessBuildWithReport、IPostprocessBuildWithReport

处理。思路就是在构建前把

StreamingAssets

文件夹改个名字,等构建结束后再改回来。

//using Microsoft.VisualBasic.Devices;
using System;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace ZYF
{
    /// <summary>
    /// 不构建StreamingAssets内容
    /// </summary>
    public class ZYF_NotBuildStreamingAssets : IPreprocessBuildWithReport, IPostprocessBuildWithReport
    {
        public int callbackOrder { get => -1; }
        const string tempDirName = "test";
        const string streamingAssetsDirName = "StreamingAssets";
        public void OnPreprocessBuild(BuildReport report)
        {
            string sp = Application.streamingAssetsPath;
            string tp = GetTempPath();
            //Computer MyComputer = new Computer();
            //MyComputer.FileSystem.RenameDirectory(sp, tempDirName);
            Debug.Log($"临时修改文件夹名称:{sp}=>{tp}");
            if (sp.Equals(tp) == false)
            {
                System.IO.Directory.Move(sp, tp);
            }

            AssetDatabase.Refresh();
        }
        public void OnPostprocessBuild(BuildReport report)
        {
            string path = GetTempPath();
            string spath = Application.streamingAssetsPath;
            Debug.Log($"需要把:{path} 换回正确的名字:{streamingAssetsDirName}");
            if (spath.Equals(path) == false)
            {
                System.IO.Directory.Move(path, spath);
            }
            AssetDatabase.Refresh();
        }

        private string GetTempPath()
        {
            string path = Application.streamingAssetsPath;
            Debug.Log($"{streamingAssetsDirName}");
            path = path.Replace(streamingAssetsDirName, tempDirName.GetHashCode().ToString());
            return path;
        }
    }
}