simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Problem loading ImageSource in Unity

29 Oct 2021, 19:25

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"));

....
}
 
simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Re: Problem loading ImageSource in Unity

31 Oct 2021, 14:11

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... ?
 
User avatar
jsantos
Site Admin
Posts: 3917
Joined: 20 Jan 2012, 17:18
Contact:

Re: Problem loading ImageSource in Unity

01 Nov 2021, 13:14

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.
 
simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Re: Problem loading ImageSource in Unity

02 Nov 2021, 13:27

OK - thanks for the info, will go and have a re-think :)
 
simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Re: Problem loading ImageSource in Unity

02 Nov 2021, 13:28

OK - thanks for the info, will go and have a re-think :)
 
User avatar
ttermeer-reboundcg
Posts: 17
Joined: 13 Sep 2019, 11:10
Contact:

Re: Problem loading ImageSource in Unity

02 Nov 2021, 14:46

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.
#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
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.
 
simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Re: Problem loading ImageSource in Unity

09 Nov 2021, 14:02

ah will try that.
Many thanks :)
 
simonb
Topic Author
Posts: 24
Joined: 22 May 2021, 16:35

Re: Problem loading ImageSource in Unity

28 Nov 2021, 19:14

Great that works well, thanks.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], TommyDawkins and 7 guests