knervous
Topic Author
Posts: 1
Joined: 04 Jul 2017, 23:01

Dynamically fill ResourceDictionary in Unity

15 Jul 2017, 15:05

Hello, I've just started using Noesis in the past week and have had some success and a few hangups.
I'm trying to load a lot of ImageBrush resources into a ResourceDictionary from a cs class. I'd load them manually, but it's around a thousand images.
I've got this working in VS just fine with the following
#if UNITY_5_3_OR_NEWER
#define NOESIS
#endif

#if NOESIS
using Noesis;
#else
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
#endif


namespace UIResources
{
    public partial class Icons : ResourceDictionary
    {
        private const float _scale = .156f;
        public Icons()
        {
            InitializeComponent();
            int count = 500;
            for (int file = 1; file < 35; file++)
            {
                for (int col = 0; col < 6; col++)
                {
                    for (int row = 0; row < 6; row++)
                    {
#if NOESIS
#else
                        ImageBrush newBrush = new ImageBrush();
                        newBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
                        newBrush.Stretch = Stretch.Fill;
                        newBrush.ImageSource = new BitmapImage(new Uri(String.Format("../../Assets/Resources/icons/dragitem{0}.png", file), UriKind.RelativeOrAbsolute));
                        newBrush.Viewbox = new Rect(col * _scale, row * _scale, _scale, _scale);
                        Add("icon" + (int)(count), newBrush);
                        count++;
#endif
                    }
                }
            }

            ;

        }


#if NOESIS
        private void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/NoesisGUI/Interface/Resources/Icons.xaml");
        }
#endif
    }

}
The equivalent of loading the Bitmap is what's got me confused on the Noesis side. I've seen references to TextureSource, haven't had any success with that though. I'd like to be able to load the pngs from a uri relative to the script if possible.
Thanks!
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Dynamically fill ResourceDictionary in Unity

18 Jul 2017, 12:34

Hi,

In Unity, images that are not directly referenced by a xaml asset need to be loaded through Resources.Load(). Then you can use TextureSource to create an image source from a Unity texture object:
for (int file = 1; file < 35; file++)
{
  var iconPath = String.Format("icons/dragitem{0}", file);
  var tex = UnityEngine.Resources.Load<UnityEngine.Texture2D>(iconPath);
  var source = new TextureSource(tex);
    
  for (int col = 0; col < 6; col++)
  {
    for (int row = 0; row < 6; row++)
    {
      ImageBrush newBrush = new ImageBrush();
      newBrush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
      newBrush.Stretch = Stretch.Fill;
      newBrush.ImageSource = source
      newBrush.Viewbox = new Rect(col * _scale, row * _scale, _scale, _scale);
      this["icon" + (int)(count)] = newBrush;
      count++;
    }
  }
}
Consider also to share the same TextureSource for all the icons that use the same Texture as I did.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 31 guests