- Home /
How do I make that when I hover the mouse over GUI Texture it changes color?
Can someone please help me? I'm using function OnMouseOver() #pragma strict
function Start () {
}
function Update () {
}
function OnMouseEnter() {
renderer.material.color = Color.red;
}
However I am supposed to attach some renderer thing? Please help and also please don't answer in the comments
Answer by YoungDeveloper · Jun 27, 2014 at 03:46 PM
Here's a simple C# example which will start you going.
private Rect rect;
private Event EVENT;
private void Start(){
rect = new Rect (0,0,100,100);
}
private void OnGUI(){
EVENT = Event.current;
if(rect.Contains(EVENT.mousePosition)) GUI.backgroundColor = Color.green;
else GUI.backgroundColor = Color.red;
GUI.Button (rect, "");
//resets color to default
GUI.backgroundColor = Color.white;
}
Theoretically, you can check does the rect contains mouse in update, but for that you will have to use Input.mousePosition and fix y, as Input (0,0) starts from other corner. Like this
//This will return mouse position in GUI "normal" coordinate system
private Vector2 FixedInputMousePosition(){
Vector2 pos = Input.mousePosition;
pos.y = Screen.height - pos.y;
return pos;
}
//And check it like this
private void Update(){
if(rect.Contains(FixedInputMousePosition())){
//Mouse is over rect
}
}
Answer by Macmini1 · Jun 27, 2014 at 03:54 PM
You might have to put a Box Collider on the GUI Texture.
Answer by koray1396 · Jun 27, 2014 at 04:16 PM
you can simply use the following and you can change mouse events as you wish;
http://docs.unity3d.com/ScriptReference/Input.html
GUITexture exampleGui;
if(exampleGui.HitTest(Input.mousePosition)){
if(!Input.GetMouseButton(0)){
exampleGui.color = Color.red;
}
}
Cool! But it just stays there... How can I do it so that when the mouse gets "off" it turns back into the normal color?
Answer by hav_ngs_ru · Jun 27, 2014 at 06:05 PM
You may draw GUI Button instead of GUI Texture, and just customize "normal" and "hover" styles as you wish. it will change it's view automatically.
And make you "active" style the same as "normal" style to make is looks not like button when user press it.
Your answer
Follow this Question
Related Questions
Android Button Screen 1 Answer
Developing a Dynamic GUI 1 Answer
Which are the computer requisites to develop in Unity3D? 1 Answer
GUI.DrawTexture isn't centred. Help? 1 Answer