- Home /
function GUI.DrawTexture with OnMouseEnter/Exit to change texture all within script?
Basically, I want to make a title screen all within a script so that I can set a variable to turn off the GUI and go straight into the game without loading another level. Basically an intro all within the title screen.
When I draw the GUI, and when the mouse enters currentTexture, it will set the value to the variable of controlStartEnteredTexture. If I want to save space and do it in all one script, then would I make variables for each element, and then make more functions for OnMouseEnter and OnMouseExit, or can I say it both statements, but start a new line and code there? Here's what I got and I'll tell you if it worked after I code it.
// These are the place holder variables that hold the start texture, and will change upon mouse enter/exit.
var currentStartGameTexture : Texture2D;
var currentExitGameTexture : Texture2D;
// These variables define what the textures should change into.
var hoverStartGameTexture : Texture2D;
var hoverExitGameTexture : Texture2D;
// These variables define what the current textures will change into when you aren't hovering over it.
var standStartGameTexture : Texture2D;
var standExitGameTexture : Texture2D;
// This texture is what makes the background.
var backgroundTexture : Texture2D;
function OnGUI(){
// This draws the background centered and scaled to be on any resolution.
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), backgroundTexture, ScaleMode.ScaleToFit, false, 0);
// This draws the start button to be centered on any resolution.
GUI.DrawTexture(Rect(Screen.width/2.5, Screen.height/2.5, Screen.width/2, Screen.height/2), currentStartGameTexture, ScaleMode.ScaleToFit, true, 0);
// This draws the exit button to be centered on any resolution.
GUI.DrawTexture(Rect(Screen.width/2.5, Screen.height/1.6, Screen.width/2, Screen.height/2), currentExitGameTexture, ScaleMode.ScaleToFit, true, 0);
}
// OnMouseEnter will change the current texture to the hover texture based on what it is being hovered over.
function OnMouseEnter(){
// This is the start game icon that will change when the mouse is hovered over it.
currentStartGameTexture = hoverStartGameTexture;
// This is the exit game icon that will change when the mouse is hovered over it.
currentExitGameTexture = hoverExitGameTexture;
}
// OnMouseExit will change the current texture to the stand texture.
function OnMouseExit(){
// This is the start game icon that will change the current texture to the stand texture.
currentStartGameTexture = standStartGameTexture;
// This is the exit game icon that will change the current texture to the stand texture.
currentExitGameTexture = standExitGameTexture;
}
Nothing happens in game, so I assume that didn't work, so now I am going to see if it will change if I only render only a single GUI at once with one script and see if it changes. I will post that script below.
function OnGUI(){
// This draws the start button to be centered on any resolution.
GUI.DrawTexture(Rect(Screen.width/2.5, Screen.height/2.5, Screen.width/2, Screen.height/2), currentStartGameTexture, ScaleMode.ScaleToFit, true, 0);
}
// OnMouseEnter will change the current texture to the hover texture based on what it is being hovered over.
function OnMouseEnter(){
// This is the start game icon that will change when the mouse is hovered over it.
currentStartGameTexture = hoverStartGameTexture;
}
// OnMouseExit will change the current texture to the stand texture.
function OnMouseExit(){
// This is the start game icon that will change the current texture to the stand texture.
currentStartGameTexture = standStartGameTexture;
}
That doesn't work either. I don't think OnMouseEnter/Exit is referring to the image at all, which is why it's not working. It's just stating that OnMouseEnter... blablabla.
A fully coded GUI that I am able to change the textures when the mouse hovers over that GUI element is a lot harder than I thought.
Another issue is that if I add a boolean statement before a the function OnGUI, it will give me several errors. Am I not allowed to put anything before a function? Just so you know, I'm new to scripting and I'm self learning, so it might be a stupid question.
Your answer
Follow this Question
Related Questions
How do I create a GUI on collision detection? 1 Answer
GUI.Window function with variables? 3 Answers
Display GUI when function is called 2 Answers
Button is unable to grab functions with two parameters? 0 Answers
Variable won't change, any suggestions? 2 Answers