Unity3D: event subscription for dynamic menu items
Hello everyone,
this is my first post, and i have to say I'm pretty impressed by NoesisGUI, it's one of the most promising platforms for GUI development in Unity I've worked with.
However, i still have troubles understanding the communication between Unity3D and NoesisGUI, so sorry for the noobish question.
I have a top menubar in xaml:
And a DockP1.cs script attached to the main camera in Unity:
It works fine, the menubar shows with all submenus etc. However, i simply don't know how to attach an event listener to these entries properly. So how to tell, if a specific button was clicked/pressed?
Thanks,
Vincent
this is my first post, and i have to say I'm pretty impressed by NoesisGUI, it's one of the most promising platforms for GUI development in Unity I've worked with.
However, i still have troubles understanding the communication between Unity3D and NoesisGUI, so sorry for the noobish question.
I have a top menubar in xaml:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel >
<StackPanel DockPanel.Dock="Top">
<Menu x:Name="MainMenu" ItemsSource="{Binding}">
<Menu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
</Menu>
</StackPanel>
</DockPanel >
</Grid>
Code: Select all
using System.Collections.Generic;
using UnityEngine;
public class MenuEntry
{
public MenuEntry(string header) { Header = header; Children = new List<MenuEntry>(); }
public string Header { get; private set; }
public List<MenuEntry> Children { get; private set; }
}
public class DockP1 : MonoBehaviour
{
private void Start()
{
NoesisView gui = GetComponent<NoesisView>();
List<MenuEntry> menu = new List<MenuEntry>();
menu.Add(new MenuEntry("File"));
menu[0].Children.Add(new MenuEntry("Open"));
menu[0].Children.Add(new MenuEntry("Save"));
menu[0].Children.Add(new MenuEntry("Exit"));
menu.Add(new MenuEntry("Edit"));
menu[1].Children.Add(new MenuEntry("Copy"));
menu[1].Children.Add(new MenuEntry("Cut"));
menu[1].Children.Add(new MenuEntry("Paste"));
menu.Add(new MenuEntry("Help"));
menu[2].Children.Add(new MenuEntry("About"));
gui.Content.DataContext = menu;
}
private void Update()
{
}
}
Thanks,
Vincent
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: Unity3D: event subscription for dynamic menu items
Hi and welcome to NoesisGUI
You need to expose a command in your menu entry class so UI can call you when menu item is clicked. You can implement a command like the DelegateCommand in our Commands tutorial: https://github.com/Noesis/Tutorials/blo ... Command.cs
Use it in the menu entry:
And bind it in the xaml:
Hope this helps.
You need to expose a command in your menu entry class so UI can call you when menu item is clicked. You can implement a command like the DelegateCommand in our Commands tutorial: https://github.com/Noesis/Tutorials/blo ... Command.cs
Use it in the menu entry:
Code: Select all
public class MenuEntry
{
public MenuEntry(string header) { Header = header; Children = new List<MenuEntry>(); }
public string Header { get; private set; }
public List<MenuEntry> Children { get; private set; }
public DelegateCommand ClickCommand { get; private set; }
public MenuEntry()
{
ClickCommand = new DelegateCommand(OnMenuClicked);
}
private void OnMenuClicked(object param)
{
UnityEngine.Debug.Log("Menu entry clicked");
}
}
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel >
<StackPanel DockPanel.Dock="Top">
<Menu x:Name="MainMenu" ItemsSource="{Binding}">
<Menu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
<Menu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Command" Value="{Binding ClickCommand}"/>
</Style>
</Menu.ItemContainerStyle>
</Menu>
</StackPanel>
</DockPanel >
</Grid>
Re: Unity3D: event subscription for dynamic menu items
Absolutely helpful, thank you!Hope this helps.
I only had to correct one simple thing in the MenuEntry class (two constructors). I post it for future reads:
Code: Select all
public class MenuEntry
{
public string Header { get; private set; }
public List<MenuEntry> Children { get; private set; }
public DelegateCommand ClickCommand { get; private set; }
public MenuEntry (string header)
{
Header = header;
Children = new List<MenuEntry>();
ClickCommand = new DelegateCommand(OnMenuClicked);
}
private void OnMenuClicked(object param)
{
UnityEngine.Debug.Log(("Menu entry clicked: " + Header));
}
}
Vincent
Re: Unity3D: event subscription for dynamic menu items
Thanks for your feedback!
Who is online
Users browsing this forum: Google [Bot] and 68 guests