A custom Style Dictionary transformer for Unity UI-Toolkit.
npm install style-dictionary-unity-transformer style-dictionary- Add publish workflow for npm releases
- Add unit tests for all transforms and formats
- Add example project demonstrating usage with Unity
- Figure out handling variables that use CSS
calc()
import StyleDictionary from 'style-dictionary';
import { registerUnityTransforms } from 'style-dictionary-unity-transformer';
// Register all Unity transforms with Style Dictionary
registerUnityTransforms(StyleDictionary);
// Configure and build
const sd = new StyleDictionary({
source: ['tokens/**/*.json'],
platforms: {
unity: {
transformGroup: 'unity',
buildPath: 'Assets/Styles/',
files: [{
destination: 'tokens.uss',
format: 'unity/uss-variables'
}]
}
}
});
await sd.buildAllPlatforms();import StyleDictionary from 'style-dictionary';
import { registerUnityTransforms, getUnityConfig } from 'style-dictionary-unity-transformer';
registerUnityTransforms(StyleDictionary);
const sd = new StyleDictionary({
source: ['tokens/**/*.json'],
platforms: {
unity: getUnityConfig({
buildPath: 'Assets/UI/Styles/',
fileName: 'design-tokens',
selector: ':root',
outputReferences: true
})
}
});
await sd.buildAllPlatforms();This transformer supports the Design Tokens Community Group (DTCG) format:
{
"color": {
"primary": {
"$value": "#3366FF",
"$type": "color"
},
"secondary": {
"$value": "{color.primary}",
"$type": "color"
}
},
"spacing": {
"small": {
"$value": "8px",
"$type": "dimension"
},
"medium": {
"$value": "16px",
"$type": "dimension"
}
},
"shadow": {
"card": {
"$value": {
"offsetX": "0px",
"offsetY": "4px",
"blur": "8px",
"color": "rgba(0, 0, 0, 0.1)"
},
"$type": "shadow"
}
}
}The transformer generates Unity Style Sheets with CSS custom properties:
/**
* Unity Style Sheet (USS) Design Tokens
*
* Auto-generated by Style Dictionary
* Do not edit directly
*/
:root {
/* color */
--color-primary: #3366FF;
--color-secondary: var(--color-primary);
/* spacing */
--spacing-small: 8px;
--spacing-medium: 16px;
/* shadow */
--shadow-card: 0px 4px 8px rgba(0, 0, 0, 0.1);
}Converts color values to USS-compatible format:
- Opaque colors →
#RRGGBBhex format - Colors with alpha →
rgba(r, g, b, a)format - Supports hex, rgb, rgba, hsl, hsla input formats
Converts dimension values to USS-compatible px units:
rem/em→px(assumes 16px base)pt→px(1pt ≈ 1.333px)%→ kept as-is (USS supports percentages)- Unitless numbers →
px
Converts shadow tokens to USS text-shadow format:
- Format:
<offset-x> <offset-y> <blur> <color> - Supports multiple shadows (comma-separated)
- Note: USS only supports
text-shadow, notbox-shadow
The unity transform group applies these transforms in order:
attribute/cti- Adds category/type/item attributesname/kebab- Converts to kebab-case variable namescolor/uss- Color value transformationsize/uss- Dimension value transformationshadow/uss- Shadow value transformation
Outputs USS files with CSS custom properties (variables).
| Option | Type | Default | Description |
|---|---|---|---|
selector |
string |
':root' |
CSS selector for the variable block |
outputReferences |
boolean |
true |
Use var() references for linked tokens |
showFileHeader |
boolean |
true |
Include auto-generated file header comment |
fileHeader |
string |
- | Custom file header text |
Outputs TSS (Theme Style Sheet) files for Unity theming. TSS files can import other USS/TSS files and the default Unity theme.
| Option | Type | Default | Description |
|---|---|---|---|
selector |
string |
':root' |
CSS selector for the variable block |
outputReferences |
boolean |
true |
Use var() references for linked tokens |
showFileHeader |
boolean |
true |
Include auto-generated file header comment |
fileHeader |
string |
- | Custom file header text |
importDefaultTheme |
boolean |
true |
Import the default Unity theme |
imports |
string[] |
[] |
Additional USS/TSS files to import |
import StyleDictionary from 'style-dictionary';
import { registerUnityTransforms, getUnityTssConfig } from 'style-dictionary-unity-transformer';
registerUnityTransforms(StyleDictionary);
const sd = new StyleDictionary({
source: ['tokens/**/*.json'],
platforms: {
unityTheme: getUnityTssConfig({
buildPath: 'Assets/UI/Themes/',
fileName: 'CustomTheme',
importDefaultTheme: true,
imports: ['/Assets/Styles/base.uss']
})
}
});
await sd.buildAllPlatforms();/**
* Unity Theme Style Sheet (TSS)
*
* Auto-generated by Style Dictionary
* Do not edit directly
*/
@import url("unity-theme://default");
@import url("/Assets/Styles/base.uss");
:root {
/* color */
--color-primary: #3366FF;
}Unity Style Sheets have some limitations compared to CSS:
- Units: Only
pxand%are supported (norem,em,vw,vh) - Shadows: Only
text-shadowis supported (nobox-shadow) - Variables in functions:
var()cannot be used inside functions likergb() - Math: No
calc()or mathematical operations on variables
Registers all Unity USS/TSS transforms, formats, and transform groups with Style Dictionary.
Returns a pre-configured platform configuration object for USS output.
Returns a pre-configured platform configuration object for TSS theme output.
For advanced usage, you can import individual components:
import {
colorUssTransform,
sizeUssTransform,
shadowUssTransform,
ussVariablesFormat,
tssThemeFormat,
unityTransformGroup
} from 'style-dictionary-unity-transformer';