[solved] Load PNGs at runtime to texture GUI buttons

Hello,

I came across an issue today on something I thought would be very straightforward.

I’m looking into loading a bunch of PNG files whose names are stored in a XML doument. These files can be either stored in an external folder or loaded in Unity’s asset directory beforehand.

I simply want to use these files as textures to create buttons for a GUI interface. My script already loads the content of the XML file and creates the buttons for the GUI. I just need to bridge the gap now :slight_smile:

I had a look at the dedicated page Unity - Manual: IMGUI Basics but I couldn’t find any relevant help. Nothing more on the forums unfortunately.

Can anyone help me with that? I doubt it’s a major issue to solve but honestly, I’m lost!

Cheers,

D.

Look in the docs for the WWW class. You can use it to load your textures at runtime using file://.

Using C# to load PNG/JPG to texture:

// "Empty" texture. Will be replaced by LoadImage
Texture2D texture = new Texture2D(4,4);

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] imageData = new byte[fs.Length];
fs.Read(imageData, 0, (int) fs.Length);
texture.LoadImage(imageData);

If using JavaScript, you can find good starting point in docs:

http://unity3d.com/support/documentation/ScriptReference/Texture2D.LoadImage.html

BR,
Juha

@Quietus: I thought of this solution but I couldn’t think there was not a more direct way.

@JFo: I implemented that and it worked perfectly. I’m starting with C# and I still feel a bit shy using the old C way of thinking!

Thanks to both of you anyway for your replies.

D.