VincentL
Topic Author
Posts: 2
Joined: 19 Mar 2018, 10:32

Unity3D: event subscription for dynamic menu items

20 Mar 2018, 14:45

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:
<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>
And a DockP1.cs script attached to the main camera in Unity:
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()
    {
    }
}
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
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Unity3D: event subscription for dynamic menu items

20 Mar 2018, 17:06

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:
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");
    }
}
And bind it in the xaml:
<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>
Hope this helps.
 
VincentL
Topic Author
Posts: 2
Joined: 19 Mar 2018, 10:32

Re: Unity3D: event subscription for dynamic menu items

20 Mar 2018, 21:12

Hope this helps.
Absolutely helpful, thank you!

I only had to correct one simple thing in the MenuEntry class (two constructors). I post it for future reads:
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));
    }
}
Thanks again,
Vincent
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Unity3D: event subscription for dynamic menu items

21 Mar 2018, 05:51

Thanks for your feedback!

Who is online

Users browsing this forum: Google [Bot] and 68 guests