- Home /
Creating an array of texture variables
Hello :) I'm trying to animate a GUITexture by switching the textures. I've been doing it the long way like so...
var HB001: Texture;
var HB002 : Texture;
var HB003 : Texture;
var HB004 : Texture;
Function Update()
{
var g_Health = gameObject.Find("g_Health");
if(HEALTH > 97)
{
g_Health.guiTexture.texture = HB001;
}
else if(HEALTH > 96)
{
g_Health.guiTexture.texture = HB002;
}
}
And this works fine, but I know I should be able to loop it. The main problem is generating the many textures (to be applied later in the inspector). I tried these:
var HBarr : Texture[] = new Texture[7];
And
for(var n = 0; n < 87 ; n++)
{
var HBarr : Texture[5];
}
And others but I can't get this to work. I still new to programming so I really appreciate the help. I have Sprite Manager 2 but I don't know if it can be used with GUItextures.
Thanks in advance!
why you need to define variables inside the for loop? also you'd adress the HBarr
with HBarr[5]
ins$$anonymous$$d of var HBarr : Texture[5]
. am i missing here something?
also not every frame takes equally long. if you need to have exact time on how long the textures have to be switched, you should use Time.deltaTime
within the update.
Do you mean it should be HBarr[5] : Texture; ? Because that doesn't work either.
no, if you have `` and assign textures to it like HBarr[0] = ...
etc.,
var HBarr : Texture[] = new Texture[7];
HBarr[0] = sometex; HBarr[1] = sometex2; ...
for(var n = 0; n < 7 ; n++) {
HBarr[n]; // doing
}
i have the slight impression that we're on two separate tracks. could you clarify your question a bit more? in that case i'm sorry to mess it up.
This doesn't work either, what would I need the 'sometex' for? I think we may be on different tracks yes.. What I'm trying to do is define an array of variable textures so that I then have a very long list to insert on the inspector of this script's object. This is above the update function.
owww i think i got you now. afaik there is no easy and convenient possibility of doing so, but you could write your own inspector behaviour (http://answers.unity3d.com/questions/14277/customizing-the-inspector-array-handling.html).
what i would do is, because i simply would be too lazy to customize the inspector, to load the textures within the code and store them onto your texture[] array inside a for loop.
Answer by Kith · Jun 13, 2011 at 12:57 AM
Funny that you mentioned this. I just finished implementing this same behavior in a game that I'm working on (http://www.youtube.com/watch?v=rGn1i_q4ApY). All the explosions there are done like this.
Basically, how I managed to do it was by creating the array (without specifying a size)
var explosionFrames : Texture[];
And then went into the inspector and started manually adding the textures. My Update function then looked like this.
function Update () {
timer++;
if ((timer >= playRate) && framePointer < explosionFrames.length - 1) {
framePointer++;
renderer.material.mainTexture = explosionFrames[framePointer];
timer = 0;
}
}
The timer variable just keeps track of how long it's been since the last frame was played. The framePointer keeps track of the array index (which frame in the animation we're at). You'll probably have to change the renderer.material.mainTexture line to fit your GUITexture needs, but the principle is the same.
-Kith
Ah great! That's exactly what I needed. The simplest too hehe.. how did I miss it... Anyway, many thanks, this will be most helpful. I will also doing my effects in the same way as you so double thanks :)
No problem! Always happy to help. Good luck with your game! -$$anonymous$$ith
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Loop through textures every half second 2 Answers
Changing texture of childrens 2 Answers
change texture of GuiButton in for loop 1 Answer
Loop trough list of textures and chek mouse position over 0 Answers