Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jvt619 · Dec 23, 2013 at 06:18 AM · guimasklayerlayermaskcrosshair

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Dec 23, 2013 at 06:29 AM 0
Share

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.

avatar image jvt619 · Dec 23, 2013 at 07:10 AM 0
Share
     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?

avatar image robertbu · Dec 23, 2013 at 07:14 AM 0
Share

Looks fine. That's all I see in terms of issues. Others may some something else.

avatar image Statement · Dec 23, 2013 at 10:22 AM 1
Share

"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 :(

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the proper way to blend between two Mecanim States in a Layer, when both animation clips have different Masks? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges