• 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 mohaned1001 · Dec 11, 2017 at 11:17 PM · #pragma

how to get the pixel color at the mouse position

i am trying to get the pixel color under the mouse position the code i used is giving me the correct pixel color just fine but not the correct position as you could see in the picture below and the code

alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class get_pixel : MonoBehaviour
 {
 
     public RawImage pic;
     public Text tex_mouse = null;
     public Text tex2_color = null;
     void Start ()
     {
         
     }
     
     void Update ()
     {
         Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         int pos1 = (int)mousePosition.x;
         int pos2 = (int)mousePosition.y;
         Color32 color = (pic.texture as Texture2D).GetPixel(pos1, pos2);
         tex_mouse.text = "x = " + pos1 + " | " + "y = " + pos2;
         tex2_color.text = color.ToString();
     }
 }

as you can see the mouse is already in the black but it still give me the pixel color as blue so i want it to give me the pixel color at the mouse position

untitled.png (145.4 kB)
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 TerribleFlea · Mar 09 at 09:05 AM

I needed a script that reads a pixel regardless of the resolution of the image. Here, use it!

 I needed a script that reads a pixel regardless of the resolution of the image. Here, use it!
 Vector3[] corners = new Vector3[4];
 Image image = GetComponent<Image>();
 image.rectTransform.GetWorldCorners(corners);
 Rect newRect = new Rect(corners[0], corners[2]-corners[0]);
 if (newRect.Contains(Input.mousePosition))
 {
 Vector2 size = new Vector2( 822, 512);
 Vector2 pixelCoord = Input.mousePosition-corners[0];
 pixelCoord /= image.rectTransform.rect.size;
 pixelCoord *= size;
 Color colorPixel=image.sprite.texture.GetPixel((int)pixelCoord.x, (int)pixelCoord.y);
 }
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 DarthHawk13 · Jul 19, 2019 at 03:03 AM

@mohaned1001

What your code is actually telling Unity to do is to match the x position of the mouse with that many pixels over to the right on the image and the y position of the mouse with that many pixels up on the image. You are not returning where the mouse is over the image.

For your code to work the way it is your image must be at the bottom left corner of the screen. Like the images I have included below. The texture in the screenshots below is 256x256. I've used your code exactly.

alt text!alt text



GetPixels starting position (0,0) is the bottom left of the image. For the mouse position to line up on the image with your code your image must be at the bottom left of the screen.

Your thinking in trying to solve the problem is correct. However, Unity is doing the math differently. Mouse position 0,0 on the screen, with games made with Unity, is actually the very center of the screen.

I think this link elsewhere in the Unity forums may be useful.: https://forum.unity.com/threads/get-color-of-texture-under-mouse-position.103500/


unity-7-18-2019-7-28-58-pm-812.png (111.7 kB)
unity-7-18-2019-7-29-29-pm-416.png (111.4 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

How i destroy the object of that part that collides with another object? 2 Answers

How to make keywords per material works properly (not in Editor) 0 Answers

スクリプトが追加できません。,スクリプトクラスが見つからない (I can't add a script. , Script class not found) 0 Answers

How take a full res. Photo using webcam 0 Answers

Is it possible to have a changing line of script based off of variables? 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