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
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
}
}
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.
you can simply use the following and you can change mouse events as you wish;
GUITexture exampleGui;
if(exampleGui.HitTest(Input.mousePosition)){
if(!Input.GetMouseButton(0)){
exampleGui.color = Color.red;
}
}
You might have to put a Box Collider on the GUI Texture.