Unity:自动化打包脚本

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的也写完了,你还可以翻译成其他版本的,不懂的地方欢迎留言交流。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值