Remove or Add Blank Images to GUI.DrawTexture in Unity 3D
I got my code to work correctly but I can't seem to remove GUI.DrawTexture if on press on another GUI button that not on the previous or next button. How can I remove the texture or add a blank texture to the GUI.DrawTexture once I press the 3D Model Gui button?
#pragma strict
var images : Texture2D[];
var i : int = 0;
public var objectToHide:GameObject;
function OnGUI () {
GUI.DrawTexture(Rect(365,20,1500,900),images[i]);
if (GUI.Button(new Rect(880,930,100,50),"PREVIOUS"))
{
if (i > 0)
i--;
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
objectToHide.SetActive(false);
}
if (GUI.Button(new Rect(1000,930,100,50),"NEXT"))
{
if (i < images.Length - 1)
i++;
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
objectToHide.SetActive(false);
}
if (GUI.Button(new Rect(1120,930,100,50),"3D MODEL"))
{
objectToHide.SetActive(true);
}
}
Answer by Statement · Oct 25, 2015 at 05:33 PM
Keep state of your behaviour.
var showImage = true;
And if showImage is true, display the texture...
if (showImage)
GUI.DrawTexture(Rect(365,20,1500,900),images[i]);
If we click 3D model, hide the texture.
if (GUI.Button(new Rect(1120,930,100,50),"3D MODEL"))
{
showImage = false; // set a boolean
objectToHide.SetActive(true);
}
I've got some experience with Unitys IMGUI and I can tell you it'll get boring pretty quickly if you don't make an object model wrapper around IMGUI calls because you'll end up juggling so many state variables and cook up a nice bowl of ramen code.
But now that Unity has a new UI system, why are you using the old system? You don't appear to be writing editor extensions.
Thanks you! That really worked! Originally, I have it working with Unity new UI system, but I couldn't apply the next-previous images without the hard coding. I didn't know how to setup a next - previous in the new UI system.