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
0
Question by UserNobody · Oct 03, 2019 at 01:31 PM · editorwindowmousepositioncoordinatespixel

How to get pixel coordinates from mouse position?

Hello everyone!
I am making an editor extension that will allow me to paint textures within editor, but I have trouble getting a pixel coordinates from a mouse click position.

I know there's some math involved, but I'm terrible at math unfortunately.. So not sure how to achieve this :/ Is there some Unity's helper method that I could use maybe? Or if there are none, what calculation is needed to get the pixel coordinates?

I would really appreciate any help! Thank you!

This is my code:

 using System;
 using UnityEditor;
 using UnityEngine;
 
 public class Painting : EditorWindow
 {
     int size = 5;
     float zoomScale = 1.0f;
 
     Rect imgRect
     {
         get
         {
             return new Rect(25, 25, this.position.width - 50, this.position.height - 50);
         }
     }
     Texture2D texture = null;
 
     [MenuItem("Painter/Open")]
     public static void OpenWindow()
     {
         var window = EditorWindow.GetWindow<Painting>("Painter");
         window.Show();
     }
 
     private void OnEnable()
     {
         this.Setup();
     }
     private void Setup()
     {
         texture = new Texture2D(Screen.width, Screen.height);
         Color32[] pixels = new Color32[texture.width * texture.height];
         for (int i = 0; i < pixels.Length; i++)
         {
             pixels[i] = new Color32(255, 255, 255, 255);
         }
         texture.SetPixels32(pixels);
         texture.Apply();
     }
 
     private void OnGUI()
     {
         Matrix4x4 oldMatrix = GUI.matrix;
 
         BeginWindows();
         Matrix4x4 Translation = Matrix4x4.TRS(new Vector3(Screen.width / 2, Screen.height / 2, 0), Quaternion.identity, Vector3.one);
         Matrix4x4 Scale = Matrix4x4.Scale(new Vector3(zoomScale, zoomScale, 1.0f));
 
         GUI.matrix = Translation * Scale * Translation.inverse;
 
         GUIStyle imageStyle = new GUIStyle(); imageStyle.normal.background = texture;
         GUI.Label(imgRect, GUIContent.none, imageStyle);
 
         GUI.BeginGroup(imgRect);
 
         GUI.matrix = oldMatrix;
         var e = Event.current;
         if (e.type == EventType.MouseDown && e.button == 0)
         {
             this.Paint(GUIUtility.ScreenToGUIPoint(e.mousePosition));
         }
         else if (e.type == EventType.ScrollWheel)
         {
             var zoomDelta = 0.1f;
             zoomDelta = e.delta.y < 0 ? zoomDelta : -zoomDelta;
             zoomScale += zoomDelta;
             zoomScale = Mathf.Clamp(zoomScale, 0.25f, 1.25f);
             e.Use();
         }
 
         GUI.EndGroup();
     }
     private void Paint(Vector2 point)
     {
         Debug.Log(point);
         for (int x = Convert.ToInt32(point.x) - size; x < Convert.ToInt32(point.x) + size; x++)
         {
             for (int y = Convert.ToInt32(point.y) - size; y < Convert.ToInt32(point.y) + size; y++)
             {
                 texture.SetPixel(x, y, new Color32(0, 0, 0, 1));
             }
         }
         texture.Apply();
         this.Repaint();
     }
 }
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

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

114 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 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

How to get the correct contextual menu mouse position on graph view when I have ContentDragger & ContentZoomer manipulators? 4 Answers

How to get equal screen coordinates on different resolutions? 1 Answer

GLSL - Paint fragments in a certain view position 0 Answers

Get the color of a pixel at mouse position? 2 Answers

Drawing a box with mouse dragged on screen 2 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