Problem loading ImageSource in Unity
HI , I am trying to bind an image source from a ViewModel
The URI code below, works in Blend but not in Unity were it throws the following error:
[NOESIS/E] Assets/GUI/XAML/MainWindow.xaml(1): Image not found '/NGTest8;component/Assets/Images/LoadingShot1.JPG'
I have basically taken the path from the 'Questlog' sample and slightly modified it. That works fine in Blend & unity for me, but mine doesn't.....
Am I missing something simple here?
namespace NGTest8
{
public partial class MainWindowViewModel : INotifyPropertyChanged
{
private readonly ImageSource image1 = new BitmapImage(new Uri("pack://application:,,,/NGTest8;component/Assets/Images/LoadingShot1.JPG"));
private readonly ImageSource image2 = new BitmapImage(new Uri("pack://application:,,,/NGTest8;component/Assets/Images/LoadingShot2.JPG"));
....
}
The URI code below, works in Blend but not in Unity were it throws the following error:
[NOESIS/E] Assets/GUI/XAML/MainWindow.xaml(1): Image not found '/NGTest8;component/Assets/Images/LoadingShot1.JPG'
I have basically taken the path from the 'Questlog' sample and slightly modified it. That works fine in Blend & unity for me, but mine doesn't.....
Am I missing something simple here?
namespace NGTest8
{
public partial class MainWindowViewModel : INotifyPropertyChanged
{
private readonly ImageSource image1 = new BitmapImage(new Uri("pack://application:,,,/NGTest8;component/Assets/Images/LoadingShot1.JPG"));
private readonly ImageSource image2 = new BitmapImage(new Uri("pack://application:,,,/NGTest8;component/Assets/Images/LoadingShot2.JPG"));
....
}
Re: Problem loading ImageSource in Unity
After spending more time looking over your samples, I guess the answer is that it is just not possible to load an image from Unity in this way ?
All the samples seem to duplicate the ViewModel, one for blend and then another that does exactly the same thing but in a 'Unity' flavour
(using a Texture2D rather than an ImageSource)
Could you confirm that this is indeed the case and I should maintain 2 VM's going forward... ?
All the samples seem to duplicate the ViewModel, one for blend and then another that does exactly the same thing but in a 'Unity' flavour
(using a Texture2D rather than an ImageSource)
Could you confirm that this is indeed the case and I should maintain 2 VM's going forward... ?
Re: Problem loading ImageSource in Unity
In Unity there is no way to load resources from code (well, there are a few exceptions like Resources folder and Streaming Assets but in general those are not recommended). A dependency injection is necessary. You can do that using our Xaml.Dependencies extension (and this is no longer recommended) or using a more Unity friendly way as our examples are doing: exposing a Texture2D property in your MonoBehaviour and use that MonoBehaviouras as DataContext.
Re: Problem loading ImageSource in Unity
OK - thanks for the info, will go and have a re-think :)
Re: Problem loading ImageSource in Unity
OK - thanks for the info, will go and have a re-think :)
- ttermeer-reboundcg
- Posts: 17
- Joined:
- Contact:
Re: Problem loading ImageSource in Unity
In our project we use custom control and/or converter. This make the code works on both WPF and Unity.
With the code below you can return an image to your bindings or converters that will always work.
You can encapsulate that with a path manager depending on the platform. We use images loaded from the game folder but also from streaming assets. This even allowed us to provide easy modding for players.
With the code below you can return an image to your bindings or converters that will always work.
Code: Select all
#if NOESIS
var fileData = File.ReadAllBytes(filePath);
var tex = new Texture2D(2, 2, TextureFormat.RGBA32, mipmap);
tex.LoadImage(fileData);
return new TextureSource(tex);
#else
return new BitmapImage(new Uri(filePath));
#endif
Re: Problem loading ImageSource in Unity
ah will try that.
Many thanks :)
Many thanks :)
Re: Problem loading ImageSource in Unity
Great that works well, thanks.
Who is online
Users browsing this forum: Ahrefs [Bot] and 10 guests