How can I load an image into a GUITexture programatically? I just cant figure it out from the docs, Im afraid ![]()
After attaching a GUITexture to a gameobject, you can access it through scripting via guiTexture.texture like:
var myCustomImage : Texture2D;
guiTexture.texture = myCustomImage;
Thanks, but what if you want to make an array containing a lot of images and swap between them in the GUITexture (ie make a simple 2d animation), how would you go about doing that?
In other words, what Im trying to achieve is to learn how to make animated icons in a way that doesnt hog too much resources.
Ok, I loaded a couple of images into a texture-array and used it in conjunction with my timer that executes every second for any specific time I want. I also made a little algorithm to let me have less images than the total number of seconds that the animation will play. This way I can have 5 images show every two seconds in a ten second animation, or 3 images show every 4 seconds in a 12 second animation, and so forth…
var n_TimeLimit = 10;
var GuiTexture : GUITexture;
var arr_GuiTextureImages : Texture2D[]; // Array of images that will go into GuiTexture
function fn_TimeLimit_Timer ()
{
// Init: Array counter. Starts at 0, and adds one each time an img loads
var n_TextureNr = 0;
// Get: Nr of images in texture-array
var n_NumberOfTextures = arr_GuiTextureImages.length;
// Init: First texture in array
GuiTexture.texture = arr_GuiTextureImages[0];
// Loop: Until timer runs out
for (var n = 1; n <= n_TimeLimit; n++)
{
// Wait: 1 second
yield WaitForSeconds (1);
// Check: Change image every (total seconds) / (nr of images) second
if ( (n_NumberOfTextures / n) == 1)
{
// Set: Texture
GuiTexture.texture = arr_GuiTextureImages[n_TextureNr];
// Increase: Textures
n_TextureNr ++;
// Double: Number of textures
n_NumberOfTextures += n_NumberOfTextures;
}
}
}
Perhaps it will be of some help to somebody. Its working fine for me at least ![]()
Hi Danneman
I saw your script and needed a bit of assistance. Well how do i play the animation, the script is perfect i can add images on it, but I wanted to know how to play the animation, cause on play, the texture image dose not change.
Thanks
Theron
You just need to call the fn_TimeLimit_Timer function to set the animation running. It’s actually a coroutine, so it will run in parallel with normal script operation.
I tried calling this animation script but it gives me an error.
var animationtest : Animation_test;
function update()
{
animationtest.fn_TimeLimit_Timer() ;
}
animation test is the script im using.
What error do you get? If the script is exactly the same as what you’ve pasted then the main problem is with the Update function. The name must be spelt with a capital U (ie, “Update”, not “update”).