Dynamically fill ResourceDictionary in Unity
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
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!
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
Code: Select all
#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
}
}
Thanks!
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: Dynamically fill ResourceDictionary in Unity
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:
Consider also to share the same TextureSource for all the icons that use the same Texture as I did.
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:
Code: Select all
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++;
}
}
}
Who is online
Users browsing this forum: Ahrefs [Bot] and 11 guests