1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity UI Toolkit で 9-slice (Sprite border) を使う

1
Last updated at Posted at 2026-07-02

image.png

AIからそのまま引用になってしまいますが、自分用の記事なのでいいよね・・・

結論から言うと Unity UI Toolkit(UIDocument / USS / UXML)では、Sprite の 9‑slice をそのまま使うことはできない。
ただし 代替手段は複数あり、実質的に同じ見た目を再現することは可能。

UI Toolkit は USS の -unity-slice-* プロパティで 9‑slice を実現できる。

✔ 方法:Texture2D を使い、USS でスライス指定する

.uss
.my-sliced {
    background-image: url("res://my_texture.png");
    -unity-slice-left: 16;
    -unity-slice-right: 16;
    -unity-slice-top: 16;
    -unity-slice-bottom: 16;
    background-repeat: stretch;
}

ポイント
Sprite ではなく Texture2D を使う

スライス値は ピクセル単位で手動指定

UGUI の Sprite border と同じ概念だが、UI Toolkit は自動で読み取らない

🧪 Unity Editor での設定方法(実務向け)

あなたの Unity 開発スタイルに合わせて、実際のワークフローをまとめる。

  1. 画像を Texture2D としてインポート
    Texture Type → Default

Sprite にしない(Sprite border を使えないため)

  1. USS でスライス指定
.uss
.panel {
    background-image: url("res://Textures/panel.png");
    -unity-slice-left: 12;
    -unity-slice-right: 12;
    -unity-slice-top: 12;
    -unity-slice-bottom: 12;
}
  1. UXML で適用
.xml
<VisualElement class="panel" />

🧭 代替案(Sprite を使いたい場合)

どうしても Sprite を使いたい場合は以下の方法がある。

  1. UGUI を併用する(UIDocument の上に Canvas を重ねる)
    Sprite の 9‑slice をそのまま使える

UI Toolkit と UGUI のハイブリッド構成

  1. Sprite を Texture2D に変換して使う
    Editor 拡張で自動変換する

Sprite border を読み取り、USS の slice 値を自動生成するツールを作ることも可能

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?