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 Grasshorse · Sep 11, 2011 at 06:26 PM · texturebuttonvariablesdynamiceval

How I do something like eval() but not eval() ?

Hello,

I'm working on a project where I want to dynamically load textures into Unity at runtime and apply them to buttons. It's pulling all of the texture names out of the Resources directory just fine, and I have all of the arrays the texture names without extensions. I have even built the commands so that print() them and cut and paste the generated code into the program it works fine. So when I try and execute the variable that contains the entire command I want to execute, nothing happens. It doesn't create the Textures, and certainly doesn't assign them.

Now the only way I know of executing a variable as a command in Unity is eval(). I know that it is a command that is best left alone, but I don't know anything that is better. Anyone have any ideas?

Best,

Steve

 for(var p:int = 0;p < textureNameArray.length; p++)
 {
     for( var q:int = 0; q < textureArray.GetUpperBound(0); q++)
     {
         if(textureNameArray[p] == textureArray[q,0])
         {
             command = ("var " + textureArray[q,0] + "Texture" + q + ":Texture = Resources.Load(\"" + textureArray[q,1] + "\", Texture) ;");
             print(command);
             eval(command);
         }
     }
 }
 
 // This is a sample of the code that is output as a result.
 var hexesTexture26:Texture = Resources.Load("hexes_tape_1_icon", Texture) ;

Adding some upadated code.

 for(var p:int = 0;p < textureNameArray.length; p++)
 {
     for( var q:int = 0; q < textureArray.GetUpperBound(0); q++)
     {
         if(textureNameArray[p] == textureArray[q,0])
         {
             textureName = textureArray[q,0] + "Texture" + q;
             print("textureName " + textureName);
             textureFileName = "\"" + textureArray[q,1] + "\"";
             print("textureFileName " + textureFileName);
             MakeTexture(textureName,textureFileName);
         }
     }
 }
 
 function MakeTexture(makeTextureNameID,makeTextureFileNameID)
 {
     print("makeTextureNameID " + makeTextureNameID);
     print("makeTextureFileNameID " + makeTextureFileNameID);
     var makeTextureNameID : Texture = Resources.Load(makeTextureFileNameID,Texture);
 }
Comment
Add comment · Show 3
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 Eric5h5 · Sep 11, 2011 at 08:13 PM 3
Share

Why do you need to use eval(), or anything like it? Just make a function and run that.

avatar image Grasshorse · Sep 11, 2011 at 09:04 PM 0
Share

Ha, thank you Eric. That must seem totally obvious to you and I could never figure out. Big duh on my part. $$anonymous$$any many thanks!

avatar image Grasshorse · Sep 11, 2011 at 11:00 PM 0
Share

Well shoot, I thought that I understood, but I guess I didn't. From the error I am getting, and the troubleshooting I have done, the error is on the line where the var is created. It is interpreting "makeTextureNameID" as a string(makeTextureNameID), and not the dynamic variable passed into the function. I seem to run into this issue all the time and can't wrap my head around it. If anyone has any ideas I would love to hear them.

Best, Steve

1 Reply

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

Answer by Peter G · Sep 12, 2011 at 12:43 AM

I understand your intentions, its just how you're going about it is a bit off. ;)

It sounds like you really need to be storing all your textures in a multidimensional array. It looks like you start to do that with textureArray but then you leave that behind and start trying to generate code on the fly. To actually do what you want, you need reflection, but that's cumbersome and really isn't needed here. I'm not sure why you can't do something like:

 var textures = new Texture[ x , y ];
 
 function FillTextureArray () {
      for( var i = 0 ; i < x ; i++) {
           for( var j = 0 ; j < y ; j++) {
                textures[ i , j ] = Resources.Load( "Texture" + i.ToString() + j.ToString() , Texture);
                // in textures[1,2] goes "texture12" or whatever similar naming scheme you have.
           }
      }
 }

As long as I understand what you're doing this sounds easier and faster.

Comment
Add comment · Show 1 · 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 Grasshorse · Sep 13, 2011 at 02:56 PM 0
Share

Got it! I never understood that other Objects(such as Textures) could be stored in an Array other than String, int, float, boolean, etc. That was what I just wasn't processing(pun fully intended). $$anonymous$$any thanks for showing me this! I hope it helps others as well.

Steve

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

Show images on buttons from an example 1 Answer

Assigning UV Map to model at runtime 0 Answers

Need help with creating dynamic textures. 1 Answer

Button which I can choose the exact looks on 1 Answer

Button on one scene changes variable on next scene 1 Answer


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