Unity-android/iOS自动设置ProjectSetting

工作中碰到由同一个项目,打包中不同包名的游戏apk或ipa的需求,比如项目Trunk1.0,需要打出一个包名为com.xx.xx.a的包,之后可能又需要打出包名为com.xx.xx.b的包。而每切换打新的包,就需要更改 

Product Name,Default Icon,Icon,BundleId,等等(如下图 所示) 

Unity-android/iOS自动设置ProjectSetting_第1张图片
Unity-android/iOS自动设置ProjectSetting_第2张图片

所以为了更有效率打包,写个编辑器一键设置这些参数势在必行。 

写工具前先明白有哪些参数设置是包与包之前不同之处。 

这里针对我这个项目来介绍下:

主要改动的方面:

ProjectSetting

SDK相关

配置表的设计

通过将动态改变的参数放到配置表中 

这里我采取的是Excel来配置,读取using Excel;

`publicclassMenu{publicstringID ="";publicstringbundle_identifier ="";publicstringcompany_name ="";publicstringproduct_name ="";publicstringkeyaliasName ="";publicstringkeyaliasPass ="";publicstringkeystoreName ="";publicstringkeystorePass ="";publicstringWXAppID ="";publicstringscheme ="";}publicclassExcelAccess  {publicstaticstringExcelName ="ProjectSetting.xlsx";publicstaticstring[] SheetNames = {"Sheet1"};publicstaticListSelectMenuTable(inttableId,intappId)    {        DataRowCollection collect = ExcelAccess.ReadExcel(SheetNames[tableId -1]);        List menuArray =newList();for(inti =1; i < collect.Count; i++)        {if(i <3)continue;if(appId !=0)            {if(collect[i][0].ToString() != appId.ToString())continue;            }            Menu menu =newMenu();            menu.ID = collect[i][0].ToString();            menu.bundle_identifier = collect[i][1].ToString();            menu.company_name = collect[i][2].ToString();            menu.product_name = collect[i][3].ToString();            menu.keyaliasName = collect[i][4].ToString();            menu.keyaliasPass = collect[i][5].ToString();            menu.keystoreName = collect[i][6].ToString();            menu.keystorePass = collect[i][7].ToString();            menu.WXAppID = collect[i][8].ToString();            menu.scheme = collect[i][9].ToString();            menuArray.Add(menu);        }returnmenuArray;    }//////读取 Excel 需要添加 Excel; System.Data;/////////staticDataRowCollection ReadExcel(stringsheet)    {        FileStream stream = File.Open(FilePath(ExcelName), FileMode.Open, FileAccess.Read, FileShare.Read);        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);        DataSet result = excelReader.AsDataSet();returnresult.Tables[sheet].Rows;    }staticstringFilePath(stringname)    {stringpath = Application.dataPath +"/ExcelConfig/"+ name;returnpath;    }`

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

修改ProjectSetting

1.这里先介绍我们友好的编辑器界面: 

Unity-android/iOS自动设置ProjectSetting_第3张图片

记录选中的游戏是哪一款,然后用一个唯一id记录下来,当我点击 设置 按钮,把唯一id记录在txt文件:

staticpublicvoidChangeAppId()    {        Debug.Log("--------------------------ChangeAppId--------------------------------------");if(menu !=null)        {stringpath = Application.dataPath +"/Resources/AppId.txt";            FileStream fs =newFileStream(path, FileMode.Create);            StreamWriter sw =newStreamWriter(fs);//开始写入sw.Write(menu.ID);//清空缓冲区sw.Flush();//关闭流sw.Close();            fs.Close();        Debug.Log("【新的AppId】:"+ menu.ID);        }    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

2.根据选中哪个游戏,对应动态修改ProjectSetting

menu为选中这款游戏在配置表里对应的数据 

数据的内容格式: 

Unity-android/iOS自动设置ProjectSetting_第4张图片

PlayerSettings.bundleIdentifier= menu.bundle_identifier;PlayerSettings.companyName= menu.company_name;PlayerSettings.productName= menu.product_name;PlayerSettings.defaultInterfaceOrientation= UIOrientation.LandscapeLeft;

1

2

3

4

Android和iOS在projectSetting设置有些区别(Android设置Device,打包的脚本,签名,而ios没有签名这一项) 

Android上:

static public void SetAndroidProject(Menu menu)    {        if (isFormalPackage)        {            PlayerSettings.Android.targetDevice= AndroidTargetDevice.FAT;PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);}        else        {            PlayerSettings.Android.targetDevice= AndroidTargetDevice.ARMv7;PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.Mono2x);}        PlayerSettings.Android.minSdkVersion= AndroidSdkVersions.AndroidApiLevel9;PlayerSettings.Android.keyaliasName= menu.keyaliasName;PlayerSettings.Android.keyaliasPass= menu.keyaliasPass;PlayerSettings.Android.keystoreName= menu.keystoreName;PlayerSettings.Android.keystorePass= menu.keystorePass;}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

