Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 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
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

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

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

Advice wanted on state transition code. 2 Answers

Service not available 0 Answers

CG compiled to GLSL: hyperbolic functions not found. 0 Answers

how to keep running background my app when press mobile home button? 0 Answers

Terrain not shown 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