During the build process, Unity pre-processes template files and evaluates all macros and conditional directives included in those files. As part of this process, Unity finds and replaces all macro declarations with the values the Unity Editor supplies. Unity automatically pre-processes all .html, .php, .css, .js and .json files in the template folder.
The following internal preprocessor variables refer to data in the Project, and Unity assigns value to them at build time according to the values the Editor supplies. JavaScript macros and conditional directives can use these internal preprocessor variables.
Variable | Type | Description |
---|---|---|
COMPANY_NAME | String | The Company Name defined in the Player SettingsSettings that let you set various player-specific options for the final game built by Unity. More info See in Glossary. |
PRODUCT_NAME | String | The Product Name defined in the Player Settings. |
PRODUCT_VERSION | String | The Version defined in the Player Settings. |
WIDTH | Integer | The Default Canvas Width defined in the Player Settings > Resolution and Presentation. |
HEIGHT | Integer | The Default Canvas Height in the Player Settings > Resolution and Presentation. |
SPLASH_SCREEN_STYLE | String | This is set to the “Dark” value when Splash Style Player Settings > Splash Image is set to Light on Dark, otherwise it’s set to the “Light” value. |
BACKGROUND_COLOR | String | Represents the Background Color defined in a form of a hex triplet. |
UNITY_VERSION | String | The Unity version. |
DEVELOPMENT_PLAYER | Boolean | This is set to true if the Development BuildA development build includes debug symbols and enables the Profiler. More info See in Glossary option is enabled. |
DECOMPRESSION_FALLBACK | String | This is set to Gzip or Brotli , depending on the compressionA method of storing data that reduces the amount of storage space it requires. See Texture Compression, Animation Compression, Audio Compression, Build Compression.See in Glossary method you use and the type of decompressor included in the build. If neither is included, the variable is set to an empty string. |
INITIAL_MEMORY | Integer | The initial size of the WASM memory heap in megabytes (MB). |
USE_WASM | Boolean | This is set to true if the current build is a WebAssembly build. |
USE_THREADS | Boolean | This is set to true if the current build uses threads. |
USE_WEBGL_1_0 | Boolean | This is set to true if the current build supports the WebGL1.0 graphics API. |
USE_WEBGL_2_0 | Boolean | This is set to true if the current build supports the WebGL2.0 graphics API. |
USE_DATA_CACHING | Boolean | This is set to true if the current build uses indexedDB caching for the downloaded files. |
LOADER_FILENAME | String | This is set to the file name of the build loader script. |
DATA_FILENAME | String | This is set to the file name of the main data file. |
FRAMEWORK_FILENAME | String | This is set to the file name of the build framework script. |
CODE_FILENAME | String | This is set to the file name of the WebAssembly module when the current build is a WebAssembly build, otherwise it’s set to the file name of the asm.js module. |
MEMORY_FILENAME | String | This is set to the file name of the memory file when memory is stored in an external file, otherwise it’s set to an empty string. |
SYMBOLS_FILENAME | String | This is set to the file name of the JSON file containing debug symbols when the current build is using debug symbols, otherwise it’s set to an empty string. |
BACKGROUND_FILENAME | String | This is set to the file name of the background image when the background image is selected in the Player Settings > Splash Image, otherwise it’s set to an empty string. |
JavaScript macros are blocks of JavaScript code in template files, surrounded by three sets of curly brackets. This JavaScript code can use the internal preprocessor variables listed above. These variables are assigned at build time according to the values the Editor supplies. During the build, the preprocessor evaluates all macros and replaces them with the output of the variable.
You can use JavaScript macros to preprocess values supplied by the Editor. These macros can be as complex as you like. They can include multiple operators, loops, functions, and any other JavaScript constructs.
The following example line is from the index.html
file used in the Default template:
<div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
If the value of the Product Name in the Player Settings is set to My WebGL Game
, the internal preprocessor variable PRODUCT_NAME
has the value My WebGL Game
. In the output index.html
file, the line appears as:
<div id="unity-build-title">My WebGL Game</div>
Below is a more complex example from the same index.html
template file:
canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
If the target build folder is called Let’s try WebGL
, and if you select a background image in the Player Settings, the internal preprocessor variable BACKGROUND_FILENAME
has the value Let’s try WebGL.jpg
. In the output index.html
file, the line changes into:
canvas.style.background = "url('" + buildUrl + "/Let%27s try WebGL.jpg') center / cover";
Conditional directives #if, #else, and #endif control whether Unity includes a specific portion of the preprocessed file in the output file, or discards it for the current build.
Code that starts with an #if directive and ends with an #endif directive is called a conditional group. Conditional groups can also include #else directives. Unity evaluates the expression written after the #if as a JavaScript expression. If this expression has a truthy value that translates to true when evaluated in a Boolean context, then Unity keeps the line group immediately following the #if directive in the output file. If the #if expression is false, and an #else directive is included in the conditional group, Unity keeps the line group immediately following the #else directive in the output. An example of a conditional group is as follows:
#if EXPRESSION
// this block is included in the output if EXPRESSION has a truthy value
#else
// this block is included in the output otherwise
#endif
Evaluated JavaScript expressions can include brackets, logical operators and other JavaScript constructs. Conditional directives can be nested.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.