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<string> BuildApk = new List<string>();
 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     /// <summary>
 22     /// 進行update綁定
 23     /// </summary>
 24     static BwUnityScripsCompiling()
 25     {
 26         EditorApplication.update += Update;
 27     }
 28     /// <summary>
 29     /// 檢測導入.刪除,移動,資源,使用 PlayerPrefs.SetInt進行存儲
 30     /// </summary>
 31     /// <param name="importedAssets"></param>
 32     /// <param name="deletedAssets"></param>
 33     /// <param name="movedAssets"></param>
 34     /// <param name="movedFromAssetPaths"></param>
 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     /// <summary>
 51     /// 判斷unity編譯更新文件是否完成
 52     /// </summary>
 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     /// <summary>
 63     /// unity文件編譯完成之后,可執行打包動作
 64     /// </summary>
 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     /// <summary>
 73     /// 方法一:使用虛擬按鍵執行打包程序  執行:File-->Buile&Run
 74     /// </summary>
 75     /// <param name="bVk"></param>
 76     /// <param name="bScan"></param>
 77     /// <param name="dwFlags"></param>
 78     /// <param name="dwExtraInfo"></param>
 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     /// <summary>
114     /// 方法二:利用unity3d自帶API執行打包程序  執行:File-->Build
115     /// </summary>
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 }

暫時告一段落,並且執行無問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM