- Home /
GUI texture to change texture when hovered over or clicked?
Trying to get a script working for GUI where's the GUI texture will change when I've hover'd over it etc, just wondering how'd I'd go about doing that?
Not to sure if I'd use OnGUI() or OnMouseEnter etc.
I'm using C# Thanks if you can help!
Depends. How does your current GUI work? There are ways of making this happen for any method, really, but I'd like to know your specifics before I can tell you one of them. If it's just UnityGUI (as goes inside of OnGUI) I'd use a custom GUIStyle which had a different texture for 'hover' in the background.
At the moment, I don't have anything for the GUI. I have a image without a blur at the back and one that makes it shine for when it is hovered, I just don't know the script on how to change textures.
Well, just use OnGUI for that stuff. All the functionality is pretty much there for you already! It's not a script exactly, you define a custom GUIStyle in your script at the top, like
public GUIStyle buttonStyle;
then set it all up in the inspector (including dragging images in from the Project window).
Answer by natestale · May 01, 2014 at 08:52 PM
I dont know if this is useful (2014) but, if i reach this place looking for the same question, im sure that it is going to help another people, so here i go, this is the solution for HOVER and CLICK using UnityScript (Javascript). Hope i can help anyone ;)
var normalTex : Texture2D;
var hoverTex : Texture2D;
function OnMouseEnter () {
guiTexture.texture = hoverTex;
}
function OnMouseExit(){
guiTexture.texture = normalTex;
}
function OnMouseDown(){
Debug.Log("clicked");
}
Answer by mrapple · Nov 03, 2011 at 05:37 AM
Update;
This is what I'm using to test, I've got some other scripts setup etc, but thats irrelevant, what I want to know is how to change a texture to another texture when the mouse is over it.
This is what I tried, which gives no errors but doesn't work.
public Texture2D hoverTexture;
public Texture2D NormalTexture;
public Texture2D PressTexture;
void OnMouseEnter()
{
NormalTexture = hoverTexture;
print("EnteredArea");
}