0
0

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】UIToolkitでは角丸と枠線が超簡単!

Posted at

概要

UIToolkitでは、各表示要素をスタイルシートによって装飾できます
その中でも、角丸と枠線はuGUIの頃と比べて格段に楽になっています

この記事では、以下のような形を作るまでの手順をまとめます

スクリーンショット 2024-12-24 23.22.16.png

作る

Projectパネルで、適当な場所にEditorWindowのテンプレートを作成します
右クリックからCreate > UI Toolkit > Editor Windowで作成できます
スクリーンショット 2024-12-24 23.25.30.png

Editor Windowを押下すると、↓画像のようなウィンドウが開きます
適当な名前でいいのですが、ここではBorderHandsOnとしましょう
スクリーンショット 2024-12-24 23.25.56.png

すると3つのファイルが生成されます
</>マークのファイルをダブルクリックしてUI Builderパネルを表示しましょう
スクリーンショット 2024-12-24 23.26.42.png

UI Builderでは、UIの見た目を調整することができます
すでにテンプレートで用意されている、表示確認ようのLabelが配置されているので削除してしまいましょう
スクリーンショット 2024-12-24 23.27.04.png

削除後、ContainersリストからVisualElementを↓画像のように配置しましょう
スクリーンショット 2024-12-24 23.27.26.png

子の要素を選択し、画面右のInspectorからパラメータを変更します
画像の通り変更してみてください

Flex: Grow: 0
Size: Width: 100, Height: 100
Background: Color: 自分の好きな色(Alphaを1以上にするのを忘れないこと)

スクリーンショット 2024-12-24 23.29.12.png

ここまでできると、矩形のUIが表示されているはずです
これに枠線と角丸をつけていきます
スクリーンショット 2024-12-24 23.29.39.png

InspectorのBorderから変更できます
まず枠線からつけてみましょう
画像のようにWidthを4pxにします
スクリーンショット 2024-12-24 23.32.00.png

すると画像のように黒い枠線がつきます
(付かない場合はColorのAlphaを1以上にしてみてください)
スクリーンショット 2024-12-24 23.30.01.png

次に角丸をつけます
Radiusをそれぞれ設定します
スクリーンショット 2024-12-24 23.30.33.png

すると矩形に角丸がつきました
スクリーンショット 2024-12-24 23.30.40.png

以上です

今回作成したuxmlは以下になります

<engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <Style src="project://database/Assets/UI/HandsOnBorder/BorderHandsOn.uss?fileID=7433441132597879392&amp;guid=66240669b5f434d3cb7df6b7e65c3e9b&amp;type=3#BorderHandsOn" />
    <engine:VisualElement style="flex-grow: 1;">
        <engine:VisualElement style="flex-grow: 0; flex-shrink: 0; width: 100px; height: 100px; background-color: rgb(212, 129, 129); border-top-width: 4px; border-right-width: 4px; border-bottom-width: 4px; border-left-width: 4px; border-left-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-top-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-top-left-radius: 15px; border-top-right-radius: 67px; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px;" />
    </engine:VisualElement>
</engine:UXML>

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?