I'll provide a comprehensive solution with UXML, USS, and C# script for a dynamic, centered interact menu in Unity UI Toolkit. Here's a complete implementation:
- UXML (InteractMenu.uxml):
<?xml version="1.0" encoding="utf-8"?>
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<ui:VisualElement name="interact-menu" class="interact-menu">
<ui:VisualElement name="entries-container" class="entries-container">
<!-- Entries will be dynamically added here -->
</ui:VisualElement>
<ui:VisualElement name="center-entry" class="center-entry">
<ui:VisualElement name="center-icon" class="entry-icon"></ui:VisualElement>
<ui:Label name="center-text" class="entry-text" text="Interact"></ui:Label>
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
- USS (InteractMenu.uss):
.interact-menu {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
width: 250px;
background-color: rgba(0, 0, 0, 0.6);
border-radius: 10px;
padding: 10px;
}
.entries-container {
flex-direction: column;
max-height: 300px;
overflow: scroll;
}
.center-entry {
flex-direction: row;
align-items: center;
justify-content: center;
background-color: rgba(50, 50, 50, 0.8);
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
.entry-icon {
width: 40px;
height: 40px;
background-color: white;
margin-right: 10px;
}
.entry-text {
color: white;
font-size: 16px;
}
.menu-entry {
flex-direction: row;
align-items: center;
background-color: rgba(30, 30, 30, 0.7);
border-radius: 5px;
padding: 5px;
margin-bottom: 5px;
}
.menu-entry-icon {
width: 30px;
height: 30px;
background-color: lightgray;
margin-right: 10px;
}
.menu-entry-text {
color: white;
font-size: 14px;
}
- C# Script (InteractMenu.cs):
using UnityEngine;
using UnityEngine.UIElements;
public class InteractMenu : MonoBehaviour
{
private UIDocument uiDocument;
private VisualElement entriesContainer;
private VisualElement centerIcon;
private Label centerText;
void Start()
{
uiDocument = GetComponent<UIDocument>();
var root = uiDocument.rootVisualElement;
entriesContainer = root.Q<VisualElement>("entries-container");
centerIcon = root.Q<VisualElement>("center-icon");
centerText = root.Q<Label>("center-text");
}
public void AddEntry(Texture2D icon, string text)
{
var entry = new VisualElement();
entry.AddToClassList("menu-entry");
var entryIcon = new VisualElement();
entryIcon.AddToClassList("menu-entry-icon");
entryIcon.style.backgroundImage = icon;
var entryText = new Label(text);
entryText.AddToClassList("menu-entry-text");
entry.Add(entryIcon);
entry.Add(entryText);
entriesContainer.Add(entry);
}
public void RemoveEntry(string text)
{
var entryToRemove = entriesContainer.Q<VisualElement>(className: "menu-entry",
predicate: e => e.Q<Label>().text == text);
if (entryToRemove != null)
{
entriesContainer.Remove(entryToRemove);
}
}
public void SetCenterEntry(Texture2D icon, string text)
{
centerIcon.style.backgroundImage = icon;
centerText.text = text;
}
}
- Usage Example:
public class GameController : MonoBehaviour
{
public InteractMenu interactMenu;
public Texture2D exampleIcon;
void Start()
{
// Add entries
interactMenu.AddEntry(exampleIcon, "Open Door");
interactMenu.AddEntry(exampleIcon, "Pick Up Item");
// Set center entry
interactMenu.SetCenterEntry(exampleIcon, "Interact");
}
}
To use this:
- Create a new UI Document in your scene
- Attach the InteractMenu.cs script to the same GameObject
- Assign the UI Document in the Inspector
- Use the methods to dynamically manage menu entries
Key Features:
- Transparent, right-side menu
- Dynamic entry addition/removal
- Centered, always-visible entry
- Customizable via USS
- Icon and text support
Customize the USS file to match your game's visual style. The C# script provides methods to add entries, remove entries, and set the center entry dynamically.