Unity iOS 省心打包

作者: 2b75747cf703 | 来源:发表于2016-01-29 15:31 被阅读6503次

    http://www.jianshu.com/p/dbd7c4b205b0 Unity iOS 省心打包(二)

    #if UNITY_IOS
    
    using UnityEngine;
    
    using UnityEditor;
    using UnityEditor.Callbacks;
    using System.IO;
    using UnityEditor.iOS.Xcode;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    public static class BuildiOSPlayer
    {
        [PostProcessBuild]
        static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)
        {
            Debug.Log(pathToBuiltProject);
    
            EditProj(pathToBuiltProject);
            EditInfoPlist(pathToBuiltProject);
            EditUnityAppController(pathToBuiltProject);
        }
    
        static void EditProj(string pathToBuiltProject)
        {
            string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
    
            PBXProject pbxProj = new PBXProject();
            pbxProj.ReadFromFile(projPath);
    
            string targetGuid = pbxProj.TargetGuidByName("Unity-iPhone");
            //string debugConfig = pbxProj.BuildConfigByName(target, "Debug");
            //string releaseConfig = pbxProj.BuildConfigByName(target, "Release");
            pbxProj.SetBuildProperty(targetGuid, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
            //pbxProj.SetBuildPropertyForConfig(debugConfig, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
            //pbxProj.SetBuildPropertyForConfig(releaseConfig, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
    
            pbxProj.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", true);
            pbxProj.AddFrameworkToProject(targetGuid, "StoreKit.framework", true);
            pbxProj.AddFrameworkToProject(targetGuid, "AVKit.framework", true);
    
            pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libsqlite3.dylib", "Frameworks/libsqlite3.dylib", PBXSourceTree.Sdk));
            pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libz.dylib", "Frameworks/libz.dylib", PBXSourceTree.Sdk));
    
            //pbxProj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Frameworks");
            //pbxProj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
    
            //pbxProj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
    
    
            #region 添加资源文件(中文路径 会导致 project.pbxproj 解析失败)
            string frameworksPath = Application.dataPath + "/Frameworks";
            string[] directories = Directory.GetDirectories(frameworksPath, "*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < directories.Length; i++)
            {
                string path = directories[i];
    
                string name = path.Replace(frameworksPath + "/", "");
                string destDirName = pathToBuiltProject + "/" + name;
    
                if (Directory.Exists(destDirName))
                    Directory.Delete(destDirName, true);
    
                Debug.Log(path + " => " + destDirName);
                Utility.CopyDirectory(path, destDirName, new string[] { ".meta", ".framework", ".mm", ".c", ".m", ".h", ".xib", ".a", ".plist", ".org", "" }, false);
    
                foreach (string file in Directory.GetFiles(destDirName, "*.*", SearchOption.AllDirectories))
                    pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile(file, file.Replace(pathToBuiltProject + "/", ""), PBXSourceTree.Source));
            }
            #endregion
    
            pbxProj.WriteToFile(projPath);
        }
    
        static void EditInfoPlist(string filePath)
        {
            string path = filePath + "/Info.plist";
    
            PlistDocument plistDocument = new PlistDocument();
            plistDocument.ReadFromFile(path);
    
            PlistElementDict dict = plistDocument.root.AsDict();
    
            PlistElementArray array = dict.CreateArray("CFBundleURLTypes");
            PlistElementDict dict2 = array.AddDict();
            dict2.SetString("CFBundleURLName", PlayerSettings.bundleIdentifier);
            PlistElementArray array2 = dict2.CreateArray("CFBundleURLSchemes");
            array2.AddString(PlayerSettings.bundleIdentifier);
    
            dict2 = array.AddDict();
            dict2.SetString("CFBundleURLName", "weixin");
            array2 = dict2.CreateArray("CFBundleURLSchemes");
            array2.AddString(BabybusConst.WEIXIN_ID);
    
            dict2 = array.AddDict();
            dict2.SetString("CFBundleURLName", "");
            array2 = dict2.CreateArray("CFBundleURLSchemes");
            array2.AddString("QQ" + BabybusConst.QQ_ID.ToString("X"));
    
            dict2 = array.AddDict();
            dict2.SetString("CFBundleURLName", "");
            array2 = dict2.CreateArray("CFBundleURLSchemes");
            array2.AddString("tencent" + BabybusConst.QQ_ID);
    
    
            #region quick action
            string[] quickActions = { "Poem", "Pet", "Movie", "Telephone" };
            string[] quickActionsIcon = { "PoemIcon", "PetIcon", "MovieIcon", "TelephoneIcon" };
            //string[] icons = { "UIApplicationShortcutIconTypeBookmark", "UIApplicationShortcutIconTypeLove", "UIApplicationShortcutIconTypeCaptureVideo", "UIApplicationShortcutIconTypeFavorite" };
            array = dict.CreateArray("UIApplicationShortcutItems");
            for(int i=0; i<quickActions.Length; ++i)
            {
                dict2 = array.AddDict();
                //dict2.SetString("UIApplicationShortcutItemIconType", icons[i]);
                dict2.SetString("UIApplicationShortcutItemIconFile", quickActionsIcon[i]);
                dict2.SetString("UIApplicationShortcutItemTitle", quickActions[i] + "Title");
                dict2.SetString("UIApplicationShortcutItemType", quickActions[i]);
                dict2.CreateDict("UIApplicationShortcutItemUserInfo");
                //dict2.SetString("UIApplicationShortcutItemSubtitle", quickActions[i]);
            }
            #endregion
    
            dict.SetString("CFBundleIdentifier", PlayerSettings.bundleIdentifier);
    
            var assetInfos = Utility.DeserializeXmlFromFile<List<AssetInfo>>(Application.dataPath + "/Resources/配置/APP.xml");
            array = dict.CreateArray("LSApplicationQueriesSchemes");
            foreach (var assetInfo in assetInfos)
            {
                if (string.IsNullOrEmpty(assetInfo.bundleIdentifier4iOS))
                    array.AddString(assetInfo.extra);
                else
                    array.AddString(assetInfo.bundleIdentifier4iOS);
            }
    
            plistDocument.WriteToFile(path);
        }
    
        static void EditUnityAppController(string pathToBuiltProject)
        {
            string unityAppControllerPath = pathToBuiltProject + "/Classes/UnityAppController.mm";
            if (File.Exists(unityAppControllerPath))
            {
                string headerCode = "#include \"../Libraries/Plugins/iOS/SDKPlatformIOS.h\"\n" +
                                    "#import <AVFoundation/AVAudioSession.h>\n\n";
                string unityAppController = headerCode + File.ReadAllText(unityAppControllerPath);
    
                Match match = Regex.Match(unityAppController, @"- \(void\)startUnity:\(UIApplication\*\)application\s+\{[^}]+\}");
                if(match.Success)
                {
                    string newCode = match.Groups[0].Value.Remove(match.Groups[0].Value.Length - 1);
                    newCode += "\n" +
                               "    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];\n" +
                               "    [[AVAudioSession sharedInstance] setActive:YES error:nil];\n" +
                               "}\n\n" +
                               "- (void)application:(UIApplication*)application performActionForShortcutItem: (UIApplicationShortcutItem*)shortcutItem completionHandler: (void(^)(BOOL))completionHandler\n" +
                               "{\n" +
                               "    [[SDKPlatform share] performActionForShortcutItem:shortcutItem];\n" +
                               "}";
                    unityAppController = unityAppController.Replace(match.Groups[0].Value, newCode);
                }
    
                File.WriteAllText(unityAppControllerPath, unityAppController);
            }
        }
    }
    
    #endif
    
        public static void CopyDirectory(string srcPath, string dstPath, string[] excludeExtensions, bool overwrite = true)
        {
            if (!Directory.Exists(dstPath))
                Directory.CreateDirectory(dstPath);
    
            foreach (var file in Directory.GetFiles(srcPath, "*.*", SearchOption.TopDirectoryOnly).Where(path => excludeExtensions == null || !excludeExtensions.Contains(Path.GetExtension(path))))
            {
                File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)), overwrite);
            }
    
            foreach (var dir in Directory.GetDirectories(srcPath))
                CopyDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)), excludeExtensions, overwrite);
        }
    

    相关文章

      网友评论

      • 10394fcb25fa:- (void)startUnity:(UIApplication*)application
        {
        ...
        }

        您好,使用@"- \(void\)startUnity:\(UIApplication\*\)application\s+\{{FNXX==XXFN}+\}"匹配不到此方法内match.Success = false....是什么原因呢
        ssdfsj:@ChaosDev 请问你改好了吗?对正则表达式不熟,startUnity方法匹配的表达式应该是什么
        10394fcb25fa:@千里伏骥 嗯,谢谢
        2b75747cf703:这个可能是手误,你参考下其他的代码就懂了
      • LiGoEX:请问Utility这个类在哪?
        2b75747cf703:@ChaosDev http://www.jianshu.com/p/6fbbea26dde2
        10394fcb25fa:@千里伏骥 能把使用的CopyDirectory方法帖下么
        2b75747cf703:@LiGoEX 这个类代码不好看就没发出去了
      • 丨W:请问AddFileToBuild 之后要添加的文件夹是蓝色的 我要添加为group该怎么做
        2b75747cf703:@丨W http://www.jianshu.com/p/dbd7c4b205b0 你想要的应该是添加.xcassets
      • 五蕴盛:你好,老师,怎么样才能把Unity3d的项目整合到我的iOS工程里边呢,里面用一个按钮控制Unity的展示与退出,类似于3d动画。我一点都找不到门路。。。求救了
        五蕴盛:@五蕴盛 我那边直接放弃了😂,兄弟,多保重😳
        Mory:@五蕴盛 你弄得怎么样了,我也碰到这样的问题。整合进去以后不报错了,但是运行时就出错,dyld: Symbol not found: __ZN14Il2CppIUnknown3IIDE,Google了一下别人说要么将il2cpp脚本格式换成mono2x格式的导出,要么是什么下载补丁。你那边怎么样了?看看能不能相互帮忙
        2b75747cf703:@五蕴盛宋帅杰 http://stackoverflow.com/questions/8336250/unity3d-integration-with-uiview

      本文标题:Unity iOS 省心打包

      本文链接:https://www.haomeiwen.com/subject/hsmjkttx.html