Unity3D: event subscription for dynamic menu items
Posted: 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:
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