Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 greggman · May 14, 2016 at 11:00 AM · uiguihandlesondrawgizmos

How can I draw something in screen pixel coordinates in a OnDrawGizmos in MonoBehaviour?

I'd like to draw a rect in screen pixel coordinates in OnDrawGizmos in a MonoBehaviour

 public class Test : MonoBehaviour
 {
    void OnDrawGizmos()
    {
        Rect rect = new Rect(10, 10, 150, -100);
        UnityEditor.Handles.DrawSolidRectangleWithOutline(rect, Color.black, Color.white);
    }
 }

Those values in rect end up being world space coords oriented toward the screen relative to the Transform of the GameObject this script is attached to.

I'd like to just draw in pixels on the screen where 0, 0 is the top left of the screen. Do I need to get out the camera and compute those positions relative to the current GameObject's transform or is there an easier way?

Ideally I'd like to be able to use things like GUI.Label it's not working

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by greggman · May 14, 2016 at 06:12 PM

So this is NOT the solution I was looking for. The solution I wanted was some way to just tell Unity I want to draw in 2D and start using the GUI, GUILayout functions but when I tried to use them I got errors. Maybe someone has an example?

In lieu of that here's some code to compute the correct world coordinates for the desired screen coordinates

 Vector3 ScreenToWorld(float x, float y) {
     Camera camera = Camera.current;
     Vector3 s = camera.WorldToScreenPoint(transform.position);
     return camera.ScreenToWorldPoint(new Vector3(x, camera.pixelHeight - y, s.z));
 }

 Rect ScreenRect(int x, int y, int w, int h) {
     Vector3 tl = ScreenToWorld(x, y);
     Vector3 br = ScreenToWorld(x + w, y + h);
     return new Rect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
 }

So with that this seems to work

 public class Test : MonoBehaviour
 {
    void OnDrawGizmos()
    {
        Rect rect = ScreenRect(10, 10, 150, 100);
        UnityEditor.Handles.DrawSolidRectangleWithOutline(rect, Color.black, Color.white);
    }

 ...
Comment
Add comment · Share
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
0

Answer by AuriMoogle · Feb 25 at 08:53 PM

Unity's gizmos can't draw in screen space but you can draw on the camera.NearClipPlane. This will give the impression of drawing on the screen. You just have to take the camera projection type and canvas scale into account. Further explanation and more content can be found at my blog: http://anja-haumann.de/unity-screen-space-gizmos/

 using UnityEngine;
 
 public static class ScreenGizmos
 {
     private const float offset = 0.001f;
 
     /// <summary>
     /// Draws a line in screen space between the 
     /// <paramref name="startPixelPos"/> and the 
     /// <paramref name="endPixelPos"/>. 
     /// </summary>
     public static void DrawLine(
         Canvas canvas, 
         Camera camera, 
         Vector3 startPixelPos, 
         Vector3 endPixelPos)
     {
         if (camera == null || canvas == null)
             return;
 
         Vector3 startWorld = PixelToCameraClipPlane(
             camera, 
             canvas, 
             startPixelPos);
 
         Vector3 endWorld = PixelToCameraClipPlane(
             camera, 
             canvas, 
             endPixelPos);
 
         Gizmos.DrawLine(startWorld, endWorld);
     }
 
     /// <summary>
     /// Converts the <paramref name="screenPos"/> to world space 
     /// near the <paramref name="camera"/> near clip plane. The 
     /// z component of the <paramref name="screenPos"/> 
     /// will be overriden.
     /// </summary>
     private static Vector3 PixelToCameraClipPlane(
         Camera camera, 
         Canvas canvas,
         Vector3 screenPos)
     {
         // The canvas scale factor affects the
         // screen position of all UI elements.
         screenPos *= canvas.scaleFactor;
 
         // The z-position defines the distance to the camera
         // when using Camera.ScreenToWorldPoint.
         screenPos.z = camera.nearClipPlane + offset;
         return camera.ScreenToWorldPoint(screenPos);
     }
 }
Comment
Add comment · Share
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

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

78 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 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 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 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 avatar image avatar image

Related Questions

UI elements change position 0 Answers

Can I do a "rotatory" UI like this? 1 Answer

Place UI element on pointer up position. 1 Answer

Setting GUI color alpha math issue. 2 Answers

How do I switch screen view to another view/window? 1 Answer


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