IOS自动化打包,自动导入并修改文件

近几日学习IOS自动化打包,特此记录下:

 

 

#if UNITY_IOS || UNITY_IPHONE

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.iOS.Xcode;
using UnityEditor.Callbacks;
using UnityEditor;
using System.IO;
using UnityEditor.XCodeEditor;

public static class XcodePost
{
   [PostProcessBuild(700)]
   public static void OnPostProcessBuild(BuildTarget target,string pathToBuildProject)
    {
        if (target != BuildTarget.iOS)
            return;

        //修改属性文件
        var projPath = pathToBuildProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        var proj = new PBXProject();
        proj.ReadFromFile(projPath);
        var targetGuid = proj.TargetGuidByName("Unity-iPhone");
        proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "YES");
        proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
        proj.AddFrameworkToProject(targetGuid, "libz.tbd", false);
        proj.AddFrameworkToProject(targetGuid, "libresolv.tbd", false);
        proj.AddFrameworkToProject(targetGuid, "libsqlite3.tbd", false);
        proj.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false);
        //proj.AddFrameworkToProject(targetGuid, "HeroStatistics.framework", true);
        proj.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);

        proj.WriteToFile(projPath);


        //复制framework包
        CopyAndReplaceDirectory("Assets/lib/HeroStatistics.framework", Path.Combine(pathToBuildProject, "Frameworks/HeroStatistics.framework"));
        proj.AddFileToBuild(targetGuid, proj.AddFile("Frameworks/HeroStatistics.framework", "Frameworks/HeroStatistics.framework", PBXSourceTree.Source));


        //修改infolist文件
        string plistPath = pathToBuildProject + "/Info.plist";
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));
        PlistElementDict rootDict = plist.root;

        PlistElementDict pedic = rootDict.CreateDict("HeroConfigInfo");
        pedic.SetString("channelname", "IOS");
        plist.WriteToFile(plistPath);

        //修改代码文件
        string unityAppControllerPath = pathToBuildProject + "/Classes/UnityAppController.mm";
        XClass UnityAppController = new XClass(unityAppControllerPath);

        string importStr = "\n" + "#import <HeroStatistics/HeroStatisticsManager.h>";
        UnityAppController.WriteBelow("#import <OpenGLES/ES2/glext.h>", importStr);

        string codeStr = "\n" +
                        "-(void)viewDidLoad {\n" +
                        "[[HeroStatisticsManager sharedInstance] initConfig];\n" +
                        "NSLog(@\" Init Sdk ----- !!\");\n}";

        UnityAppController.WriteBelow("- (void)preStartUnity               {}", codeStr);

        string insetStr = "\n" + "    [self viewDidLoad];\n";

        UnityAppController.WriteBelow("UnitySetPlayerFocus(1);", insetStr);
    }


    internal static void CopyAndReplaceDirectory(string srcPath, string dstPath)
    {
        //路径下该文件夹若存在,则删除
        if (Directory.Exists(dstPath))
        {
            Directory.Delete(dstPath);
        }
        //路径下的文件若存在,则删除
        if (File.Exists(dstPath))
        {
            File.Delete(dstPath);
        }
        //创建该路径下文件夹
        Directory.CreateDirectory(dstPath);
        Debug.Log(dstPath + "----" + srcPath);

        foreach (var file in Directory.GetFiles(srcPath))
        {
            Debug.Log(Path.Combine(dstPath, Path.GetFileName(file)));
            File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));
        }


        foreach (var dir in Directory.GetDirectories(srcPath))
            CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)));
    }

}

#endif

 

class代码如下:

using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace UnityEditor.XCodeEditor
{
    public partial class XClass : System.IDisposable
    {

        private string filePath;

        public XClass(string fPath)
        {
            filePath = fPath;
            if (!System.IO.File.Exists(filePath))
            {
                Debug.LogError(filePath + "路径下文件不存在");
                return;
            }
        }


        public void WriteBelow(string below, string text)
        {
            StreamReader streamReader = new StreamReader(filePath);
            string text_all = streamReader.ReadToEnd();
            streamReader.Close();

            int beginIndex = text_all.IndexOf(below);
            if (beginIndex == -1)
            {
                Debug.LogError(filePath + "中没有找到标致" + below);
                return;
            }

            int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length);

            text_all = text_all.Substring(0, endIndex) + "\n" + text + "\n" + text_all.Substring(endIndex);

            StreamWriter streamWriter = new StreamWriter(filePath);
            streamWriter.Write(text_all);
            streamWriter.Close();
        }

        public void Replace(string below, string newText)
        {
            StreamReader streamReader = new StreamReader(filePath);
            string text_all = streamReader.ReadToEnd();
            streamReader.Close();

            int beginIndex = text_all.IndexOf(below);
            if (beginIndex == -1)
            {
                Debug.LogError(filePath + "中没有找到标致" + below);
                return;
            }

            text_all = text_all.Replace(below, newText);
            StreamWriter streamWriter = new StreamWriter(filePath);
            streamWriter.Write(text_all);
            streamWriter.Close();

        }

        public void Dispose()
        {

        }
    }
}

 

xplist

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace UnityEditor.XCodeEditor
{
    public partial class XCPlist : System.IDisposable
    {

        private string filePath;
        List<string> contents = new List<string>();
        public XCPlist(string fPath)
        {
            filePath = Path.Combine(fPath, "info.plist");
            if (!System.IO.File.Exists(filePath))
            {
                Debug.LogError(filePath + "路径下文件不存在");
                return;
            }

            FileInfo projectFileInfo = new FileInfo(filePath);
            StreamReader sr = projectFileInfo.OpenText();
            while (sr.Peek() >= 0)
            {
                contents.Add(sr.ReadLine());
            }
            sr.Close();

        }
        public void AddKey(string key)
        {
            if (contents.Count < 2)
                return;
            contents.Insert(contents.Count - 2, key);

        }

        public void ReplaceKey(string key, string replace)
        {
            for (int i = 0; i < contents.Count; i++)
            {
                if (contents[i].IndexOf(key) != -1)
                {
                    contents[i] = contents[i].Replace(key, replace);
                }
            }
        }

        public void Save()
        {
            StreamWriter saveFile = File.CreateText(filePath);
            foreach (string line in contents)
                saveFile.WriteLine(line);
            saveFile.Close();
        }

        public void Dispose()
        {

        }
    }
}

 

posted @ 2020-07-24 15:29  你这只母牛  Views(1083)  Comments(0Edit  收藏  举报