- Home /
Rotate GUI custom mouse cursor?
This is my Script:
var yourCursor : Texture2D; // Your cursor texture
var cursorSizeX : int = 16; // Your cursor size x
var cursorSizeY : int = 16; // Your cursor size y
function Start()
{
Screen.showCursor = false;
}
function OnGUI()
{
GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX/3, Event.current.mousePosition.y-cursorSizeY/3.5, cursorSizeX, cursorSizeY), yourCursor);
}
I want mouse cursor rotate itself always when I Play the game. Could anyone help me :(
Thanks for your kind!
Answer by robertbu · Dec 21, 2013 at 05:34 PM
GUI elements can be rotated using GUIUtility.RotateAroundPivot(). Here is some example code. Note I've used a more traditional calculation for x and y. I'm assuming your calculations were so that the cursor position was offset from the middle of the texture. You'll have to adapt the 'pivot' calculation for whatever cursor calculation you use.
var yourCursor : Texture2D; // Your cursor texture
var cursorSizeX : int = 16; // Your cursor size x
var cursorSizeY : int = 16; // Your cursor size y
var speed = 90.0;
private var angle = 0.0;
function Start()
{
Screen.showCursor = false;
}
function Update() {
angle += Time.deltaTime * speed;
angle = angle % 360.0;
}
function OnGUI()
{
var matx = GUI.matrix;
var x = Event.current.mousePosition.x-cursorSizeX/2.0;
var y = Event.current.mousePosition.y-cursorSizeY/2.0;
var pivot = Vector2(x + cursorSizeX / 2.0,y + cursorSizeY / 2.0);
GUIUtility.RotateAroundPivot(angle, pivot);
GUI.DrawTexture (Rect(x, y, cursorSizeX, cursorSizeY), yourCursor);
GUI.matrix = matx;
}
Hi, thanks!!! Your script is working!!!
But how can I change the rotate pivot to ↑ picture's rotate direction (X,Z)? Could you $$anonymous$$ch me (:
Your picture did not post. I need to see what you are talking about. Posting the cursor itself would also be helpful.
Ahhhh. I've never seen code for this kind of problem. You might be able to directly manipulate the GUI.matrix to solve this problem, but a more typical solution would be to create a series of images and cycle through the images...like GIFs use to create animations. Here is a bit of starter code to demonstrate:
#pragma strict
var images : Texture[];
var fps = 10.0; // Frames per second
var cursorSizeX : int = 16; // Your cursor size x
var cursorSizeY : int = 16; // Your cursor size y
private var index = 0;
function Update () {
index = (Time.time * fps) % images.Length;
}
function OnGUI() {
GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX/3, Event.current.mousePosition.y-cursorSizeY/3.5, cursorSizeX, cursorSizeY), images[index]);
}
You need to set the size of 'images' in the inspector and then drag and drop your frames into the slots.
Your answer

Follow this Question
Related Questions
change mouse cursor on mouseover 3 Answers
HIdden cursor slider issue 1 Answer
Offseting position of guiText in relation to mouse 0 Answers
smoothing again 4 Answers