首页 > 其他分享 >Unity 导出设置iOS 项目

Unity 导出设置iOS 项目

时间:2023-01-04 18:02:04浏览次数:35  
标签:string iOS SetString 导出 framework Unity proj new plist

别人的代码

xcode打包部分设置的脚本如下
 
public class XcodeSetting : MonoBehaviour
{
    private static List<Menu> menuList;
 
    [PostProcessBuild(999)]
    public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)
    {
        if (BuildTarget == BuildTarget.iOS)
        {
            Debug.Log("OnPostprocessBuild ProjectPath:" + path);
            string projPath = PBXProject.GetPBXProjectPath(path);//获取.xcodeproj文件的路径
 
            PBXProject proj = new PBXProject();//new()一个PBXProject对象,然后从上面获取的路径中读出字符串。
            string contents = File.ReadAllText(projPath);
            proj.ReadFromString(contents);
 
            string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName());//获取targetGUID
 
            // 链接器
            proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");//bitcode是被编译程序的一种中间形式的代码。包含bitcode配置的程序将会在App store上被编译和链接。bitcode允许苹果在后期重新优化我们程序的二进制文件(我们第三方库不一定支持,所以要设置为NO)
            proj.SetBuildProperty (target, "OTHER_LDFLAGS", "-Objc -all_load -lstdc++.6.0.9 -lsqlite3");//Other Linker Flags  在ios开发过程中,有时候会用到第三方的静态库(.a文件),然后导入后发现编译正常但运行时会出现selector not recognized的错误,从而导致app闪退。
 
            //pbxProj.AddBuildProperty(targetGuid, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //pbxProj.AddBuildProperty(targetGuid, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //pbxProj.AddBuildProperty(targetGuid, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/BabyFrameWork/**");
            //------------------------拷贝系统Framework----------------------------------------------
            string xcodePath = Application.dataPath + "/xcode.txt";
            xcodePath = xcodePath.Replace("Assets/", "");
            FileStream xcode_fs = new FileStream(xcodePath, FileMode.Open, FileAccess.Read);
            StreamReader xcode_sr = new StreamReader(xcode_fs);//仅 对文本 执行  读写操作 
            string line = null;
            while((line= xcode_sr.ReadLine())!= null)
            {
                Debug.Log ("framework=" + line);
                string frameWorkName = line.Split('.')[0];
                string filterName = line.Split ('.') [1];
                if (filterName == "framework") {
                    if(frameWorkName == "JavaScriptCore")
                    {
                        proj.AddFrameworkToProject(target, line, true);//这里第一个参数是第三部中获取到的GUID,第二个参数是framework名(这里一定要是.framework为后缀),第三个参数是用来设置framework是require还是optional。
                    }
                    else
                    {
                        proj.AddFrameworkToProject(target, line, false);//这里第一个参数是第三部中获取到的GUID,第二个参数是framework名(这里一定要是.framework为后缀),第三个参数是用来设置framework是require还是optional。
                    }
                }
                else
                {
                    proj.AddFileToBuild (target, proj.AddFile("usr/lib/"+line, "Frameworks/"+line, PBXSourceTree.Sdk));
                }
            }
            //C#读取TXT文件之关上文件,留心顺序,先对文件内部执行 关上,然后才是文件~     
            xcode_sr.Close();
            xcode_fs.Close();
            //--------------------------拷贝系统Framework end-------------------------------------
 
 
            File.WriteAllText(projPath, proj.WriteToString());
 
 
            //------------------------------APPID-----------------------------------------------------
            string txtPath = Application.dataPath + "/Resources/AppId.txt";
            FileStream fs = new FileStream(txtPath, FileMode.Open, FileAccess.Read); 
            StreamReader sr = new StreamReader(fs);//仅 对文本 执行  读写操作 
            int AppId = int.Parse(sr.ReadToEnd());
            //C#读取TXT文件之关上文件,留心顺序,先对文件内部执行 关上,然后才是文件~     
            sr.Close();
            fs.Close();
 
            menuList = ExcelAccess.SelectMenuTable(1, AppId);
            if (menuList != null)
            {
                if (menuList.Count > 0)
                {
                    Menu menu = menuList[0];
 
                    string wxAppid = menu.WXAppID;
                    PlistElementDict dict;
 
                    //UrlType
                    //Handle plist  
                    string plistPath = path + "/Info.plist";
                    PlistDocument plist = new PlistDocument();
                    plist.ReadFromString(File.ReadAllText(plistPath));
                    PlistElementDict rootDict = plist.root;
 
                    //NSContactsUsageDescription->通讯录
                    //NSMicrophoneUsageDescription->麦克风
                    //NSPhotoLibraryUsageDescription->相册
                    //NSCameraUsageDescription->相机
                    //NSLocationAlwaysUsageDescription->地理位置
                    //NSLocationWhenInUseUsageDescription->地理位置
                    rootDict.SetString("NSLocationAlwaysUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSLocationUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSLocationWhenInUseUsageDescription", "地理位置相近的玩家不可进入同一个牌桌");
                    rootDict.SetString("NSMicrophoneUsageDescription", "使用麦克风");
                    rootDict.SetString("NSPhotoLibraryUsageDescription", "使用相册");
                    rootDict.SetString("NSPhotoLibraryAdditionsUsageDescription","需要访问您的相册");
 
                    //PList文件添加微信为白名单
                    PlistElementArray array = rootDict.CreateArray("LSApplicationQueriesSchemes");
                    array.AddString("weixin");
 
                    // 设置支持HTTP
                    dict = rootDict.CreateDict("NSAppTransportSecurity");
                    dict.SetBoolean("NSAllowsArbitraryLoads", true);
 
                    PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");
                    // add weixin url scheme应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装。
                    PlistElementDict wxUrl = urlTypes.AddDict();
                    wxUrl.SetString("CFBundleTypeRole", "Editor");
                    wxUrl.SetString("CFBundleURLName", "weixin");
                    PlistElementArray wxUrlScheme = wxUrl.CreateArray("CFBundleURLSchemes");
                    wxUrlScheme.AddString(wxAppid);
 
                    
                    //add csmj url scheme
                    PlistElementDict appUrl = urlTypes.AddDict();
                    appUrl.SetString("CFBundleTypeRole", "Editor");
                    appUrl.SetString("CFBundleURLName", "chaoshanmajiang");
                    PlistElementArray appUrlScheme = appUrl.CreateArray("CFBundleURLSchemes");
                    appUrlScheme.AddString("elephant"+ menu.ID);
 
 
                    //add location appkey url scheme
                    PlistElementDict locationUrl = urlTypes.AddDict();
                    locationUrl.SetString("CFBundleTypeRole", "Editor");
                    locationUrl.SetString("CFBundleURLName", "locationAppKey");
                    PlistElementArray locationScheme = locationUrl.CreateArray("CFBundleURLSchemes");
                    locationScheme.AddString("xx"+menu.Location_AppKey);
 
                    File.WriteAllText(plistPath, plist.WriteToString());
                }
            }
 
            CopyAndReplaceIcon (path);
        }
    }
 
    static void CopyAndReplaceIcon (string projectPath)
    {
        string targetWXShareIconPath = projectPath + "/SDK/WX/res2.png";
        string sourceIconPath = System.Environment.CurrentDirectory + "/Assets/Art/ICON/IOS/57.png";
        Debug.Log (string.Format ("CopyAndReplaceIcon from {0} to {1}", sourceIconPath, targetWXShareIconPath));
        File.Copy (sourceIconPath, targetWXShareIconPath, true);
    }
}

