- Home /
Popup Textures for game objects
I'm trying to design something where you can click on a game object and an image pops up, but then you can click an arrow and go to the next image, all in one click on the game object. I have this code which pops up a single image, but I want to incorporate more. Any help is appreciated!
var popupTexture : Texture2D; private var paperPopup : PaperPopup;
function Start () { paperPopup = FindObjectOfType(PaperPopup);
if (popupTexture == null) {
popupTexture = renderer.material.mainTexture;
}
}
function OnMouseDown () { paperPopup.Show(this); }
Answer by Cherno · Oct 09, 2013 at 11:20 PM
Sounds pretty simple :)
All you have to do is set a boolean to true if you click on the object, and then inside OnGUI() make your textures, buttons etc. appear if the boolean is set to true.
Thanks for the help! I'm really new to Unity, so I'm not sure what you mean by how to make my textures, etc. appear if the boolean is true. Can you give me more specifics?
Here is a comprehensive example.
//Declare variables
var ShowButtons : boolean;
var Image1 : Texture2D;//Drag an image from the project view here.
function Update()
{
var ray = Camera.main.camera.ScreenPointToRay (Input.mousePosition);//declares that our ray will go out from the main camera straight forward towards the screen
var hit : RaycastHit;//stores information about things we hit with our ray
if(Input.GetButtonDown("Fire1"))//Fire1 = Left mouse button (by default)
{
if (Physics.Raycast (ray, hit, $$anonymous$$athf.Infinity))//draws our ray, stored information about anything the ray hits in "hit", and goes on infinitely
{
ShowButtons = true;
}
else
{
ShowButtons = false;
}
}
}
function OnGUI()//the function where all GUI stuff is hapening. Similar to Update() in that it is called constantly, but unlike Update(), it is called multiple times per frame so drawn images don't flicker
{
if(ShowButtons == true)
{
GUI.DrawTexture (Rect(100, 100, Image1.width, Image1.height), Image1);//draw our image1
var ButtonZone1 : Rect = new Rect(100, 100 + Image1.height, 50, 50);//declare where our button1 is and how big it is
if (GUI.Button(ButtonZone1,"Button Zone 1"))//if a button that has the position and dimension of our ButtonZone1 Rectangle variable is clicked...
{
if (Event.current.button == 0)//...and the mouse button is the left one (0 = left, 1 = right, 2 = middle)
{
Debug.Log("Clicked Button 1");//... then print a message in the console.
}
}
var ButtonZone2 : Rect = new Rect(150, 100 + Image1.height, 50, 50);
if (GUI.Button(ButtonZone2,"Button Zone 2"))
{
if (Event.current.button == 0)
{
Debug.Log("Clicked Button 2");
}
}
}
}
Your answer
Follow this Question
Related Questions
How to change texture of another object by clicking on a object? 0 Answers
How can I change the texture of a game object by clicking on another game object? 1 Answer
OnDestroy collision detect 4 Answers
Stop a Lerp from looping 3 Answers
OnMouseUp error 0 Answers