- Home /
How to change GUI.DrawTexture with OnMouseEnter function?
So I'm self teaching myself, and this is the first kink along the way, and I was wondering what is the best way for me change the texture that made with GUI.DrawTexture by using OnMouseEnter and OnMouseExit so that I can change the texture to something else when the mouse is hovered over it, and then reset when it exits?
Normally, if I was doing this using the scene itself, I'd have no problem with it, but for something done entirely in a script, I'm really confused.
// Allows you to disable to the gui when you want by setting this value to false.
var guiDraw : boolean = true;
// Allows you to set what texture you want the background to be.
var controlBackgroundTexture : Texture2D;
// Allows you to set what texture you want the start button to be when it is not entered in by the mouse.
var controlStartNotEnteredTexture : Texture2D;
// Allows you to set what texture you want the start button to be when it IS entered in by the mouse.
var controlStartEnteredTexture : Texture2D;
// Allows you to set what texture you want the exit game to be when it is not entered in by the mouse.
var controlExitNotEnteredTexture : Texture2D;
// Allows you to set what texture you want the exit game to be when it IS entered in by the mouse.
var controlExitEnteredTexture : Texture2D;
// Begins drawing the GUI.
function OnGUI(){
// Checks to see if the GUI is enabled, and if it is, it will display it.
if(guiDraw == true){
// This draws the background centered and scaled to be on any resolution.
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), controlBackgroundTexture, 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), controlStartNotEnteredTexture, ScaleMode.ScaleToFit, true, 0) && {
function OnMouseEnter(){
guiTexture.Texture = controlStartEnteredTexture;
}
}
// 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), controlExitNotEnteredTexture, ScaleMode.ScaleToFit, true, 0);
}
}
Now, if you tell me that I should just use OnMouseExit before GUI.DrawTexture, I'm seriously going to slam my head in my desk, because I might make this a little more difficult that what I am actually thinking of.
Answer by Jamora · Jul 17, 2013 at 11:03 AM
OnMouseEnter and OnMouseExit should be separate functions. Unity will call them for you when appropriate.
What you should do is something like:
var currentTexture : Texture2D;
function OnGUI(){
GUI.DrawTexture(Rect(/*values*/),currentTexture)
}
function OnMouseEnter(){
currentTexture = controlStartEnteredTexture;
}
function OnMouseExit(){
currentTexture = controlExitNotEnteredTexture;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110237im_/https://answers.unity.com/themes/thub/images/avi.jpg)