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);
}
Why do you need to use eval(), or anything like it? Just make a function and run that.
– Eric5h5