zoukankan      html  css  js  c++  java
  • iOS开发之与unity生成Xcode工程后执行操作

    我们用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);
            }
        }
    }
  • 相关阅读:
    初识JAVA
    计算机语言发展史
    课时11:禁用、清理二级缓存,以及整合Ehcache缓存
    课时10:MyBatis一级缓存、二级缓存
    课时9::MyBatis整合Log4j、延迟加载
    课时7:动语态SQL、foreach、输入参数为类中的集合属性、集合、数组、动态数组
    课时6 输出参数为简单类型、对象类型、HashMap及resultMap使用
    课时5 入参为HashMap,以及mybatis调用存储过程CRUD
    课时4:l两种取值符号以及ParameterType为简单,对象,嵌套对象类型
    课时3:属性文件丶全局参数丶别名丶类型转换器丶resultMap
  • 原文地址:https://www.cnblogs.com/hecanlin/p/12720142.html
Copyright © 2011-2022 走看看