Page 1 of 1

Problem loading ImageSource in Unity

Posted: 29 Oct 2021, 19:25
by simonb
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"));

....
}

Re: Problem loading ImageSource in Unity

Posted: 31 Oct 2021, 14:11
by simonb
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... ?

Re: Problem loading ImageSource in Unity

Posted: 01 Nov 2021, 13:14
by jsantos
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

Posted: 02 Nov 2021, 13:27
by simonb
OK - thanks for the info, will go and have a re-think :)

Re: Problem loading ImageSource in Unity

Posted: 02 Nov 2021, 13:28
by simonb
OK - thanks for the info, will go and have a re-think :)

Re: Problem loading ImageSource in Unity

Posted: 02 Nov 2021, 14:46
by ttermeer-reboundcg
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.

Re: Problem loading ImageSource in Unity

Posted: 09 Nov 2021, 14:02
by simonb
ah will try that.
Many thanks :)

Re: Problem loading ImageSource in Unity

Posted: 28 Nov 2021, 19:14
by simonb
Great that works well, thanks.