添加命令

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

Windows
MacOS
Linux
Prerequisite Topics

This page assumes you have prior knowledge of the following topics. Please read them before proceeding.

On this page

以下范例中包含样本命令,在打印出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

Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback