0评论

Unity命令行打包

清风 2018-09-07 133浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏开发行业精英群711501594

Unity已经自带了可视化打包,而本篇还要多此一举给大家介绍命令行打包,是考虑到刚接触的unity的小伙伴在将游戏发布到Android平台时,需要接不同平台的sdk,甚至有些平台还需要改包名。而游戏从接完平台sdk,到发布还需要经过无数次的测试,发布后甚至还需若干次升级。你要是每次都手动的将每个平台jar、res等,拷贝到unity Plugins文件下,然后再去改包名,多次操作让人心累不说,万一不小心弄错,把什么拷贝错了或者包名写错了忘记改了等等,还要重弄。为了方便大家,才有此篇关于命令行打包的文章,让大家用脚本去解决问题。

一、在Assets\Editor目录下放个脚本
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 命令行批处理工具类
/// </summary>
public class Batchmode {
    static List<string> levels = new List<string>();
    static string keystoreFile = @"D:\keystore.txt";
    public static void BuildAndroid() {
        if(!File.Exists(keystoreFile))
            throw new Exception("Not find keystore file");
        StreamReader sr = File.OpenText(keystoreFile);
        string password = sr.ReadToEnd().Trim();
        PlayerSettings.Android.keystorePass = password;
        PlayerSettings.Android.keyaliasPass = password;
        foreach ( EditorBuildSettingsScene scene in EditorBuildSettings.scenes ) {
            if ( !scene.enabled ) continue;
            levels.Add( scene.path );
        }
		EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
        string res = BuildPipeline.BuildPlayer( levels.ToArray(), "android.apk", BuildTarget.Android, BuildOptions.None );
        if (res.Length > 0)
            throw new Exception("BuildPlayer failure: " + res);
    }
}

二、写个批处理(路径改成自己的工程路径)
@echo off
@set unity="C:\Program Files\Unity\Editor\Unity.exe"
echo 正在生成APK文件...
%unity%  -batchmode -quit -nographics -executeMethod Batchmode.BuildAndroid  -logFile D:\Editor.log -projectPath "D:\China\2014-11-12_client_wushen_android" 
echo APK文件生成完毕!
pause