Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zinnfandel · Jun 12, 2011 at 11:26 PM · textureloopspritemanager

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zwacky · Jun 12, 2011 at 11:40 PM 0
Share

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.

avatar image zinnfandel · Jun 12, 2011 at 11:48 PM 0
Share

Do you mean it should be HBarr[5] : Texture; ? Because that doesn't work either.

avatar image zwacky · Jun 13, 2011 at 12:01 AM 0
Share

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.

avatar image zinnfandel · Jun 13, 2011 at 12:10 AM 0
Share

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.

avatar image zwacky · Jun 13, 2011 at 12:29 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zinnfandel · Jun 13, 2011 at 01:23 AM 0
Share

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 :)

avatar image Kith · Jun 13, 2011 at 01:28 AM 0
Share

No problem! Always happy to help. Good luck with your game! -$$anonymous$$ith

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges