- Home /
Dealing with multiple crosshairs
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class Crosshair : MonoBehaviour
{
public Texture2D defaultCrosshair;
public Texture2D GrabCrosshair;
public Texture2D NotGrabbableCrosshair;
private Texture2D crosshair;
public LayerMask GrabMask;
public LayerMask NonGrabMask;
void Start ()
{
crosshair = defaultCrosshair;
}
void Update ()
{
}
void OnGUI()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
if(Physics.Raycast(ray, out hit, Mathf.Infinity, GrabMask))
{
crosshair = GrabCrosshair;
}
else if(Physics.Raycast(ray, out hit, Mathf.Infinity, NonGrabMask))
{
crosshair = NotGrabbableCrosshair;
}
else
{
crosshair = defaultCrosshair;
}
float w = crosshair.width;
float h = crosshair.height;
Vector2 pivot = new Vector2(Screen.width/2, Screen.height/2);
Rect position = new Rect(pivot.x - w/5.4f, pivot.y - h/5.2f, w/2, h/2);
GUI.DrawTexture(position, crosshair);
}
}
is this the best way to handle multiple crosshairs like a default crosshair, grabbable crosshair, and nongrabbable crosshair?
One issue with this code is that OnGUI() often gets called multiple times per frame. Put this inside your OnGUI():
Debug.Log(Time.frameCount + ", " + Event.current.type);
Every event with the same frame count happens within the same frame.
So I'd move lines 31 - 42 into Update() to avoid doing your double raycast many times per frame.
using UnityEngine;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode]
public class Crosshair : $$anonymous$$onoBehaviour
{
public Texture2D defaultCrosshair;
public Texture2D GrabCrosshair;
public Texture2D NotGrabbableCrosshair;
private Texture2D crosshair;
public Layer$$anonymous$$ask Grab$$anonymous$$ask;
public Layer$$anonymous$$ask NonGrab$$anonymous$$ask;
void Start ()
{
crosshair = defaultCrosshair;
}
void Update ()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
if(Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, Grab$$anonymous$$ask))
{
crosshair = GrabCrosshair;
}
else if(Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, NonGrab$$anonymous$$ask))
{
crosshair = NotGrabbableCrosshair;
}
else
{
crosshair = defaultCrosshair;
}
}
void OnGUI()
{
float w = crosshair.width;
float h = crosshair.height;
Vector2 pivot = new Vector2(Screen.width/2, Screen.height/2);
Rect position = new Rect(pivot.x - w/5.4f, pivot.y - h/5.2f, w/2, h/2);
GUI.DrawTexture(position, crosshair);
}
}
is this Ok now?
Looks fine. That's all I see in terms of issues. Others may some something else.
"The best way" often is a subjective topic since every person has their own definition of what best means. What kind of treats are you looking for when you ask for the best solution?
Best as efficient code?
Best as clean code?
Best as flexible code?
Best as testable code?
Best as robust code?
Best as simple code?
Best as all of the above?
... And did I miss your original intent of best?
I don't say you are wrong in any way and your question is perfectly fine. I just have this strange urge to re$$anonymous$$d people now and then that in order to try to give the best answer, it could help if it is stated in which way it should be best. Also some of these virtues may be contradicting each other. Flexible code and Efficient code doesn't always go hand in hand, yet both may have good traits you want to have but you may be forced to pick which one is most important to you. If you don't care about testable code, then you can write testable code right off the bat. And so on.
It looks like a good way to solve the problem, and @robertbu augmented your code to become more efficient by reducing redundant raycasts so it became a better way to solve your problem. But neither of us knows if it is the best way to solve your problem.
I am so nitpicky I annoy myself sometimes :(
Your answer
Follow this Question
Related Questions
Non rectangular GUI Mask / Matte? 2 Answers
Is there a way to change the order of the "Layers" Tab? 0 Answers
Drag through Mask 0 Answers
Detecting that I'm clicking a unit even though I'm not? 0 Answers