iOS上:

static public void SetIosProject(Menu menu)    {        PlayerSettings.iOS.targetDevice= iOSTargetDevice.iPhoneAndiPad;if (isFormalPackage)        {            PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, ScriptingImplementation.IL2CPP);}        else        {            PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, ScriptingImplementation.Mono2x);}    }

1

2

3

4

5

6

7

8

9

10

11

12

13

3.icon方面的动态更改 

Unity-android/iOS自动设置ProjectSetting_第5张图片

1:ICON 

Unity-android/iOS自动设置ProjectSetting_第6张图片
Unity-android/iOS自动设置ProjectSetting_第7张图片

这里放置美术给的app的icon 

Android: 

Unity-android/iOS自动设置ProjectSetting_第8张图片

iOS上: 

Unity-android/iOS自动设置ProjectSetting_第9张图片

2:LoginImage 对应的图片是default 

icon,图片都是从ICON文件夹里Android:256,iOS:180尺寸拷贝过来的。

publicstaticvoidGenerateLoginImage(RuntimePlatform platform)    {if(menu !=null)        {intappId =int.Parse(menu.ID);//D:\trunk1.0\BackUp\Plugins_Android\1 目录创建stringrootPath = Application.dataPath +"/BackUp/LoginImage/"+ appId;            rootPath = rootPath.Replace("Assets/","");            DirectoryInfo rootDirec =null;if(File.Exists(rootPath) ==false)//创建根目录{                rootDirec = Directory.CreateDirectory(rootPath);            }else{                rootDirec =newDirectoryInfo(rootPath);            }            Debug.Log("【LoginImage】:"+ rootPath);stringplatformName = platform == RuntimePlatform.Android ?"Android":"IOS";stringiconSizePic = platform == RuntimePlatform.Android ?"256.png":"180.png";stringicon_srcPath = Application.dataPath +"/BackUp/ICON/"+ appId +"/"+ platformName +"/"+ iconSizePic;            icon_srcPath = icon_srcPath.Replace("Assets/","");stringicon_animPath = Application.dataPath +"/BackUp/LoginImage/"+ appId+"/Icon.png";            icon_animPath = icon_animPath.Replace("Assets/","");            CopyAndReplace(icon_srcPath, icon_animPath);//app icon}    }privatestaticvoidCopyAndReplace(stringsrcPath,stringanimPath)    {        Debug.Log("从目录:"+ srcPath);        Debug.Log("拷贝到:"+ animPath);        File.Copy(srcPath, animPath,true);    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

3:Share_Image 

微信分享图片:图片按照你自己放置的路径来定 

4:Plugins_Android 

(稍后讲)

我们项目工程已经存在了icon引用了Assets下的某张图片, 


Unity-android/iOS自动设置ProjectSetting_第10张图片

当需要改动这些图片的时候,只需要将目标图片拷贝过来替换掉就可以。

staticpublicvoidReplaceIcon(intappId, RuntimePlatform platform)    {stringplatformName = platform == RuntimePlatform.Android ?"Android":"IOS";stringscrPath = Application.dataPath +"/BackUp/ICON/"+ appId +"/"+ platformName;        scrPath = scrPath.Replace("Assets/","");stringaminPath = Application.dataPath +"/Art/ICON/"+ platformName;        Debug.Log("--------------------------ReplaceIcon--------------------------------------");        Debug.Log("【ReplaceIcon scrPath】="+ scrPath);        Debug.Log("【ReplaceIcon aminPath】="+ aminPath);        FileUtils.CopyDir(scrPath, aminPath);    }

1

2

3

4

5

6

7

8

9

10

11

12

so easy,就是简单的文件或文件夹拷贝

SDK相关

我们游戏接的是微信SDK,这里只讲微信SDK相关

1:Android

观察这个Plugins下的目录 

bin文件夹放置jar包 

res:icon之类 

AndroidManifest.xml:项目的权限配置等

jar包 

需要更改包名 

如图所示:直接手动更改,导出csmj.jar 

Unity-android/iOS自动设置ProjectSetting_第11张图片

微信appid是动态改变的,所以把appid抽出到配置AndroidManifest.xml 

Unity-android/iOS自动设置ProjectSetting_第12张图片

然后代码里动态获得:

publicStringgetWXAppId()    {        ApplicationInfo appInfo;        String WXAppId ="";try{            PackageManager pm =this.getPackageManager();if(pm !=null)            {                appInfo = pm.getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);                WXAppId =appInfo.metaData.getString("WXAppId");                Log.i("Unity","WXAppId="+WXAppId);            }          }catch(NameNotFoundException e)        {            e.printStackTrace();            Log.d("Unity", e.getMessage());        }returnWXAppId;    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

res:

Unity-android/iOS自动设置ProjectSetting_第13张图片

从ICON对应拷贝

//修改Plugins/Android/RespublicstaticvoidRefreshResFolder()    {if(menu !=null)        {intappId =int.Parse(menu.ID);//D:\trunk1.0\BackUp\Plugins_Android\1 目录创建stringrootPath = Application.dataPath +"/BackUp/Plugins_Android/"+ appId;            rootPath = rootPath.Replace("Assets/","");            DirectoryInfo rootDirec =null;if(File.Exists(rootPath) ==false)//创建根目录{                rootDirec = Directory.CreateDirectory(rootPath);            }else{                rootDirec =newDirectoryInfo(rootPath);            }            Debug.Log("目标根目录:"+ rootPath);//D:\trunk1.0\BackUp\Plugins_Android\0 将模板目录下的res文件夹拷贝到目标目录下stringtemplateSrcPath = Application.dataPath +"/BackUp/Plugins_Android/0";            templateSrcPath = templateSrcPath.Replace("Assets/","");            FileUtils.CopyDir(templateSrcPath, rootPath);//把0目录下的内容拷贝到目标目录string[] folders =newstring[] {"drawable","drawable-hdpi","drawable-ldpi","drawable-mdpi","drawable-xhdpi","drawable-xxhdpi","drawable-xxxhdpi"};string[] icons =newstring[] {"96","72","36","48","96","144","192"};string[] newName_icons =newstring[] {"icon","app_icon","app_icon","app_icon","app_icon","app_icon","app_icon"};intfolderLen = folders.Length;for(intfolderIndex =0; folderIndex < folderLen; folderIndex++)            {stringplatformName ="Android";stringicon_srcPath = Application.dataPath +"/BackUp/ICON/"+ appId +"/"+ platformName+"/"+icons[folderIndex]+".png";//D:\trunk1.0\Assets\BackUp\ICON\9\Android\96.pngicon_srcPath = icon_srcPath.Replace("Assets/","");stringicon_animPath = rootPath +"/res/"+ folders[folderIndex]+"/"+ newName_icons[folderIndex] +".png";                CopyAndReplace(icon_srcPath, icon_animPath);            }        }    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

AndroidManifest.xml 

修改要点:替换成新包名,wxappid替换,urlscheme替换

static public void ChangeAndroidXml(int appId, Menu menu)    {        Debug.Log("--------------------------ChangeAndroidXml--------------------------------------");string xmlPath = Application.dataPath+"/Plugins/Android/AndroidManifest.xml";XmlDocument xmlDoc = new XmlDocument();XmlReaderSettingsset= new XmlReaderSettings();set.IgnoreComments= true;//这个设置是忽略xml注释文档的影响。有时候注释会影响到xml的读取XmlReader reader = XmlReader.Create(xmlPath,set);xmlDoc.Load(reader);reader.Close();//最后读取完毕后,记得要关掉reader.//获取bookshop节点的所有子节点        XmlNodeList node = xmlDoc.GetElementsByTagName("manifest");string oldPackageName ="";foreach (XmlElement ninnode)        {            if (n.Name=="manifest")            {                string package = n.Attributes["package"].Value;if (package !="")                {                    Debug.Log("【 旧包名】:"+ package);oldPackageName = package;n.SetAttribute("package", menu.bundle_identifier);}break;}        }        XmlNodeList nodeList = xmlDoc.SelectSingleNode("manifest").ChildNodes;foreach (XmlElement xninnodeList)        {            //Debug.Log("name ="+ xn.Name);if (xn.Name=="application")            {                XmlNodeList target = xn.ChildNodes;foreach (XmlElement xxintarget)                {                    if (xx.Name=="meta-data")//替换微信APPID                    {                        ChangeWXAppId(menu, xx);}                    else if (xx.Name=="activity")//替换APP的scheme                    {                        ChangeAppScheme(menu, xx);if (oldPackageName !="")                        {                            XmlAttributeCollection attributeCollection = xx.Attributes;foreach (XmlAttribute attrinattributeCollection)                            {                                //Debug.Log("attr attr.Name"+ attr.Name+"---attr.Value="+ attr.Value);if (attr.Value.Contains(oldPackageName))                                {                                    //替换掉旧包名                                    string value = attr.Value.Replace(oldPackageName, menu.bundle_identifier);xx.SetAttribute(attr.Name, value);}                            }                        }                    }                }            }        }        xmlDoc.Save(xmlPath);Debug.Log("-----------------------修改 Androidmanifest OK---------------------------");}    private static void ChangeAppScheme(Menu menu, XmlElement ele)    {        Debug.Log("------------------------------------ChangeAppScheme------------------------------");XmlNodeList activityNodes = ele.ChildNodes;foreach (XmlElement xmlNodeinactivityNodes)        {            if (xmlNode.Name=="intent-filter")            {                XmlNodeList intentNodes = xmlNode.ChildNodes;foreach (XmlElement nodeinintentNodes)                {                    //                      if (node.Name=="data")                    {                        if(node.GetAttribute("android:scheme") !="")                        {                            node.SetAttribute("android:scheme", menu.scheme);Debug.Log("【App scheme】:"+ menu.scheme);return;}                    }                }            }        }    }    private static void ChangeWXAppId(Menu menu, XmlElement ele)    {            Debug.Log("------------------------------------替换微信APPID------------------------------");//            if (ele.GetAttribute("android:name") =="WXAppId")        {            ele.SetAttribute("android:value", menu.WXAppID);Debug.Log("【微信APPID 】="+ menu.WXAppID);}    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

至此,Android的修改已经完成。

2.iOS

Unity-android/iOS自动设置ProjectSetting_第14张图片

using System.Collections;using System.Collections.Generic;using System.IO;using UnityEditor;using UnityEditor.Callbacks;#if UNITY_IPHONEusing UnityEditor.iOS.Xcode;#endifusing UnityEngine;#if UNITY_IPHONEpublic class XcodeSetting : MonoBehaviour{    private static List menuList;[PostProcessBuild(999)]    public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)    {        if (BuildTarget == BuildTarget.iOS)        {            Debug.Log(path);string projPath = PBXProject.GetPBXProjectPath(path);PBXProject proj = new PBXProject();proj.ReadFromString(File.ReadAllText(projPath));string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName());proj.SetBuildProperty(target,"ENABLE_BITCODE","NO");//C#读取TXT文件之建立  FileStream 的对象,说白了告诉程序,    //文件在那里,对文件如何 处理,对文件内容采取的处理方式                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;//UrlType                    //Handle plist                      string plistPath = path +"/Info.plist";PlistDocument plist = new PlistDocument();plist.ReadFromString(File.ReadAllText(plistPath));PlistElementDict rootDict = plist.root;rootDict.SetString("NSLocationAlwaysUsageDescription","地理位置ios8后");rootDict.SetString("NSLocationUsageDescription","iOS8之前向用户申请位置信息获取授权");rootDict.SetString("NSLocationWhenInUseUsageDescription","地理位置iOS8后");rootDict.SetString("NSMicrophoneUsageDescription","使用麦克风");rootDict.SetString("NSPhotoLibraryUsageDescription","使用相册");PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");//addweixin url scheme                    PlistElementDict wxUrl = urlTypes.AddDict();wxUrl.SetString("CFBundleTypeRole","Editor");wxUrl.SetString("CFBundleURLName","weixin");PlistElementArray wxUrlScheme = wxUrl.CreateArray("CFBundleURLSchemes");wxUrlScheme.AddString(wxAppid);//addcsmj url scheme                    PlistElementDict appUrl = urlTypes.AddDict();appUrl.SetString("CFBundleTypeRole","Editor");appUrl.SetString("CFBundleURLName","chaoshanmajiang");PlistElementArray appUrlScheme = appUrl.CreateArray("CFBundleURLSchemes");appUrlScheme.AddString(menu.scheme);File.WriteAllText(plistPath, plist.WriteToString());}            }    }}}#endif

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

iOS修改xcode工程,需要在打包完毕后,会自动调用 

[PostProcessBuild(999)] 

public static void OnPostprocessBuild(BuildTarget BuildTarget, string path)

如果代码报错,请在playersetting里 下载ios相关。

其他设置等我以后解锁再添加。Mark一下。

2017-08-07更新 

PS:新增替换外部图片 

using System.Collections; 

using System.Collections.Generic; 

using System.IO; 

using UnityEditor; 

using UnityEditor.Callbacks;

if UNITY_IPHONE

using UnityEditor.iOS.Xcode;

endif

using UnityEngine;

if UNITY_IPHONE

public class XcodeSetting : MonoBehaviour 

private static List

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", "使用相册");

                //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(menu.scheme);

                //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);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

}

endif

你可能感兴趣的:(Unity-android/iOS自动设置ProjectSetting)