- Home /
Draw crosshair INSTEAD of cursor.
Hi, I have a Unity NOOB question. Basically I have a crosshair png I made in GIMP, I just want to hid the cursor and draw this crosshair at the mouse position in 2D space. I know this is something super easy, I am just very new to Unity's GUI class. Any references will be greatly appreciated, because I'm not just asking for a script, but more of something to study.
This is for the non hardware accelerated crosshair.
It will simply place the UI texture on the center off the mouse pointer. note that the clicking happens on the left corner of the pointer, so you have to tweak the adjust paramters to center your texture on the left corner of the pointer. With an extra script you can switch out the texture with different ones (not included in this script). that way you can created item icon's and so on.
using UnityEngine;
using System.Collections;
public class crosshair : $$anonymous$$onoBehaviour {
static public bool center =true;
static public bool cursorOff =false;
public float cursorAdjustY;
public float cursorAdjustX;
public Texture crosshairTexture;
public int W;
public int H;
public bool on=true;
void OnGUI(){
if(on==true){
GUI.DrawTexture(new Rect(Screen.width/2+cursorAdjustX, Screen.height/2+cursorAdjustY, W, H), crosshairTexture, Scale$$anonymous$$ode.ScaleToFit);
}
}
// Update is called once per frame, this will hide your //original mouse, but still keep its functions. On low fps
//this will cause mouse flicker.
void Update () {
Screen.lockCursor = cursorOff;
Screen.lockCursor = center;
}
}
Answer by Eric5h5 · Oct 06, 2013 at 09:31 PM
Use a hardware cursor: http://docs.unity3d.com/Documentation/ScriptReference/Cursor.html