Mikael Gyth
Topic Author
Posts: 33
Joined: 15 Aug 2013, 12:53

Example of Noesis interacting with Unity3D

21 Feb 2018, 06:35

Hello.
I haven't been using Noesis GUI for over 3 years, and some things have changed since then.
I was able to set up Noesis in Unity and get A GUI showing some values bound to by a ViewModel.
So the GUI itself is working, even if it's a mess to have all this code that neither Unity, Visual Studio or Blend knows what to do with. Working in files with no support from the tools isn't fun, but it works, sort of.

However I was not able to find ONE example of the Noesis code actually interacting with anything in Unity3D, sure I can see my GUI and press buttons on it, but I need to get the information from game objects in a scene in Unity. How do I connect the data from my scripts to the ViewModel that Noesis is using? After all, I want a GUI not a static display of information.

An example with super simple XAML + code behind + ViewModel that gets its data from a GameObject in a scene would be helpful.
 
User avatar
jsantos
Site Admin
Posts: 2903
Joined: 20 Jan 2012, 17:18
Contact:

Re: Example of Noesis interacting with Unity3D

21 Feb 2018, 12:43

So the GUI itself is working, even if it's a mess to have all this code that neither Unity, Visual Studio or Blend knows what to do with. Working in files with no support from the tools isn't fun, but it works, sort of.
Could you please elaborate a bit more about that "mess" you are talking about?
However I was not able to find ONE example of the Noesis code actually interacting with anything in Unity3D, sure I can see my GUI and press buttons on it, but I need to get the information from game objects in a scene in Unity. How do I connect the data from my scripts to the ViewModel that Noesis is using? After all, I want a GUI not a static display of information.

An example with super simple XAML + code behind + ViewModel that gets its data from a GameObject in a scene would be helpful.
Well, the ViewModel is exposing properties that are automatically bound to the XAML. You are in full control of the ViewModel, that's not a Noesis thing, it is a class you create using Unity and you can control its lifetime and behavior. We are working in more complete examples though, but if you need help with any specific scenarios, please let us know and we will show you how to do it with Noesis and Unity.
 
Mikael Gyth
Topic Author
Posts: 33
Joined: 15 Aug 2013, 12:53

Re: Example of Noesis interacting with Unity3D

21 Feb 2018, 15:30

Firstly I'm sorry if I came off a bit brash in my original post, I know it's not a good excuse to not be polite but I was frustrated.
Could you please elaborate a bit more about that "mess" you are talking about?
Well, Neither Visual Studio or Blend is able to "handle" the XAML files very well. There are references that it cannot resolve etc.
So by just importing Noesis GUI into a project, opening the solution in Visual Studio and then opening the file MainWindow.xaml in the Buttons sample it looks like this:
Image
But that's ok, I know you can set up a WPF project in the middle of your unity project to get some sort of resolution to this.
We are working in more complete examples though, but if you need help with any specific scenarios, please let us know and we will show you how to do it with Noesis and Unity.
That's great, I would like to either access the ViewModel or be able to inject it.

Given that I have a new project with an empty scene and NoesisGUI installed. I have created the NoesisView on the camera and attached the demo sample "Buttons", MainWindow.xaml, to the component. When I run the game I see the buttons and can interact with them.
The buttons sample use a ViewModel, this is created in the code-behind file "MainWindow.xaml.cs".

How can I, from a GameObject, with a C# script in it, either set this ViewModel or access the one that is created in code behind?
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Example of Noesis interacting with Unity3D

23 Feb 2018, 13:47

Hi, let me step in to show you a very simple example on how to interact with Unity objects.

First consider this small xaml (you can create them even without a code-behind class like this one):
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="50">
        <TextBlock Text="Rotation Speed:"/>
        <Slider Minimum="0" Maximum="50" Value="{Binding Speed, FallbackValue=0}" Width="100" Margin="10,0"/>
        <TextBlock Text="{Binding Speed, StringFormat=F2}"/>
    </StackPanel>
</Grid>
Attaching this xaml asset to the Main Camera will create a NoesisView component.

Then you can write a MonoBehavior and attach it also to the camera:
using System.ComponentModel;
using UnityEngine;

public class UnityInteraction : MonoBehaviour, INotifyPropertyChanged
{
    public GameObject cube;

    private float _speed;
    public float Speed
    {
        get { return _speed; }
        set
        {
            if (_speed != value)
            {
                _speed = value;
                OnPropertyChanged("Speed");
            }
        }
    }

    private void Start()
    {
        NoesisView view = GetComponent<NoesisView>();
        view.Content.DataContext = this;
    }

    private void Update()
    {
        if (cube != null)
        {
            cube.transform.Rotate(0, Time.deltaTime * Speed, 0);
        }
    }

    #region INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}
In MonoBehavior's Start method you can get a reference to the NoesisView component and access its Content to set the DataContext yourself. As you can see, in this case I made the MonoBehavior to be the ViewModel itself but you can separate that in different classes if you want.

That's all, now you have a NoesisGUI Slider controlling the rotation speed of a Unity's Cube gameobject.
 
Mikael Gyth
Topic Author
Posts: 33
Joined: 15 Aug 2013, 12:53

Re: Example of Noesis interacting with Unity3D

24 Feb 2018, 12:23

Brilliant, thanks. This should be the tutorial, simple short and easy to understand.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Example of Noesis interacting with Unity3D

28 Feb 2018, 20:11

You're welcome, setting this as solved.

Who is online

Users browsing this forum: No registered users and 1 guest