Unity的项目手动打包确实很痛苦,得慢慢等着,好无聊,能自动化脚本打包什么的最好了,闲话不多说直接上脚本。
`
windows的bat脚本
echo off
set companyName="公司名字"
set productName="游戏名字"
set platform=0 //这个是平台编号
set outPath="打包输出路径 例如:E:\1\build"
set arg0="参数1,谁便定义吧"
set icon="游戏图标路径,例如:E:\2\icon.png"
set assetresource="游戏的Resources目录,例如:E:\1\game\Assets\Resources"
set font="E:\2\data\resource\font\simkai.ttf"
xcopy %icon% %assetresource% /Y
xcopy %font% %assetresource% /Y
echo "packaging......"
start /wait D:\unity5.3\Unity\Editor\Unity.exe -quit -batchmode -projectPath "E:\1\game" -executeMethod ProjectBuild.BuildPackage %companyName% %productName% %platform% %outPath% %arg0%
echo "build package done"
pause
`
C#代码
`
public class ProjectBuild : Editor {
private static string[] s;
static void BuildPackage()
{
s = System.Environment.GetCommandLineArgs();
var target = s[s.Length - 3];
if (target.Equals("0")) //windows
{
BuildForStandaloneWindows();
}
else if (target.Equals("1")) //android
{
BuildForAndroid();
}
else if (target.Equals("2")) //ios
{
BuildForIOS();
}
//其他平台自己看着写就行
s = null;
}
static void BuildForAndroid()
{
string[] levels = {"Assets/Scenes/1.unity"};
string path= GetExportPath(BuildTarget.Android);
BuildPipeline.BuildPlayer(levels, path, BuildTarget.Android, BuildOptions.None);
}
static void BuildForIOS()
{
string[] levels = { "Assets/Scenes/1.unity" };
string path = GetExportPath(BuildTarget.iOS);
BuildPipeline.BuildPlayer(levels, path, BuildTarget.iOS, BuildOptions.None);
}
[MenuItem("a/b")]
static void BuildForStandaloneWindows()
{
string[] levels = { "Assets/Scenes/1.unity" };
string path = GetExportPath(BuildTarget.StandaloneWindows);
PlayerSettings.defaultIsFullScreen = false;
PlayerSettings.companyName = companyName;
PlayerSettings.productName = productName;
PlayerSettings.resizableWindow = false;
PlayerSettings.displayResolutionDialog = ResolutionDialogSetting.Disabled;
var tx = Resources.Load<Texture2D>("icon");
Texture2D[] txs = { tx, tx, tx, tx, tx, tx, tx,};
var lengt = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Standalone);
if(txs!=null)
PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Standalone, txs);
var st= BuildPipeline.BuildPlayer(levels,path, BuildTarget.StandaloneWindows, BuildOptions.None);
}
private static string GetExportPath(BuildTarget target)
{
//var s = System.Environment.GetCommandLineArgs();
string path = s[s.Length - 2];
string name = string.Empty;
if (target == BuildTarget.Android)
{
name = "/" + s[s.Length - 1] + ".apk";
}
else if (target == BuildTarget.iOS)
{
name = "/" + s[s.Length - 1];
}
else if (target == BuildTarget.StandaloneWindows)
{
name = "/" + s[s.Length - 1] + ".exe";
}
UnityEngine.Debug.Log(path);
UnityEngine.Debug.Log(name);
string exepath = @path + name;
UnityEngine.Debug.Log(exepath);
return exepath;
}
private static string companyName{
get {
return s[s.Length - 5];
}
}
private static string productName {
get {
return s[s.Length - 4];
}
}
}
`
看到这儿你可能会问bat脚本在其他系统怎么破,额,额,额,好尴尬,那就来个python版本的吧,如下:
`
#!/user/bin/python
#coding=utf-8
import subprocess
import shutil
import os
UNITYPATH="D:/unity5.3/Unity/Editor/Unity.exe"
MAPPROJECT="E:1/game"
companyName="公司名字"
productName="游戏名字"
platform="0"
outPath="E:/1/game/build"
arg0="参数谁便定义"
icon="E:/1/icon.png"
assetresource="E:/1/game/Assets/Resources"
font="E:/data/font/simkai.ttf"
print "package start......"
shutil.copy(icon,assetresource)
print "复制"+icon+"到 "+assetresource
shutil.copy(font,assetresource)
print "复制"+font+"到"+assetresource
print "packing......"
subprocess.call(UNITYPATH+" -quit "+" -batchmode "+" -projectPath "+MAPPROJECT+" -executeMethod ProjectBuild.BuildPackage "+companyName+" "+productName+" "+platform+" "+outPath+" "+arg0)
print "build package done"
`
好了python的也写完了,你还可以翻译成其他版本的,不懂的地方欢迎留言交流。