您当前的位置: 首页 >  unity
  • 3浏览

    0关注

    157博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 构建时不导出StreamingAssets内容

牙膏上的小苏打2333 发布时间:2021-08-23 15:30:38 ,浏览量:3

🐉降龙十八掌
  • 🍡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
{
    /// 
    /// 不构建StreamingAssets内容
    /// 
    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;
        }
    }
}
关注
打赏
1664520285
查看更多评论
立即登录/注册

微信扫码登录

0.1460s