码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发之与unity生成Xcode工程后执行操作

时间:2020-04-17 15:20:53      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:generic   stp   mon   omd   public   foreach   color   over   engine   

我们用unity项目生成xcode工程后,我们自己可以做一些xcode的配置工作,比如资源拷贝、引用系统库、修改bulid setting信息等。

然后需要注意的是这个C#文件需要放到Editor目录下面,不然那会报错

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

public class ECKiOSBuild : MonoBehaviour {

        //这个修饰很重要,就是通过这个来表示xcode工程创建之后自动执行此方法
    [PostProcessBuild]
    public static void ECKExecuteAfteriOSBuild(BuildTarget buildTarget, string path)
    {
        if (BuildTarget.iOS == buildTarget)
        {
            //复制资源文件夹
            ECKCopyiOSResource(path);
            //修改工程文件
            ECKModifyPBXProject(path);
        }
    }

    private static void ECKModifyPBXProject(string path)
    {
        string projPath = PBXProject.GetPBXProjectPath(path);
        PBXProject proj = new PBXProject();

        proj.ReadFromString(File.ReadAllText(projPath));
        string target = proj.TargetGuidByName("Unity-iPhone");

        //添加 -ObjC标记
        proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");

        //引用资源文件夹
        string resourceDirectoryPath = "Libraries/Plugins/ECKChatSDK/iOS/Resource";
        ECKAddResourceGroupToiOSProject(path, proj, target, resourceDirectoryPath);

     //引用系统库
     proj.AddFrameworkToProject(target, "libicucore.tbd", false);
//引用资源文件夹,直接添加文件夹,以Create folder references 形式添加
        /*
        string resourcePath = "Libraries/Plugins/ECKChatSDK/iOS/Resource";
        //string fileGuid = proj.AddFolderReference(Path.Combine(path, resourcePath), resourcePath, PBXSourceTree.Source);
        string fileGuid = proj.AddFolderReference(Path.Combine(path, resourcePath), resourcePath);
        proj.AddFileToBuild(target, fileGuid);*/

        //保存工程文件
        File.WriteAllText(projPath, proj.WriteToString());
    }

    //在工程文件添加资源文件夹引用,以 Create Group 形式加到工程文件
    private static void ECKAddResourceGroupToiOSProject(string xcodePath, PBXProject proj, string target, string resourceDirectoryPath)
    {
        string dirFullPath = Path.Combine(xcodePath, resourceDirectoryPath);
        //添加文件引用
        string[] files = Directory.GetFiles(dirFullPath);
        foreach (string s in files)
        {
            string fileExtension = Path.GetExtension(s);
            if (fileExtension.Equals(".DS_Store")) {
                continue;
            }
            string fileName = Path.GetFileName(s);
            string targetFilePath = Path.Combine(resourceDirectoryPath, fileName); 
            string fileGuid = proj.AddFolderReference(targetFilePath, targetFilePath);
            proj.AddFileToBuild(target, fileGuid);
        }
        //添加子文件夹
        string[] subDirectorys = Directory.GetDirectories(dirFullPath);
        foreach (string s in subDirectorys)
        {
            string fileName = Path.GetFileName(s);
            string subDirPath = Path.Combine (resourceDirectoryPath, fileName); 
            string fileExtension = Path.GetExtension(s);
            if (fileExtension.Equals (".xcassets")) {
                string fileGuid = proj.AddFile(subDirPath, subDirPath);
                proj.AddFileToBuild(target, fileGuid);
            } else {
                ECKAddResourceGroupToiOSProject (xcodePath, proj, target, subDirPath);
            }
        }
    }

    //复制ios多语言文件和图片资源
    private static void ECKCopyiOSResource(string path)
    {
        string fromDir = Application.dataPath+"/Plugins/ECKChatSDK/iOS/Resource/";
        string targetDir = path + "/Libraries/Plugins/ECKChatSDK/iOS/Resource/";

        ECKCopyDirectoryFiles (fromDir, targetDir);
    }

    /** 将文件夹下面的所有非.meta文件(不包括子文件夹)复制到iOS工程目录下*/
    private static void ECKCopyDirectoryFiles(string fromDir, string targetDir)
    {
        if (!Directory.Exists(targetDir))
        {
            Directory.CreateDirectory(targetDir);
        }

        //复制所有文件
        string[] files = Directory.GetFiles(fromDir);

        // Copy the files and overwrite destination files if they already exist.
        foreach (string s in files)
        {
            string extension = Path.GetExtension(s);
            if (extension.Equals(".meta")) 
            {
                continue;
            }
            string fileName = Path.GetFileName(s);
            string targetFilePath = Path.Combine(targetDir, fileName);
            File.Copy(s, targetFilePath, true);
        }

        //复制所有子文件夹
        string[] subDirectorys = Directory.GetDirectories(fromDir);

        // Copy the files and overwrite destination files if they already exist.
        foreach (string s in subDirectorys)
        {
            string dirName = Path.GetFileName(s);
            string targetDirPath = Path.Combine(targetDir, dirName);
            ECKCopyDirectoryFiles (s, targetDirPath);
        }
    }
}

 

iOS开发之与unity生成Xcode工程后执行操作

标签:generic   stp   mon   omd   public   foreach   color   over   engine   

原文地址:https://www.cnblogs.com/hecanlin/p/12720142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!