打包导出包体文件夹后 自动修改包体内文件文本数据

发表于2019-01-20
评论0 4.2k浏览
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using System.Text.RegularExpressions;
public class AdincubeEditor : Editor
{
    //打包完成后调用该方法
    [PostProcessBuild(1)]
    public static void AfterBuild(BuildTarget target, string pathToBuiltProject)
    {
        Debug.Log("Build Success  " + target + "  " + pathToBuiltProject);
        //打开文件或文件夹
        //System.Diagnostics.Process.Start(pathToBuiltProject);
        if (target == BuildTarget.Android)
        {
            if (!pathToBuiltProject.EndsWith(".apk", System.StringComparison.Ordinal))
            {
                string projectPath = pathToBuiltProject + "/" + PlayerSettings.productName;
                ModifyASXmlByFilePath(projectPath + "/src/main/AndroidManifest.xml");
                ModifyASXmlByFilePath(projectPath + "/Firebase/AndroidManifest.xml");
                ModifyASGradleByFilePath(projectPath + "/build.gradle", projectPath + "/Firebase/build.gradle");
            }
        }
    }
    static void ModifyASXmlByFilePath(string filePath)
    {
        //判断是否存在该文件
        if (File.Exists(filePath))
        {
            //读取文件信息
            StreamReader sr = new StreamReader(filePath);
            string targetStr = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            //正则表达式匹配指定格式字符串
            Regex regex = new Regex("<uses-sdk.*/>");
            Match match = regex.Match(targetStr);
            if (match.Groups.Count > 0)
            {
                //获取匹配到的字符串
                string replaceStr = match.Groups[0].ToString();
                //字符串替换
                string newStr = targetStr.Replace(replaceStr, "");
                //将修改后的文本写入(覆盖原文本)文件
                StreamWriter sw = new StreamWriter(filePath, false);
                sw.Write(newStr);
                sw.Close();
                sw.Dispose();
            }
        }
    }
    static void ModifyASGradleByFilePath(string targetpath, string filePath)
    {
        if (File.Exists(filePath))
        {
            //读取文本
            StreamReader sr = new StreamReader(filePath);
            string targetStr = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            //替换字符串
            targetStr = targetStr.Replace("compile fileTree", "implementation fileTree");
            targetStr = targetStr.Replace("buildToolsVersion '28.0.0'", "buildToolsVersion '28.0.3'");
            //重新写入文本
            StreamWriter sw = new StreamWriter(filePath, false);
            sw.Write(targetStr);
            sw.Close();
            sw.Dispose();
        }
        if (File.Exists(targetpath) && File.Exists(filePath))
        {
            //读取文本A的字符串str1, 替换文本B的字符串str2
            StreamReader targetSr = new StreamReader(targetpath);
            string targetStr = targetSr.ReadToEnd();
            targetSr.Close();
            targetSr.Dispose();
            Regex regex = new Regex(@"'com.android.tools.build:gradle:\d.\d.\d'");
            Match match = regex.Match(targetStr);
            if (match.Groups.Count > 0)
            {
                string modifyStr = match.Groups[0].ToString();
                StreamReader sr = new StreamReader(filePath);
                string targetStr2 = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                Regex regex2 = new Regex(@"'com.android.tools.build:gradle:\d.\d.\d'");
                Match match2 = regex.Match(targetStr2);
                if (match2.Groups.Count > 0)
                {
                    string modifyStr2 = match2.Groups[0].ToString();
                    Debug.Log(modifyStr);
                    Debug.Log(modifyStr2);
                    if (modifyStr != modifyStr2)
                    {
                        string newStr = targetStr2.Replace(modifyStr2, modifyStr);
                        Debug.Log(targetStr2);
                        Debug.Log(newStr);
                        StreamWriter sw = new StreamWriter(filePath, false);
                        sw.Write(newStr);
                        sw.Close();
                        sw.Dispose();
                    }
                }
            }
        }
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: