- Home /
custom mouse changing when hovering over objects
hi i have a custom mouse for my unity game but i want it to when hovering over something to change to a different mouse cursor here is my code for the mouse cursor so far:
using UnityEngine;
using System.Collections;
public class CursorIcon : MonoBehaviour
{
public static int guiDepth = 0;
public Texture2D cursorImage;
private int cursorWidth = 32;
private int cursorHeight = 32;
void Awake() {
DontDestroyOnLoad(this);
}
void Start()
{
Screen.showCursor = false;
}
void OnGUI()
{
GUI.depth = guiDepth;
GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
}
void Update() {
}
}
any help will be appreciated thank you.
Answer by Cyb3rManiak · May 08, 2011 at 11:48 AM
You can use the Rect.Contains method to detect if the mouse is over a specific area of the screen/GUI and react accordingly...
Something like:
var btnTexture : Texture; var btnCursor1 : Texture; var btnCursor2 : Texture; var rectForButton: Rect = Rect (10, 10, 150, 150);
if (rectForButton.Contains(Input.mousePosition)) cursorImage = btnCursor2; else cursorImage = btnCursor2;
GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
Your answer
Follow this Question
Related Questions
Cursor disappears in unity. 1 Answer
Custom GUI Button is Black 1 Answer
Waste of time with gui ... hover and behind 2 Answers
Situational GUI / GUI on mouse over? 4 Answers
Custom-made GUI Textfield 1 Answer