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?

More than 3 years have passed since last update.

UIElementsで要素を水平に配置する方法

Last updated at Posted at 2020-05-16

UIElementsのUSSで要素を水平に配置する方法。

水平配置したい要素を囲う要素を作って horizontal クラスをつけてあげる。

elements.uxml
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
    <ui:VisualElement name="Layout">
        <Style src="style.uss" />
        <ui:Label text="垂直" />
        <ui:Label text="垂直ボタン1" />
        <ui:VisualElement name="HorizontalLayout" class="horizontal">
            <ui:Label text="水平1" />
            <ui:Label text="水平2" />
            <ui:Button text= "水平ボタン"/>
        </ui:VisualElement>
        <ui:Label text="ここからまた垂直" />
        <ui:Button text= "垂直ボタン2"/>
    </ui:VisualElement>
</ui:UXML>
style.uss
.horizontal {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
}
ExampleEditorWindow.cs
using UnityEditor;
using UnityEngine.UIElements;

public class ExampleEditorWindow: EditorWindow {
	[MenuItem("Window/Example")]
	public static void Open() => GetWindow<ExampleEditorWindow>("Example");

	private void OnEnable() {
		var root = rootVisualElement;
		root.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/style.uss"));

		var tree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/elements.uxml");
		root.Add(tree.CloneTree());
	}
}
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?