文档

​
​

Development

User Acquisition

Monetization

工业

Unity Cloud

Get Started

Asset Manager SDKs and services

Open Unity Dashboard

Unity Cloud

此页面不支持所选语言。
​
​
Unity Dashboard
  • Accounts
  • Organizations
  • Projects
  • Products
  • Billing
Developer Data
  • Introduction to the Developer Data Framework
  • Get started
  • View Diagnostics for your project
  • Privacy and consent
Unity Asset Manager
  • What's new
  • Asset Manager web
  • Asset Store
  • Manage Assets in the Editor
  • Unity Version Control
  • Developer documentation
Unity Pipeline Automation
  • About
  • Get started
  • Pipelines
    • Create a pipeline
    • Run a pipeline
    • Edit a pipeline
    • Delete a pipeline
    • Pipeline parameters
    • Steps
    • Custom Scripts
      • Use Custom Scripts
      • System parameters
      • Reference parameters
      • Coding patterns
  • Automations
  • Monitor jobs
  1. Unity Cloud Documentation
  2. Unity Pipeline Automation (beta)
  3. Custom Scripts (beta)

Coding patterns

Use coding best practices to ensure reliable and maintainable Custom Scripts.
阅读时间2 分钟
最后更新于 10 个月前

You can use the below coding patterns to improve script reliability, prevent data overwrites, and make pipeline execution easier to debug and maintain.

Use unique file paths for each step’s output file

Custom Scripts enables you to write output data files to the job’s storage, so that subsequent steps in the pipeline can access them. For example, your script can generate a
.json
artifact that another step can parse. When you write output files to the job storage, use file paths unique to that step’s execution to prevent the files from being overwritten. This issue can occur if your script runs multiple times in a pipeline. Use the
{{system.stepId}}
parameter to avoid this.

Use error codes

Use logs and exit codes to ensure your scripts catch errors. This helps your pipeline users understand the issue. You can use the
exit
command to stop script execution and return an exit code. The exit code can be an integer in the range
0–255
, this helps you identify why the script execution stopped. An exit code of
0
indicates that the execution was successful.

Python Code example

This is as example of a Custom Script that demonstrates how to save pipeline execution data to a JSON file. This code example shows the following:
  • Pattern for logging
  • Referencing system parameters
  • Getting all pipeline parameters
  • Pattern for saving output data
  • Pattern for error handling
import jsonimport logging_LOG_PREFIX = "[My Custom Script]"logging.basicConfig( level=logging.INFO, format=f"{_LOG_PREFIX} - %(asctime)s - %(levelname)s - %(message)s", datefmt="%Y/%m/%d %H:%M:%S")logger = logging.getLogger(__name__)def _save_output(): """Saves output data to a JSON file.""" output_file = '/workspace/{{system.stepId}}/output.json' logger.info(f"Saving the following output data to {output_file}:") # If there are no parameters {{inputs.parameters}} evaluates to "null" # In Python you can handle this by converting "null" to None pipeline_params = {{inputs.parameters}} if """{{inputs.parameters}}""" != "null" else None output = { "step_id": "{{system.stepId}}", "job_id": "{{system.jobId}}", "random_uuid": "{{system.newUUID}}", "organization_id": "{{system.organizationId}}", "pipeline_params": pipeline_params, } logger.info(output) with open(output_file, 'w') as f: json.dump(output, f, ensure_ascii=False, indent=4)if __name__ == "__main__": logger.info("My custom script initialized.") try: _save_output() except Exception as e: logger.error(f"Action failed: {e}") exit(1)

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • Use unique file paths for each step’s output file

    • Use error codes

    • Python Code example


报告此页面的问题