我的代码

#if UNITY_IOS
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

internal static class XCodePostProcess
{
    [PostProcessBuild(700)]
    public static void OnPostProcessBuild(
        BuildTarget target, string pathToBuiltProject)
    {
        if (target != BuildTarget.iOS)
        {
            return;
        }

    

        var projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        var proj = new PBXProject();
        proj.ReadFromFile(projPath);
        var targetGUID = "";
#if UNITY_2019_3_OR_NEWER
        targetGUID = proj.GetUnityFrameworkTargetGuid();
#else
            targetGUID = proj.TargetGuidByName("Unity-iPhone");
#endif
        proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-ObjC");
        proj.SetBuildProperty(targetGUID, "ENABLE_BITCODE", "NO");

        // framework to project
        proj.AddFrameworkToProject(targetGUID, "WebKit.framework", false);
        proj.AddFrameworkToProject(targetGUID, "VideoToolbox.framework", false);

        proj.WriteToFile(projPath);

        var path = pathToBuiltProject + "/Info.plist";
        var plist = new PlistDocument();
        plist.ReadFromFile(path);
        var root = plist.root;
        root.SetString("NSUserTrackingUsageDescription", "请允许获取您的广告标识权限,您的数据将被用于追踪、分析测评广告效果,以便后续推送更好的个性化服务。");
        root.SetString("NSCameraUsageDescription","是否允许此App访问你的相机进行拍照?");
        root.SetString("NSPhotoLibraryUsageDescription", "是否允许此App访问你的相册?把注册的账号和密码保存到相册中,当您忘记账号和密码可在相册中找回");
        root.SetString("NSPhotoLibraryAddUsageDescription", "是否允许此App访问你的相册?把注册的账号和密码保存到相册中,当您忘记账号和密码可在相册中找回");

        var urlArray = root.CreateArray("CFBundleURLTypes");
        PlistElementDict plistElement = new PlistElementDict();
        plistElement.SetString("CFBundleTypeRole", "Editor");
        //plistElement.SetString("URL identifier", "");
        urlArray.values.Add(plistElement);
        var items = plistElement.CreateArray("CFBundleURLSchemes");

        PlistElementString it = new PlistElementString("item 0");
        it.value = "com.wzhx.sgbxdios";
        items.values.Add(it);
       
        plist.WriteToFile(path);
    }
}

#endif

很多字符是设置和显示不一样具体是啥  NSUserTrackingUsageDescription 可以先在Xcode 中设置好,然后用文本编辑器查看 info.list 文件 就可以找到真实的 字符

标签:string,iOS,SetString,导出,framework,Unity,proj,new,plist
From: https://www.cnblogs.com/unity-android-ios/p/17025616.html

相关文章

  • Swift基础之如何使用iOS 9的Core Spotlight框架
    本文由​​CocoaChina​​译者KingOfOnePiece(博客)翻译作者:GABRIELTHEODOROPOULOS?校对:hyhSuper原文:​​HowToUseCoreSpotlightFrameworkiniOS9​​iOS每一次版本的......
  • iOS一个公司能上架多少个APP
    跨平台开发是软件开发中一个重要的概念,即不依赖于操作系统,也不信赖硬件环境。一个操作系统下开发的应用,放到另一个操作系统下依然可以运行。诸如java,delphi和易语言,都已......
  • [unity 2d 2021]官方小狐狸项目OnTriggerEnter2D函数问题
    防止遗忘在给小狐狸添加boxcollider和circlecollider时运用OnTriggerEnter2D函数碰撞到Cherry和Gem的时候有时候会出现两次增加原因在于两个collider有时同时碰撞......
  • Unity+Pico环境配置
    1、下载并解压PICOUnityIntegrationSDK 2、新建Unity项目并导入PICOUnityIntegrationSDK点击Window-->PackageManager,在PackageManager界面点击“+”号按钮,选......
  • iOS不上架怎么安装
    ​当我们开发完app后,需要将ipa/apk提交给测试人员测试,然后才能上线。下面就来看一下如何打包ipa文件,并上传托管平台。​做过ios开发的童鞋都知道,ios为开发者提供了描述文件......
  • 【iOS入门】新建 workspace
    熟悉VisualStudio(微软IDE)朋友可能知道,新建工程时会首先创建一个solution,然后 project会归属于这个solution。其实Xcode的workspace就类似于VisualStudio的so......
  • unity 热重载方式加载 dll
    1.代码示例voidLoadDll(){//Unity版本对于LoadDll的影响//2019就算在运行时修改了dll,也是无效的,拿的还是上一次的dll//2020unity认......
  • ios16 横竖屏切换
        [selfsetNeedsUpdateOfSupportedInterfaceOrientations];    NSArray*array=[[[UIApplicationsharedApplication]connectedScenes]allObjects......
  • unity3d脚本之鸡零狗碎
    这里记录的是unity3d开发中用到的鸡零狗碎的代码片段。1、键盘方向键移动对象publicclassKeyboardMove:MonoBehaviour{privatefloatspeed=5f;voidFixedUpdate......
  • unity3d,android平台下,高德地图定位,定位模式设定
    接上一篇,有时候不是想偷懒,实在是不会用androidstudio再次打包啊。高德地图定位androidsdk中,设置定位模式,java代码如下://设置定位模式为高精度模式,Battery_Saving为低功耗......