-
- kemarofangs
- Posts: 32
- Joined:
Cast As Interface
What is the proper way to cast interfaces in the code behind?
Is there a simple trick to make this work?
// Attempt 1) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement as I_Equipment;
// iEquipment is NULL
// Attempt 2) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement.As<I_Equipment>();
// cannot compile because interface cannot implement Noesis.BaseComponent and remain an interface.
Is there a simple trick to make this work?
// Attempt 1) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement as I_Equipment;
// iEquipment is NULL
// Attempt 2) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement.As<I_Equipment>();
// cannot compile because interface cannot implement Noesis.BaseComponent and remain an interface.
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Cast As Interface
Hi,
Which version of NoesisGUI are you using?
Since NoesisGUI 1.2 you can use the 'as' operator to cast to any implementation class or interface.
For example, if I have the following class definitions:
And the xaml below:
I can write a Unity behavior that gets the interface and uses it:
Which version of NoesisGUI are you using?
Since NoesisGUI 1.2 you can use the 'as' operator to cast to any implementation class or interface.
For example, if I have the following class definitions:
Code: Select all
public interface IHello
{
void SayHello();
}
public class HelloElement : UIElement, IHello
{
void IHello.SayHello()
{
Debug.Log("Hello!");
}
}
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Name="btn" Width="200" Height="100">
<HelloElement/>
</Button>
</Grid>
Code: Select all
public class CastBehavior : MonoBehaviour
{
void Start()
{
var gui = GetComponent<NoesisGUIPanel>();
var content = gui.GetContent();
var btn = (Button)content.FindName("btn");
var hello = btn.Content as IHello;
if (hello != null)
{
hello.SayHello();
}
else
{
Debug.Log("Element does not implement IHello");
}
}
}
-
- kemarofangs
- Posts: 32
- Joined:
Re: Cast As Interface
Hey, mine's version 1.1 from 2014. I'm downloading the new one since I'm registered now. thanks!
Just got to muck through importing the latest version without errors. Seems DependencyData.cs in Proxies is an empty file...
Just got to muck through importing the latest version without errors. Seems DependencyData.cs in Proxies is an empty file...
-
- kemarofangs
- Posts: 32
- Joined:
Re: Cast As Interface
So, is there a proper way to overwrite / upgrade the noesis unity asset? I backed up before upgrading but have since only ran into problems. It seems that the DependencyData cs file is empty no matter what I do. It's preventing UIElementData from compiling. Even tried reimporting the old asset then the newest one.
Re: Cast As Interface
Although it should update properly I recommend manually cleaning the old version and fresh installing the new one. I will investigate the problems you are having to fix them because the 1.2 is going to be uploaded to the Asset Store soon or later.
-
- kemarofangs
- Posts: 32
- Joined:
Re: Cast As Interface
Ok, I've almost got it working. Had to comment alot of the #ifndef / #endif sections since I'm using the latest full version of unity 4 and it doesn't understand the unity 5 references. Can you advise on how to get the root element of the noesis panel now?
NoesisGUIPanel noesisGUIPanel = GetComponent<NoesisGUIPanel>();
Noesis.FrameworkElement frameworkElement = noesisGUIPanel.GetContent (); // Used to be GetRoot();
// frameworkElement is NULL
// can't use frameworkElement.FindName("nameOfElement") now...
NoesisGUIPanel noesisGUIPanel = GetComponent<NoesisGUIPanel>();
Noesis.FrameworkElement frameworkElement = noesisGUIPanel.GetContent (); // Used to be GetRoot();
// frameworkElement is NULL
// can't use frameworkElement.FindName("nameOfElement") now...
Re: Cast As Interface
Yes, your code seems to be right, you must use GetContent().
Make sure you call this function when the XAML is loaded in Play mode.
Code: Select all
/// <summary>
/// Gets the root of the loaded Xaml.
/// </summary>
/// <returns>Root element.</returns>
public FrameworkElement GetContent()
-
- kemarofangs
- Posts: 32
- Joined:
Re: Cast As Interface
So I've got a fresh copy of Unity 5.4.0f3 installed with Noesis 1.2.6f5. Loading the sample lion project produces the following error... The file is there so I'm trying to figure out why the Noesis GUI panel can't locate it. Any ideas or should I just wait for the new unity store noesis package to come out?
NoesisException: Loading Assets/NoesisGUI/Samples/Lion/Lion.xaml
Resource Assets/NoesisGUI/Samples/Lion/Lion.xaml not found
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:26)
NoesisGUISystem.Noesis_LoadXAML (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:409)
NoesisGUISystem.Load (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:282)
NoesisGUISystem.LoadXaml (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel.LoadXaml () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:513)
NoesisGUIPanel.OnEnable () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:400)
NoesisException: Loading Assets/NoesisGUI/Samples/Lion/Lion.xaml
Resource Assets/NoesisGUI/Samples/Lion/Lion.xaml not found
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:26)
NoesisGUISystem.Noesis_LoadXAML (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:409)
NoesisGUISystem.Load (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:282)
NoesisGUISystem.LoadXaml (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel.LoadXaml () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:513)
NoesisGUIPanel.OnEnable () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:400)
-
- kemarofangs
- Posts: 32
- Joined:
Re: Cast As Interface
I seem to have issues trying to make unity realize that changes have been made. It's like the editor won't auto compile. Frustrating. I did windows update and rebooted then the Lion example project magically started working. Had to open my XAML file and change the background color on my personal project for it to not have the "I can't find the XAML" compiler error. I wish there there was a "force recompile all" button in unity.
Next problem is getting my custom font to work again. Any ideas? I'm trying not to be annoying and really appreciate all the help. I followed the example in the documentation to double check my work but the compiler is again complaining (http://www.noesisengine.com/docs/Gui.Co ... orial.html).
Error 1)
[dx11] Assets/Darren/Code/Title.xaml
Parsing TextBlock.FontFamily (@15,2).
FontFamily 'D:/Work/Source Code/Unity/Marauders/Assets/Darren/Font/KaushanScript-Regular' not found
UnityEngine.Debug:LogError(Object)
Noesis.BuildToolKernel:OnLog(Int32, String) (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:267)
System.Object:wrapper_native_4238D6E0(String)
Noesis.BuildToolKernel:BuildIncremental() (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:116)
NoesisPostProcessor:Build(String) (at Assets/Editor/NoesisGUI/NoesisPostProcessor.cs:251)
Warning 1)
Unable to open Assets/Darren/Font/KaushanScript-Regular.otf: Check external application preferences.
XAML) Assets\Darren\Code\Title.xaml
Here's the physical location.
Assets\Darren\Font\KaushanScript-Regular.otf
Font is installed on the OS. Download here https://www.fontsquirrel.com/fonts/kaushan-script.
Next problem is getting my custom font to work again. Any ideas? I'm trying not to be annoying and really appreciate all the help. I followed the example in the documentation to double check my work but the compiler is again complaining (http://www.noesisengine.com/docs/Gui.Co ... orial.html).
Error 1)
[dx11] Assets/Darren/Code/Title.xaml
Parsing TextBlock.FontFamily (@15,2).
FontFamily 'D:/Work/Source Code/Unity/Marauders/Assets/Darren/Font/KaushanScript-Regular' not found
UnityEngine.Debug:LogError(Object)
Noesis.BuildToolKernel:OnLog(Int32, String) (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:267)
System.Object:wrapper_native_4238D6E0(String)
Noesis.BuildToolKernel:BuildIncremental() (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:116)
NoesisPostProcessor:Build(String) (at Assets/Editor/NoesisGUI/NoesisPostProcessor.cs:251)
Warning 1)
Unable to open Assets/Darren/Font/KaushanScript-Regular.otf: Check external application preferences.
XAML) Assets\Darren\Code\Title.xaml
Code: Select all
<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:noesis="clr-namespace:NoesisGUIExtensions">
<Canvas.Background>
<ImageBrush ImageSource="../Graphics/Raster/Background/MistyMountain.jpg" />
</Canvas.Background>
<!-- FontFamily="../Font/#KaushanScript-Regular" -->
<!-- Title -->
<Rectangle Fill="Crimson" Canvas.Top="250" Canvas.Left="40" Width="700" Height="100" Opacity="0.5" />
<TextBlock Canvas.Top="75" Canvas.Left="250" Text="Marauders"
FontSize="300" FontFamily="../Font/#KaushanScript-Regular" noesis:Text.Stroke="Crimson" noesis:Text.StrokeThickness="10" />
</Canvas>
Assets\Darren\Font\KaushanScript-Regular.otf
Font is installed on the OS. Download here https://www.fontsquirrel.com/fonts/kaushan-script.
Re: Cast As Interface
First, make sure you have selected the desire platform under Tools->NoesisGUI->Settings. This comes with sane default values, but maybe the upgrading process broke something. Also, each time you hit the Build button all the assets are preprocessed.
After having all the assets compiled, each time you touch a xaml, font or texture it must be automatically rebuilt. You can check this by inspecting under Assets\StreamingAssets\NoesisGUI the corresponding .nsb file.
Regarding the font, I am opening it with windows and it tells me that the font name is Kaushan Script. Changing the xaml to the following is working fine here:
The support of font attributes is quite limited in 1.2 (just bold and italic). This has been improved in 1.3 (yet not public) to support all weights, styles and stretches.
Please, let us know if you solve the preprocessing issues.
After having all the assets compiled, each time you touch a xaml, font or texture it must be automatically rebuilt. You can check this by inspecting under Assets\StreamingAssets\NoesisGUI the corresponding .nsb file.
Regarding the font, I am opening it with windows and it tells me that the font name is Kaushan Script. Changing the xaml to the following is working fine here:
Code: Select all
<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:noesis="clr-namespace:NoesisGUIExtensions">
<Rectangle Fill="Crimson" Canvas.Top="250" Canvas.Left="40" Width="700" Height="100" Opacity="0.5" />
<TextBlock Canvas.Top="75" Canvas.Left="250" Text="Marauders" FontSize="300"
FontFamily="../Font/#Kaushan Script" noesis:Text.Stroke="Crimson" noesis:Text.StrokeThickness="10" />
</Canvas>
Please, let us know if you solve the preprocessing issues.
Who is online
Users browsing this forum: No registered users and 0 guests