【小松教你手遊開發】【unity實用技能】unity發包最佳化(android一鍵發包)

來源:互聯網
上載者:User

標籤:無   手遊開發   

unity本身的發包其實還是挺方便的,國外的遊戲基本都用unity本身的發包。

但在國內的遊戲有這麼多渠道,這個迭代的速度的情況下,就需要一套更高效的發包方式。

接下來講具體步驟,如果你們項目有熱更新會更麻煩一點。

發包最佳化的目標是做到一鍵發包,一般發包機會是一台獨立的機子,所以

第一步,更新svn

第二步,配置打包資訊。根據不同渠道接入不同sdk

第三步,build apk。

因為我們項目暫時還是測試,所以還沒做根據不同渠道接入不同sdk。具體思路是寫個xml,在上面填寫各種配置資訊,版本號碼,接入sdk名字等。這個會在之後項目做到後面補上

而且我們項目熱更新。需要把代碼打成dll。所以中間會多幾個步驟

首先要說明一下,因為熱更我們項目需要2個工程,一個解決方案

一個是遊戲主工程(所有用戶端都在用),一個打包工程(用來打包,只有初始化代碼,和編輯器打包代碼)。一個把遊戲代碼打成dll的解決方案

1.更新遊戲主工程代碼svn

2.copy遊戲主工程代碼到打dll的解決方案目錄下

3.解決方案打dll

4.dll copy到打包工程下。

5.把dll用TextAsset開啟並達成AssetBundle包

6.更新打包工程svn

7.配置打包資訊。根據不同渠道接入不同sdk

8.build apk

接下來是具體實現。

因為是一鍵打包,所有上面所有步驟都需要可以用代碼調用。所以外面操作都用.bat執行檔案操作

1.更新svn

首先寫更新svn的.bat代碼

@echo. ----------開始------------  set SVN="C:\Program Files\TortoiseSVN\bin\TprtoiseProc.exe"  %SVN% /command:update /path:"E:\xxx\Scripts" /closeonend:1  @echo. ----------完成------------  

調用.bat檔案的代碼

ProcessStartInfo pInfo = new ProcessStartInfo (fileName,String.Empty);//fileName就是你的.bat檔案的絕對路徑名  pInfo.UseShellExecute = true;  pInfo.CreateNoWindow = true;  pInfo.WorkingDirectory = Application.data.Path.Remove(Application.dataPath.LastIndexOf(‘/‘));  Process process = Process.Start(pInfo);  process.WaitForExit();  process.Close();  
2.copy遊戲主工程代碼到打dll的解決方案目錄下

這個就是copy,沒什麼要說的。

3.解決方案打dll

這裡也要用.bat檔案執行,就不用開啟vs再build

@echo. ----------開始------------  set devenv = "D:\VS2015\common7\IDE\devenv.exe"  %devenv% E:\xxxx\GameLib.sln /rebuild  @if not %errorlevel% == 0 echo ------失敗!!!---  @if not %errorlevel% == 0 pause  @if %errorlevel% == 0 echo. -------成功------  
4.dll copy到打包工程下。

也是copy,沒什麼好說的

5.dll用TextAsset開啟並打成AssetBundle包

這裡需要重點解釋一下。為什麼dll要用TextAsset開啟不直接就在之後用dll呢

1.因為要做熱更,代碼用dll來做,但是dll在遊戲中下載後不能放到Plugin目錄下,放在StreamingAsset目錄下的dll又不能直接用。所以要用反射開啟放到記憶體中

2.反射介面Assembly.Load(byte[] bytes) bytes可以從textAsset.bytes中擷取,所以就把dll讀取成TextAsset並儲存下來成AssetBundle,更新bundle包後用TextAsset開啟就能直接用

解釋一下流程:

1.把dll複製一份備用

File.Copy(path,toPath,true);

2.載入到記憶體中準備打包

AssetDatabase.ImportAsset(toPath);  AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);  TextAsset temp = AssetDatabase.LoadAssetAtPath(toPath,typeof(TextAsset)) as TextAsset;  

3.打包AssetBundle

BuildPipeline.BuildAssetBundle(temp,null,add,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle,target)); 

temp 是步驟2的TextAsset.

6.更新打包工程svn

跟第一個步驟一樣

7.配置打包資訊。根據不同渠道接入不同sdk

這裡我們還沒做,後面會補上

8.build apk
//簡單的根據時間順序起包名  string dateStr = "";  string versionCodeStr = "";  string finalStr = "";  string[] scene = {“Assets/main.unity”};  dateStr = System.dateTime.Now.ToString("yyyyMMdd");  ArrayList fileList = new ArrayList();  DirectoryInfo dir = new DirectoryInfo ("E:/xxx/apk");//apk目錄  FileInfo[] allFiles = dir.GetFiles();  long currentVersionCode =0;  foreach(FileInfo file in allFiles)  {   string fileName = file.Name.Substring(0,12);   if(currentVersionCode < long.Parse(fileName))   currentVersionCode = long.Parse(fileName);  }   if(currentVersionCode != 0 && currentVersionCode .ToString().Substring(0,8) == dateStr)  {   currentVersionCode = long.Parse(currentVersionCode .ToString().Substring(8,4)) +1;  versionCodeStr = currentVersionCode.ToString("0000");    }  else   versionCodeStr = "0101";  finalStr =dir + "/" dateStr + verionCodeStr + ".apk";  BuildPipeline.BuildPlayer(scene,finalStr,BuildTarget.Android,BuildOptions.None);  

最後添加一個編輯器按鈕調用上面這些功能就可以了

【小松教你手遊開發】【unity實用技能】unity發包最佳化(android一鍵發包)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.