Hi all,
I’m trying to change the filename of a GUITexture, but I can’t get it to work the way I want:
var someTexture : Texture2D;
fileTex = texobject[1]; //var with filename (1.jpg)
pasfotoObj = GameObject.Find(texobject[0]+“/GUI_p_pasfoto”);
pasfotoObjTex = pasfotoObj.GetComponent(GUITexture);
pasfotoObjTex.texture = someTexture;
The code above actually works fine, but I have to:
A. Drag’n’drop material in the editor
B. Change the entire material
What I want is to ONLY change the image-file the material is using, ie. something like this:
pasfotoObjTex.textureFileName = “images/1.jpg”;
And I want to do it using only scripting, no drag’n’drop. This way I can use one guitexture-object to dynamically display a lot of different pictures.
Is this possible? And if so, how?
The thing is that Unity is very smart about including only those assets that are actually used when building a player. We do this by always enforcing references to objects, if you had path names in your scripts this wouldn’t be possible and we would have to include all assets in the project folder when building a player.
You can do use arrays though, then you can assign all your textures to this array in the inspector.
var textures : Texture[];
function Start ()
{
var foundTexture : Texture;
for (var tex in textures)
{
if (text.name == "1")
{
foundTexture = tex;
break;
}
}
pasfotoObjTex = pasfotoObj.GetComponent(GUITexture);
pasfotoObjTex.texture = foundTexture;
}
Or you can use the WWW class to get your jpgs from a website instead, in that case you can do it completely path name based.
hmm, okay - I guess I have to do it the hard way, then.
though, I think that it would be cool if it was possible in some way to explicitly assign a range of pictures that can be used dynamically - maybe a way to force Unity to include specific files in the final build, which can then be accessed dynamically.
anyway, thanks for the help (again) 
You could write a short Editor script to automatically assign the array in the Inspector using path names and EditorUtility.FindAsset()
http://unity3d.com/Documentation/ScriptReference/EditorUtility.FindAsset.html
-Jon