unity3d在菜单栏,一键设置Player setting及自动打包并设置apk的存储位置

项目进行中,领导要求能够进行一键设置Player settings及自动打包并设置apk的位置,所以自己就上网搜索了很多大神的文章.最后是完成了领导需要的功能,在这里记录并分享一下(此项指针对安卓apk)

1.设置Player settings

 参考1.1:https://docs.unity3d.com/ScriptReference/PlayerSettings.html(大家可以去看一下unity提供的API,下面只写一下,我用到的部分)

private static void SetBwPlayerSetting(string bundleIdentifier,string keyStoreName,string keystorePass="123456") {
    PlayerSettings.companyName = COMPANYNAME;
    PlayerSettings.productName = PRODUCTNAME;
    PlayerSettings.applicationIdentifier = bundleIdentifier;// "com.playbyone.chyz.bw";
    PlayerSettings.bundleVersion = BUNDLEVERSION;
    PlayerSettings.Android.bundleVersionCode = BUNDLEVERSIONCODE;

    PlayerSettings.Android.keystoreName = keyStoreName;
    PlayerSettings.keystorePass = keystorePass;
}    

上述代码能够改变图中红色框选的部分

2.更改Plugins-->Android下的sdk,因为,不同的渠道使用不同的sdk文件

参考2.1:https://www.2cto.com/kf/201601/487911.html  (写的非常好的文件夹及文件的操作,我就不贴上自己写的不是太好的代码了,雨松大神下面的参考4.1里面也有文件夹的操作,喜欢的朋友可以自行选择操作)

3.检测Plugins-->Android下的sdk渠道更改完成

参考3.1:http://vr.99.com/news/01102017/002437218.shtml

参考3.2:http://www.ceeger.com/Script/AssetPostprocessor/AssetPostprocessor.OnPostprocessAllAssets.html

(也是参考了上述的文章,帮助很大,并且在参考3.1的基础上完成了自己的需求)

 

4.检测到文件已是最新的之后,开始执行打包命令

参考4.1:http://www.360doc.com/content/16/0326/11/7014874_545353578.shtml

(照例是看了雨松大神的打包,然后发现shell什么的完全不会,然后截选了里面自己能够使用的代码)

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using UnityEditor;
  6 using UnityEngine;
  7 
  8 
  9 public class BwUnityScripsCompiling:AssetPostprocessor {
 10 
 11     //private static List BuildApk = new List();
 12     private static bool BuildApk = false;
 13     //private static bool BuildFinish = false;
 14 
 15     public static void StartBuildApk()
 16     {
 17         BuildApk = true;
 18         //BuildFinish = false;
 19         return;
 20     }
 21     /// 
 22     /// 进行update绑定
 23     /// 
 24     static BwUnityScripsCompiling()
 25     {
 26         EditorApplication.update += Update;
 27     }
 28     /// 
 29     /// 检测导入.删除,移动,资源,使用 PlayerPrefs.SetInt进行存储
 30     /// 
 31     /// 
 32     /// 
 33     /// 
 34     /// 
 35     public static void OnPostprocessAllAssets(
 36     String[] importedAssets,
 37     String[] deletedAssets,
 38     String[] movedAssets,
 39     String[] movedFromAssetPaths) {
 40         List<string> importedKeys = new List<string>() { "Assets/Plugins/Android/StoreSDK" };// 可添加文件夹子集
 41         for (int i = 0; i < importedAssets.Length; i++) {
 42             for (int j = 0; j < importedKeys.Count; j++) {
 43                 if (importedAssets[i].Contains(importedKeys[j])) {
 44                     PlayerPrefs.SetInt("ImportScripts", 1);
 45                     return;
 46                 }
 47             }
 48         }
 49     }
 50     /// 
 51     /// 判断unity编译更新文件是否完成
 52     /// 
 53     private static void Update() {
 54         bool importScripts = Convert.ToBoolean(PlayerPrefs.GetInt("ImportScripts", 1));
 55         if (importScripts && !EditorApplication.isCompiling) {
 56             OnUnityScripsCompilingCompleted();
 57             importScripts = false;
 58             PlayerPrefs.SetInt("ImportScripts", 0);
 59             EditorApplication.update -= Update;
 60         }
 61     }
 62     /// 
 63     /// unity文件编译完成之后,可执行打包动作
 64     /// 
 65     private static void OnUnityScripsCompilingCompleted() {
 66         Debug.Log("Unity Scrips Compiling completed.Start building........");
 67         if (BuildApk == false)
 68             return;
 69         BuildApk = false;
 70         BuildGameBag();
 71     }
 72     /// 
 73     /// 方法一:使用虚拟按键执行打包程序  执行:File-->Buile&Run
 74     /// 
 75     /// 
 76     /// 
 77     /// 
 78     /// 
 79     //[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
 80     //private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
 81     //public static void BuildGameBag()
 82     //{
 83     //    const byte VK_CONTROL = 0x11;
 84     //    const byte VK_B = 0x42;
 85     //    const int KEYEVENTF_DOWN = 0x00;
 86     //    const int KEYEVENTF_KEYUP = 0x02;
 87 
 88     //    keybd_event(VK_CONTROL, 0, KEYEVENTF_DOWN, 0);
 89     //    keybd_event(VK_B, 0, KEYEVENTF_DOWN, 0);
 90     //    keybd_event(VK_B, 0, KEYEVENTF_KEYUP, 0);
 91     //    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
 92     //}
 93 
 94 
 95     //在这里找出你当前工程所有的场景文件,假设你只想把部分的scene文件打包 那么这里可以写你的条件判断 总之返回一个字符串数组。
 96     static string[] GetBuildScenes()
 97     {
 98         List<string> names = new List<string>();
 99         foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
100         {
101             if (e == null)
102                 continue;
103             if (e.enabled)
104             {
105                 names.Add(e.path);
106                 //Debug.LogError("参与打包场景的路径:" + e.path);
107             }
108         }
109         //Debug.LogError("添加的场景的个数为:" + names.Count);
110         return names.ToArray();
111     }
112 
113     /// 
114     /// 方法二:利用unity3d自带API执行打包程序  执行:File-->Build
115     /// 
116     public static void BuildGameBag()
117     {
118         //Debug.LogError("测试自动打包Android::BuildForAndroid1()");
119 
120         PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, "BW");//设置标签,打包的为Android
121         string path = "C:/Users/Administrator/Desktop/Productvivo11.apk";//Path.GetFullPath(".") + "/Product2.apk"; 
122 
123        // Debug.LogError("这是打完之后的包,存放的路径:" + path);
124         BuildPipeline.BuildPlayer(GetBuildScenes(), path, BuildTarget.Android, BuildOptions.None);//开始进行打包......
125 
126        // Debug.LogError("测试自动打包Android::BuildForAndroid2()");
127 
128     }
129 
130 }

5.打包完成之后的检测可进行自己想要的操作

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEditor;
 4 using UnityEditor.Callbacks;
 5 using UnityEngine;
 6 
 7 public static class BwPostProcess  {
 8 
 9 #if UNITY_EDITOR
10     [PostProcessBuild(100)]
11     public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
12     {
13        
14         if (target == BuildTarget.Android)
15         { 
16             //在里面可以进行自己想要的操作
17         }
18     }
19 #endif
20 }

暂时告一段落,并且执行无问题

 

你可能感兴趣的:(unity3d,android,打包)