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. Much better...

2 Answers

2

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

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];
}