USS を使用する場合、UI コード内のビルトインの VisualElement
プロパティの値や カスタムプロパティの値を指定できます。
USS ファイルからそれらの値を読み込むことに加えて、ビルトインのプロパティ値を VisualElement
の C# プロパティを使用して C# で割り当てることができます。C# で割り当てられた値は、Unity スタイルシート (USS) から値をオーバーライドします。
カスタムプロパティ で USS を拡張することができます。カスタム USS プロパティには --
というプレフィックスが必要です。
ここでは、サポートされている型を列挙します。
UI Toolkit は長さの単位としてピクセル (px
) とパーセンテージ (%
) をサポートします。ピクセル値は絶対値ですが、パーセンテージは通常、要素の親に相対的です。
例:
width:200px;
は、幅が 200 ピクセルであることを表します。width:50%;
は、幅が親要素の半分であることを表しています。測定単位を指定することは重要です。単位を指定しない場合、UI Toolkit はプロパティ値がピクセルで表されていると想定します。
注意: 0
は、測定単位を必要としない特別な値です。
数値は、浮動小数点数または整数リテラルで表されます。例えば、flex:1.0
です。
一部のビルトインプロパティでは、特定のキーワードがサポートされています。キーワードは、数字ではなく説明的な名前を提供します。例えば、position:absolute
です。すべてのプロパティは、プロパティをデフォルト値にリセットする initial
グローバルキーワードをサポートします。キーワードのリストについては、サポートされているプロパティ を参照してください。
UI Toolkit は、以下のリテラルの色の値と関数をサポートします。
#FFFF00
(rgba はチャンネルごとに 1 バイト)、#0F0
(rgb)rgb(255, 255, 0)
rgba(255, 255, 0, 1.0)
blue
、transparent
フォントやテクスチャなどのプロジェクトアセットを、USS ファイルから参照することができます。例えば、テクスチャを参照して、要素の背景画像として使用できます。
アセットを参照するには、url()
関数または resource()
関数を使用します。参照されたアセットは、スタイルシートをインポートすると解決されます。
例えば、以下のスタイル宣言では resource()
関数を使用して、Images
ディレクトリにある img.png
という名前のテクスチャアセットを参照し、背景画像として指定します。
`background-image: resource("Images/img.png");
Unity では、ほとんどの場合、url()
関数の使用を推奨しています。ただし、resource()
関数は、画面の密度に応じて異なるバージョンの画像アセットを自動的にロードすることをサポートしています。
url()
関数でアセットを参照する際、指定するパスは相対パスでも絶対パスでも構いません。
パスは、ファイルの拡張子を含む必要があります。
例えば、プロジェクトに、すべてのスタイルシートを含む USS
フォルダーと、すべての画像アセットを含む Resources フォルダーがあるとします。
Assets
└─ Editor
└─ Resources
└─ USS
thumb.png
という名前の画像を参照するには、以下のいずれかのパスを使用します。
相対パス | 絶対パス |
---|---|
url("../Resources/thumb.png") |
url("/Assets/Editor/Resources/thumb.png") url("project:/Assets/Editor/Resources/thumb.png") url("project:///Assets/Editor/Resources/thumb.png")
|
resource()
関数は、Unity のリソースフォルダー (Resources
と Editor Default Resources
) にあるアセットを参照できます。アセットの参照は名前で行います。
Editor Default Resources Resources
フォルダーにある場合は、ファイルの拡張子が必要です。Resources
フォルダーにある場合は、ファイルの拡張子は不要です。例:
ファイルへのパス | 参照構文 |
---|---|
Assets/Resources/Images/my-image.png |
resource("Images/my-image") |
Assets/Editor Default Resources/Images/my-image.png |
resource("Images/default-image.png") |
異なる画面密度 (DPI) の画面をサポートする必要がある場合、resource()
関数を使用すると、正しいバージョンのテクスチャアセットを自動的にロードできます。
その際、以下が必要です。
@2x
というサフィックスが付いていることを確認してください。例えば、myimage.png
の高 DPI バージョンは、myimage@2x.png
としてください。Unity がアセットをロードするとき、現在のスクリーン DPI に合ったバージョンを自動的に選択します。
例えば、USS の resource("myimage")
を使用すると、Unity は Resources/myimage.png
または Resources/myimage@2x.png
のいずれかをロードします。
引用符を使用して文字列の値を指定します。例えば --my-property: "foo"
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.