- Home /
GUI Crosshair
Hi, so I am trying to make a GUI crosshair which I will later change(based on the weapon's accuracy). Problem is, I don't really understand how the GUI system works, since I have only done one thing with it, and I need help. I have an empty game object, with this script
function OnGUI()
{
GUI.Box(Rect(0,0,15,15), "");
}
What else do I need to do to make it appear? Thanks!
You could make a GUI Crosshair in GI$$anonymous$$P. Just make a simple cross. Then you want GUITexture to be able to display the image onto your screen, then just adjust the image to your liking.
NOTE: $$anonymous$$ake sure you have the image transparent, SO you make the crosshair black and the rest of the canvas is white, make the white transparent or alpha value 1
Answer by Kirbyrawr · Apr 02, 2013 at 03:13 PM
#pragma strict
var chTexture : Texture2D;
var positionch : Rect;
static var ch = true;
function Update() // If we don't do this, we can't update the size.
{
positionch = Rect((Screen.width - chTexture.width) / 2, (Screen.height -
chTexture.height) /2, chTexture.width, chTexture.height);
}
//We need to draw the texture on the gui
function OnGUI()
{
if(ch == true)
{
GUI.DrawTexture(positionch, chTexture);
}
}
Attach it to a gameobject, and put a texture in the inspector.
Ok, so it seems to be working but I have one problem, how to I scale from the center of the crosshair and not the top right.
Answer by sweak322 · May 01, 2014 at 04:33 PM
var crosshairTexture : Texture2D; var position : Rect; static var OriginalOn = true;
function Start() { position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }
function OnGUI() { if(OriginalOn == true) { GUI.DrawTexture(position, crosshairTexture); } }
this script works wonderfully
Your answer
![](https://koobas.hobune.stream/wayback/20220613095529im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Scale GUI Texture From the Center. 1 Answer
[Solved]Label is not a member of GUI anymore. 1 Answer
The first object in selection grid can't be activated. Why? 1 Answer
OnGUI and Event.current 2 Answers