Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by DFLY · Apr 20, 2013 at 08:48 AM · texturestringconvert

convert a string to texture?

Hi,

Is there a way to choose from a list of textures in an object using a string to choose it?

I have 10 textures that will be loaded into a script in the editor, and I want to choose one at any one time to be the current texture.

I try but get the error: Can not convert 'String' to 'UnityEngine.Texture'

I have put my script below. I read in the Forum that I could put the textures in an array instead, but I have no Idea how to load textures into arrays. I think that would be more complicated than what I am trying here.

 //this is a variable to receive a string from elsewhere
 
 
 static var digit : String = "t0";
 
 //will load a choice of 10 textures in editor
 
 
 var t1 : Texture;
 var t2 : Texture;
 var t3 : Texture;
 var t4 : Texture;
 var t5 : Texture;
 var t6 : Texture;
 var t7 : Texture;
 var t8 : Texture;
 var t9 : Texture;
 var t0 : Texture;
 
 // currentTexure will be used to load that textue onto object
 
 
 private var currentTexture : Texture;
 
 
 
 function DigitDisplay () {
     
     currentTexture = digit;
 
 gameObject.renderer.material.mainTexture = currentTexture;
 }
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 AlucardJay · Apr 20, 2013 at 10:35 AM 1
Share

please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.

With a karma of 75, you should know this.

Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image DFLY · Apr 20, 2013 at 11:35 AM 0
Share

Thanks alucardj 59. Didn't know that. $$anonymous$$uch better...

avatar image DFLY · Apr 22, 2013 at 02:10 PM 0
Share

So disappointing. I can't believe that I can't just call the texture using its name (via string). I ended up having to use if statements. Surely there must be a way to insert a string as a name and not a string.

Anyway this is what I did:

pragma strict

//this is a variable to receive a string from elsewhere static var digit : String = "0";

//will load a choice of 10 textures in editor var t1 : Texture; var t2 : Texture; var t3 : Texture; var t4 : Texture; var t5 : Texture; var t6 : Texture; var t7 : Texture; var t8 : Texture; var t9 : Texture; var t0 : Texture;

// currentTexure will be used to load that textue onto object private var currentTexture : Texture;

function DisplayDigit () { if (digit == "0"){ currentTexture = t0;} if (digit == "1"){currentTexture = t1;} if (digit == "2"){currentTexture = t2;} if (digit == "3"){currentTexture = t3;} if (digit == "4"){currentTexture = t4;} if (digit == "5"){currentTexture = t5;} if (digit == "6"){currentTexture = t6;} if (digit == "7"){currentTexture = t7;} if (digit == "8"){currentTexture = t8;} if (digit == "9"){currentTexture = t9;}

      gameObject.renderer.material.mainTexture

= currentTexture; }

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by kilian277 · Apr 20, 2013 at 09:30 AM

You can use an array of textures just like this (C# !)

 public Texture[] textures;

Javascript

 public textures : Texture[];

Then you can use

 currentexture = textures[digit];

to assign the texture (digit needs to be an INTEGER).

In the editor you can assign the textures to the texture array.

Hope it helps

Comment
Add comment · Show 5 · 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 DFLY · Apr 20, 2013 at 12:57 PM 0
Share

Thanks for the help, but I really want to know how to use the string to select the texture. I can manually name the texture I want in the script and that works. So how do I convert a string to a texture name?

avatar image fafase · Apr 20, 2013 at 01:02 PM 0
Share

A texture name is a string. But the index of an array has to be an integer. That is why I offered to use a dictionary which seems to be exactly what you need.

avatar image kilian277 · Apr 20, 2013 at 02:13 PM 0
Share

What you also can do is check what the digit variable is with a case or if condition and then based on that assign the correct texture.

 if(digit=="t1")
 {
      currentTexture = t1;
 }

or

 switch(digit)
 {
    case : "f1"
      currentTexture = t1;
      break;
    case : "f2"
     etc...
 }
avatar image fafase · Apr 20, 2013 at 05:02 PM 0
Share

You really do not want to make it efficient...ok I am preaching my own parish (wonder if that has a meaning in english...) but Dictionary are using hashtables that are way faster than any switch case even more when they are based on string which requires a call for a string compare method.

If you want to use a switch, at least use an enumeration which is based on integer.

avatar image kilian277 · Apr 20, 2013 at 05:14 PM 0
Share

If he just used yours or my solution then all of this isn't necessary also i just gave an example on how to do it , the op can adjust it to his needs

avatar image
3

Answer by fafase · Apr 20, 2013 at 12:36 PM

You could create a Dictionary.

 import System.Collections.Generic;

 var myDictionary = new Dictionary.<string,Texture2D>();
 
 myDictionary.Add(t1.name,t1);
 myDictionary.Add(t2.name,t2);
 
 //and so on
 
 function DigitDisplay (string str) {
    return myDictionary[str];
 }
Comment
Add comment · 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

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert a string to int array in Unity C# 1 Answer

Converting 1s and 0s to float 1 Answer

Looking for a javascript unity implementation of FromCharCode? 0 Answers

how to change a gui button texture 1 Answer

parseInt givin' me issues! 2 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