Okay, here's a UXML structure for an interactive menu in Unity, along with C# code to manage it. This design focuses on being dynamically populated, having a centered "primary" entry, and the visual style you requested.
1. UXML (InteractiveMenu.uxml)
<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<Style src="InteractiveMenuStyle.uss" />
<VisualElement class="interactive-menu" style="width: 300px; height: 100vh; right: 0px; position: absolute;">
<VisualElement class="menu-header">
<Label text="Menu" class="menu-header-label" />
</VisualElement>
<ScrollView class="menu-scroll-view">
<VerticalLayout class="menu-content">
<VisualElement class="menu-entry primary-entry" data-entry-id="primary" style="justify-content: center;">
<Image class="entry-icon" src="Assets/Icons/PrimaryIcon.png" />
<Label text="Primary Action" class="entry-text" />
</VisualElement>
<VisualElement class="spacer" /> <!-- Spacer to push primary entry to center -->
<VisualElement class="dynamic-entries" />
</VerticalLayout>
</ScrollView>
</VisualElement>
</UXML>
2. USS (InteractiveMenuStyle.uss)
.interactive-menu {
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
border-left: 1px solid rgba(255, 255, 255, 0.2);
}
.menu-header {
background-color: rgba(0, 0, 0, 0.7);
padding: 10px;
text-align: center;
}
.menu-header-label {
font-size: 16px;
color: white;
font-weight: bold;
}
.menu-scroll-view {
height: calc(100% - 40px); /* Adjust for header height */
}
.menu-content {
padding: 5px;
align-items: flex-start; /* Align entries to the top */
}
.menu-entry {
display: flex;
align-items: center;
padding: 8px;
margin-bottom: 5px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
cursor: pointer;
}
.menu-entry:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.entry-icon {
width: 32px;
height: 32px;
margin-right: 10px;
}
.entry-text {
font-size: 14px;
color: white;
flex-grow: 1; /* Take up remaining space */
text-align: left;
}
.primary-entry {
font-weight: bold;
color: yellow;
}
.spacer {
flex-grow: 1; /* Push the primary entry to the center */
}
.dynamic-entries {
/* This is where dynamically added entries will go */
}
3. C# Script (InteractiveMenuController.cs)
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
public class InteractiveMenuController : MonoBehaviour
{
public UIDocument uiDocument;
private VisualElement dynamicEntriesContainer;
private VisualElement primaryEntry;
void Start()
{
if (uiDocument == null)
{
Debug.LogError("UIDocument not assigned!");
return;
}
VisualElement root = uiDocument.rootVisualElement;
dynamicEntriesContainer = root.Q<VisualElement>("dynamic-entries");
primaryEntry = root.Q<VisualElement>("primary");
// Example: Add some initial entries
AddEntry("Option 1", "Assets/Icons/Icon1.png");
AddEntry("Option 2", "Assets/Icons/Icon2.png");
}
public void AddEntry(string text, string iconPath)
{
VisualElement entry = new VisualElement();
entry.AddToClassList("menu-entry");
Image icon = new Image();
icon.image = AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
icon.style.width = 32;
icon.style.height = 32;
icon.style.marginRight = 10;
entry.Add(icon);
Label label = new Label(text);
label.AddToClassList("entry-text");
entry.Add(label);
entry.RegisterCallback<MouseEnterEvent>(evt => entry.AddToClassList("hovered"));
entry.RegisterCallback<MouseLeaveEvent>(evt => entry.RemoveFromClassList("hovered"));
entry.RegisterCallback<ClickEvent>(evt =>
{
Debug.Log("Entry clicked: " + text);
// Add your action here
});
dynamicEntriesContainer.Add(entry);
}
public void RemoveEntry(VisualElement entryToRemove)
{
dynamicEntriesContainer.Remove(entryToRemove);
}
public VisualElement GetPrimaryEntry()
{
return primaryEntry;
}
public void SetPrimaryEntryText(string text)
{
Label primaryLabel = primaryEntry.Q<Label>("entry-text");
if (primaryLabel != null)
{
primaryLabel.text = text;
}
}
}
How to Use:
- Create a new Unity project or open an existing one.
- Create a new UXML file (e.g.,
InteractiveMenu.uxml) and paste the UXML code into it.
- Create a new USS file (e.g.,
InteractiveMenuStyle.uss) and paste the USS code into it.
- Create a new C# script (e.g.,
InteractiveMenuController.cs) and paste the C# code into it.
- Create a Canvas in your scene.
- Add a UIDocument component to the Canvas.
- In the UIDocument's Inspector, set the UXML to your
InteractiveMenu.uxml file.
- Create an empty GameObject in your scene.
- Add the
InteractiveMenuController script to the GameObject.
- In the
InteractiveMenuController's Inspector, drag the UIDocument from the Canvas to the uiDocument field.
- Place some icons in your
Assets/Icons folder (or adjust the paths in the code).
Key Features and Explanation:
- UXML Structure: The UXML defines the layout of the menu. It uses a
VisualElement with a class interactive-menu to contain everything. A ScrollView allows for scrolling if there are many entries. VerticalLayout arranges the entries vertically.
- USS Styling: The USS file controls the visual appearance (background color, fonts, padding, etc.). The
rgba() color format allows for transparency.
- Dynamic Entries: The
dynamic-entries VisualElement is where new entries will be added by the C# code.
- Primary Entry: The
primary-entry VisualElement is always centered. The spacer element pushes it to the center.
- C# Control:
uiDocument: A reference to the UIDocument component.
dynamicEntriesContainer: A reference to the dynamic-entries VisualElement.
AddEntry(): Creates a new menu entry, adds an icon and text, and adds it to the dynamicEntriesContainer. It also adds click event handling.
RemoveEntry(): Removes an entry from the menu.
GetPrimaryEntry(): Returns the primary entry VisualElement.
SetPrimaryEntryText(): Allows you to change the text of the primary entry from code.
- Event Handling: The
RegisterCallback methods are used to handle mouse enter, mouse leave, and click events on the menu entries.
- Icon Loading: The
AssetDatabase.LoadAssetAtPath method is used to load icons from your project's Assets folder. Make sure the paths are correct.
- Transparency: The
background-color: rgba(0, 0, 0, 0.5); in the USS file creates a semi-transparent background.
Important Considerations:
- Icon Paths: Adjust the icon paths in the
AddEntry() method and the UXML to match the location of your icons in your project.
- Error Handling: Add more robust error handling (e.g., checking if the icon file exists) to the
AddEntry() method.
- Performance: If you have a very large number of entries, consider using a more efficient way to manage them (e.g., virtualized lists).
- Customization: Modify the UXML and USS files to customize the appearance of the menu to your liking.
- Accessibility: Consider adding accessibility features (e.g., keyboard navigation) to the menu.
- Data Binding: For more complex scenarios, consider using data binding to automatically update the menu entries based on data changes.
This provides a solid foundation for an interactive menu in Unity. You can expand upon this by adding more features, such as submenus, tooltips, and more complex event handling. Remember to adjust the code and styles to fit your specific needs.