Skip to content

Commit

Permalink
feat(samples): add UXML code generation sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Nov 12, 2022
1 parent ddb58b6 commit 86ecd43
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Assets/Samples/UxmlTraits.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Samples/UxmlTraits/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Samples/UxmlTraits/Editor/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
:root {
margin: 2px 8px;
}

#example-header {
margin-bottom: 10px;
}

#example-header Label {
white-space: normal;
}

.example-container {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 6px 16px;
margin-bottom: 4px;
border-color: aliceblue;
border-radius: 4px;
border-width: 2px;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<UXML xmlns:ui="UnityEngine.UIElements" xmlns:sample="UIComponents.Samples.UxmlTraits">
<ui:VisualElement name="example-header">
<ui:Label text="The UxmlTrait and UxmlName attributes generate UxmlFactory and UxmlTraits implementations for you." />
<ui:Label text="Three components are instantiated in UXML below. They use UXML traits for customization." />
</ui:VisualElement>
<ui:VisualElement class="example-container">
<ui:Label text="Bare component" />
<sample:UxmlTraitsExample />
</ui:VisualElement>
<ui:VisualElement class="example-container">
<ui:Label text="Overridden description" />
<sample:UxmlTraitsExample description-text="This is my custom description." />
</ui:VisualElement>
<ui:VisualElement class="example-container">
<ui:Label text="Overridden button color" />
<sample:UxmlTraitsExample button-color="#49006F" />
</ui:VisualElement>
</UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "UIComponents.Samples.UxmlTraits.Editor",
"rootNamespace": "UIComponents.Samples",
"references": [
"UIComponents.Samples.UxmlTraits"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Assets/Samples/UxmlTraits/Editor/UxmlTraitsExampleWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityEngine;
using UnityEngine.UIElements;

namespace UIComponents.Samples.UxmlTraits.Editor
{
public class UxmlTraitsExampleWindow : UnityEditor.EditorWindow
{
[UnityEditor.MenuItem("UIComponents Examples/UxmlTraits and UxmlName")]
private static void ShowWindow()
{
var window = GetWindow<UxmlTraitsExampleWindow>();
window.titleContent = new GUIContent("UxmlTraits and UxmlName");
window.minSize = new Vector2(470, 250);
window.Show();
}

private void CreateGUI()
{
var layout = Resources.Load<VisualTreeAsset>("UxmlTraitsExampleWindow");

layout.CloneTree(rootVisualElement);

var styles = Resources.Load<StyleSheet>("UxmlTraitsExampleWindow.style");

rootVisualElement.styleSheets.Add(styles);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Samples/UxmlTraits/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#uxml-traits-container {
display: flex;
flex-direction: row;
}

#uxml-traits-container Label {
-unity-font-style: bold;
}

#uxml-traits-description-label {
-unity-font-style: italic;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<UXML xmlns:ui="UnityEngine.UIElements">
<ui:VisualElement name="uxml-traits-container">
<ui:Label text="UxmlTraitsExampleComponent" />
<ui:Button name="uxml-traits-button" text="Button" />
</ui:VisualElement>
<ui:Label name="uxml-traits-description-label" text="No description set." />
</UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Assets/Samples/UxmlTraits/UIComponents.Samples.UxmlTraits.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "UIComponents.Samples.UxmlTraits",
"rootNamespace": "UIComponents.Samples",
"references": [
"UIComponents"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/Samples/UxmlTraits/UxmlTraitsExampleComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using UnityEngine;
using UnityEngine.UIElements;

namespace UIComponents.Samples.UxmlTraits
{
/// <summary>
/// An example component which uses UxmlName and UxmlTrait.
/// The component is instantiated with the name "UxmlTraitsExample" in UXML,
/// and the UxmlTrait attributes generate the necessary code for
/// customizing this component in UXML.
/// </summary>
[UxmlName("UxmlTraitsExample")]
[Layout("UxmlTraitsExampleComponent")]
[Stylesheet("UxmlTraitsExampleComponent.style")]
public partial class UxmlTraitsExampleComponent : UIComponent
{
[UxmlTrait]
public string DescriptionText
{
set
{
_descriptionText = value;
if (_descriptionLabel != null)
_descriptionLabel.text = value;
}
}
private string _descriptionText;

[UxmlTrait]
private Color _buttonColor;

[Query("uxml-traits-button")]
private Button _button;

[Query("uxml-traits-description-label")]
private Label _descriptionLabel;

public override void OnInit()
{
if (!string.IsNullOrEmpty(_descriptionText))
_descriptionLabel.text = _descriptionText;

_button.style.backgroundColor = _buttonColor;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Samples/UxmlTraits/UxmlTraitsExampleComponent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Assets/UIComponents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"path": "Samples~/Counter"
},
{
"displayName": "Asset Loading from Resources",
"displayName": "Asset loading from Resources",
"description": "Contains an example of asset loading from Resources.",
"path": "Samples~/Resources"
},
{
"displayName": "Asset Loading from Addressables",
"displayName": "Asset loading from Addressables",
"description": "Contains an example of asset loading from Addressables.",
"path": "Samples~/Addressables"
},
Expand All @@ -43,6 +43,11 @@
"displayName": "Event interfaces",
"description": "Contains an example of using event interfaces.",
"path": "Samples~/EventInterfaces"
},
{
"displayName": "UXML code generation",
"description": "Contains an example of UxmlName and UxmlTrait usage.",
"path": "Samples~/UxmlTraits"
}
],
"license": "MIT",
Expand Down
27 changes: 27 additions & 0 deletions UIElementsSchema/UIComponents.Samples.UxmlTraits.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:editor="UnityEditor.UIElements" xmlns:engine="UnityEngine.UIElements" xmlns="UnityEditor.PackageManager.UI" elementFormDefault="qualified" targetNamespace="UIComponents.Samples.UxmlTraits" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="UnityEngine.UIElements.xsd" namespace="UnityEngine.UIElements" />
<xs:complexType name="UxmlTraitsExampleType">
<xs:complexContent mixed="false">
<xs:restriction base="engine:VisualElementType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="engine:VisualElement" />
</xs:sequence>
<xs:attribute default="" name="name" type="xs:string" use="optional" />
<xs:attribute default="" name="view-data-key" type="xs:string" use="optional" />
<xs:attribute default="Position" name="picking-mode" type="engine:VisualElement_picking-mode_Type" use="optional" />
<xs:attribute default="" name="tooltip" type="xs:string" use="optional" />
<xs:attribute default="None" name="usage-hints" type="engine:VisualElement_usage-hints_Type" use="optional" />
<xs:attribute default="0" name="tabindex" type="xs:int" use="optional" />
<xs:attribute default="false" name="focusable" type="xs:boolean" use="optional" />
<xs:attribute default="" name="class" type="xs:string" use="optional" />
<xs:attribute default="" name="content-container" type="xs:string" use="optional" />
<xs:attribute default="" name="style" type="xs:string" use="optional" />
<xs:attribute default="RGBA(0.000, 0.000, 0.000, 1.000)" name="button-color" type="xs:string" use="optional" />
<xs:attribute default="" name="description-text" type="xs:string" use="optional" />
<xs:anyAttribute processContents="lax" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name="UxmlTraitsExample" substitutionGroup="engine:VisualElement" xmlns:q1="UIComponents.Samples.UxmlTraits" type="q1:UxmlTraitsExampleType" />
</xs:schema>
26 changes: 24 additions & 2 deletions UIElementsSchema/UIComponents.Tests.Roslyn.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<xs:attribute default="" name="style" type="xs:string" use="optional" />
<xs:attribute default="" name="description" type="xs:string" use="optional" />
<xs:attribute default="0" name="age" type="xs:int" use="optional" />
<xs:attribute default="0" name="unix-timestamp" type="xs:long" use="optional" />
<xs:attribute default="0" name="timestamp" type="xs:long" use="optional" />
<xs:attribute default="0" name="music-volume" type="xs:float" use="optional" />
<xs:anyAttribute processContents="lax" />
</xs:restriction>
Expand Down Expand Up @@ -71,12 +71,34 @@
<xs:attribute default="" name="content-container" type="xs:string" use="optional" />
<xs:attribute default="" name="style" type="xs:string" use="optional" />
<xs:attribute default="RGBA(0.000, 0.000, 0.000, 1.000)" name="text-color" type="xs:string" use="optional" />
<xs:attribute default="0" name="current-time" type="xs:double" use="optional" />
<xs:attribute default="0" name="time" type="xs:double" use="optional" />
<xs:attribute default="Hello" name="greeting" xmlns:q3="UIComponents.Tests.Roslyn" type="q3:RoslynTestComponent_greeting_Type" use="optional" />
<xs:attribute default="false" name="enabled" type="xs:boolean" use="optional" />
<xs:anyAttribute processContents="lax" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name="RoslynTestComponent" substitutionGroup="engine:VisualElement" xmlns:q4="UIComponents.Tests.Roslyn" type="q4:RoslynTestComponentType" />
<xs:complexType name="NestedRoslynTestType">
<xs:complexContent mixed="false">
<xs:restriction base="engine:VisualElementType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="engine:VisualElement" />
</xs:sequence>
<xs:attribute default="" name="name" type="xs:string" use="optional" />
<xs:attribute default="" name="view-data-key" type="xs:string" use="optional" />
<xs:attribute default="Position" name="picking-mode" type="engine:VisualElement_picking-mode_Type" use="optional" />
<xs:attribute default="" name="tooltip" type="xs:string" use="optional" />
<xs:attribute default="None" name="usage-hints" type="engine:VisualElement_usage-hints_Type" use="optional" />
<xs:attribute default="0" name="tabindex" type="xs:int" use="optional" />
<xs:attribute default="false" name="focusable" type="xs:boolean" use="optional" />
<xs:attribute default="" name="class" type="xs:string" use="optional" />
<xs:attribute default="" name="content-container" type="xs:string" use="optional" />
<xs:attribute default="" name="style" type="xs:string" use="optional" />
<xs:attribute default="" name="trait" type="xs:string" use="optional" />
<xs:anyAttribute processContents="lax" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name="NestedRoslynTest" substitutionGroup="engine:VisualElement" xmlns:q5="UIComponents.Tests.Roslyn" type="q5:NestedRoslynTestType" />
</xs:schema>
1 change: 1 addition & 0 deletions UIElementsSchema/UIElements.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<xs:import schemaLocation="UnityEngine.UIElements.xsd" namespace="UnityEngine.UIElements" />
<xs:import schemaLocation="UnityEditor.UIElements.xsd" namespace="UnityEditor.UIElements" />
<xs:import schemaLocation="UnityEditor.UIElements.Debugger.xsd" namespace="UnityEditor.UIElements.Debugger" />
<xs:import schemaLocation="UIComponents.Samples.UxmlTraits.xsd" namespace="UIComponents.Samples.UxmlTraits" />
<xs:import schemaLocation="UIComponents.Tests.Roslyn.xsd" namespace="UIComponents.Tests.Roslyn" />
<xs:import schemaLocation="UIComponents.Samples.EventInterfaces.xsd" namespace="UIComponents.Samples.EventInterfaces" />
<xs:import schemaLocation="UnityEditor.PackageManager.UI.xsd" namespace="UnityEditor.PackageManager.UI" />
Expand Down

0 comments on commit 86ecd43

Please sign in to comment.