- Home /
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;
}
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
Thanks alucardj 59. Didn't know that. $$anonymous$$uch better...
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; }
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
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?
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.
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...
}
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.
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
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];
}
Your answer
Follow this Question
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