天天看點

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;
        }
    }
}