添加命令

向AutomationTool的自动化项目添加命令的方法。

Choose your operating system:

Windows

macOS

Linux

前置主题

为了理解并使用本文中的内容,请确保您已掌握以下主题:

本页面的内容

以下范例中包含样本命令,在打印出Fibonacci序列后将成功退出。按照此范例步骤,向自动化项目添加新命令(或脚本):

步骤

必要步骤 :继续前确保自动化项目范围中拥有空白 *.cs 源文件。

  1. 首先命名源文件,在本范例中将其命名为 SimpleScript.cs

  2. 现在,将以下代码复制到脚本中:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using AutomationTool;
    
    namespace SampleScript.Automation
    {
        // 使用[Help()]属性记载命令及其参数。
        [Help("Sample script printing a Fibonacci sequence of values.")]
        [Help("Usage:SampleScript -Terms=<N>")]
        [Help("Terms=<N>", "N (int) represents how many terms to compute, and it must be greater than or equal to 1.")]
    
        // BuildCommand是所有命令的基类。
        public class SampleCommand :BuildCommand
        {
            public override void ExecuteBuild()
            {
                // 本范例使用ParseParamInt()方法获取命令参数。ParseParam()获取布尔值,ParseParamValue获取字符串。
                int NumTerms = ParseParamInt("Terms");
                if (NumTerms < 1)
                {
                    throw new AutomationException("Invalid number of terms specified.Enter -help for syntax.");
                }
                else
                {
                    LogInformation("Fibonnacci sequence:");
    
                    int TermA = 1;
                    int TermB = 1;
                    for (int TermIdx = 0; TermIdx < NumTerms; TermIdx++)
                    {
                        LogInformation("  {0}", TermA);
    
                        int NextTerm = TermA + TermB;
                        TermA = TermB;
                        TermB = NextTerm;
                    }
                }
            }
        }
    }
  3. 最后,打开命令行并导航到 Engine\Build\BatchFiles ,然后输入 RunUAT.bat SampleCommand -terms=4

最终结果

应出现以下输出:

AddingaCommand_EndResult.png

欢迎帮助改进虚幻引擎文档!请告诉我们该如何更好地为您服务。
填写问卷调查
取消