Use environment variables
Use built-in environment variables and define custom environment variables in Unity Build Automation (UBA):
- Built-in variables automatically expose build environment and source control metadata.
- You can set custom environment variables to configure your pre-build and post-build scripts according to your specific needs.
Common built-in environment variables
UBA provides built-in environment variables that are automatically available during builds. These variables offer useful details about the build environment, target settings, and version control information. For example, the following are commonly used built-in environment variables:
Variable name | Description |
---|---|
UNITY_VERSION | The version of Unity the build uses. |
BUILD_TARGET | The target platform, such as Android, iOS, or WebGL. |
BUILD_PATH | The path to the build output directory. |
PROJECT_PATH | The path to the root directory of the Unity project. |
BUILD_NUMBER | The unique identifier for the build. |
GIT_BRANCH | The Git branch currently building. |
GIT_COMMIT | The hash of the current Git commit. |
For a complete list of available environment variables, refer to the environment variables reference documentation.
Set environment variables
Configure environment variables at the Build Target level. This allows you to specify different variables for each build target so you can use tailored configurations for various platforms or environments, such as staging or production.
Define environment variables in UBA:
- Go to the Unity Cloud Dashboard and select your project.
- Select Build Automation > Configurations.
- Select the edit (pencil) icon to open your chosen build target settings.
- Select Advanced Settings.
- Under Environment variables, enter the variable key and value.
- Select Apply and then select Save Changes at the top of the page.
Use environment variables
After you define your environment variables, you can reference them during the build process within Unity scripts or custom pre- and post-build shell scripts.
Note: Ensure the variable keys match exactly because they're case-sensitive.
Access variables in Unity C# scripts
Use the System.Environment
class to retrieve environment variables:
string apiKey = System.Environment.GetEnvironmentVariable("MY_API_KEY");
if (!string.IsNullOrEmpty(apiKey))
{
Debug.Log("API Key loaded successfully.");
}
else
{
Debug.LogWarning("API Key not found.");
}
Access variables in shell scripts
If you use pre-build or post-build shell scripts, you can use the dollar sign prefix to access specific environment variables:
echo "Build Number: $BUILD_NUMBER"
Missing environment variables
If you can't access your environment variables, check the following solutions:
- Check that you correctly defined the environment variables in your Build Target settings.
- Verify that correctly spelled the variables and that they match case-sensitivity.
- Review your build logs for any related warnings or errors.
- Use diagnostic commands in pre- or post-build scripts to check available variables:
- Linux or macOS:
env
- Windows:
set
- Linux or macOS: