- Home /
How can i show the texture in the array?
I have this script and i am wondering how i can display the texture that is in this array. The script i have is:
var personArray : Array = [myPerson];
var myTexture : Texture;
class myPerson
{
var name = "Brandon";
var texture : Texture;
}
function Start ()
{
texture = myTexture;
}
function OnGUI ()
{
GUI.Button(Rect(0, 0, 100, 100), personArray[0]);
}
Answer by flamy · Feb 21, 2013 at 04:56 AM
well the above answer solves the problem of handling texture with arrays, but it is not a good practice to link textures to scripts from editor, since they will stay in memmory as long as the gameObject is in the scene... For an instance 20 of 4mb texture will hold 80 mb in RAM for nothing.
Better practice is to use string arrays of path to texture and use Resources.Load() function to load them when needed. It will be automatically unloaded by garbage collector or you can use this function to do that manually.
This suggestion becomes completely useless if you have to use all of the textures at once. But just remember that objects(not only texture) that are attached to editor will stay in memory until the gameobject exists.
@vijayakumar (flamy) . Thanks for sharing your knowledge. Its really helpful to me as a beginner. If u dont $$anonymous$$d can you explain with an example. i tried child.renderer.material.mainTexture = Resources.Load("halowin_pumkin-off") as Texture; but no texture showing.
$$anonymous$$ake a folder called Resources in your project and place the texture inside that folder. if it is inside folder of heiraricy Resources/UI/mytexture you have to pass UI/mytexture, I hope this is clear.
@flamy: cool.its work. while clicking on each object, same texture will load. is it make memory lose ? how can i unload?
one good thing about Resources.Load is, if it is in memory already it wont load again, it will just give a reference to it. To unload you can you use Resourcs.UnloadAsset(); or use Resources.UnloadUnusedAsstes();
#pragma strict
var personArray : String[];
var $$anonymous$$yTexture : Texture;
var imageIndex :int;
function Start()
{
imageIndex=0;
$$anonymous$$yTexture= Resources.Load(personArray[0]) as Texture;
}
function OnGUI()
{
if(GUILayout.Button($$anonymous$$yTexture)) //changes texture
{
Resources.UnloadAsset($$anonymous$$yTexture);
imageIndex++;
if(imageIndex>=personArray.Length)
imageIndex=0;
$$anonymous$$yTexture = Resources.Load(personArray[imageIndex]) as Texture;
}
}
Answer by sona.viswam · Feb 21, 2013 at 04:07 AM
Declare array of textures.
var personArray : Texture[];
From scene you can give array length and just drag and drop your textures to the elements of array (public parameters).
Then you call that texture as
function OnGUI ()
{
GUI.Button(Rect(0, 0, 100, 100), personArray[0]);
}
HTH..
But is there a way to do it with the array that i already created? Sorry im new to arrays.
Can i just use the array that i have and pull out that texture?
Your answer
Follow this Question
Related Questions
Applying different textures to spawned objects. 2 Answers
Aspect ratio keeping guitexture scaling 1 Answer
Menu material error 1 Answer
drawing GUI or Instantiate an object to highlight multiple object 2 Answers
What type of Array should I use? 1 Answer