Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question on GetPixel

Discussion in 'Scripting' started by Foestar, Apr 7, 2020.

  1. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Hey all, so I'm trying to get the color of a color circle based off where I click on said circle. This seemed like an easy concept, so I simply found a color circle on google and started from there.

    I first imported said circle and made sure it had read/write enabled. I then made a quick script getting the location of the mouse and set up a custom color field so I could see both in the inspector and in the UI the color I had selected. I did this through a function that was called using an event trigger for pointer click. This way when I click on the said object we do the onClickPickSkinColor function listed below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using UnityEngine.UI;
    4.  
    5. public class SkinColorChange : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Color goblinSkinColor;
    9.     [SerializeField]
    10.     private Image goblinSkinColorBox;
    11.  
    12.     public void onClickPickSkinColor()
    13.     {
    14.         var mouseX = Input.mousePosition.x;
    15.         var mouseY = Input.mousePosition.y;
    16.  
    17.         goblinSkinColor = this.GetComponent<Image>().sprite.texture.GetPixel((int)mouseX, (int)mouseY);
    18.         goblinSkinColorBox.color = goblinSkinColor;
    19.     }
    20. }
    21.  
    I thought this would work, but I've noticed I don't get the color i'm clicking on. After a few minutes studying what was going on it made me realize it may be because of the coordinates I am using. I think the GetPixel is transfering the mousePosition.x into its own local positioning making for weird effects.

    The thing is though is I can't seem to think of how to make this happen on the general scale rather than the local scale. I've been trying different things and attempting to wrap my head around it but this seems to just keep slipping from me.
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Do a raycast instead from the camera to your mouse cursor. This will give you a raycasthit object. One of the properties of the raycasthit is textureCoord1, use that x and y as the coords to GetPixel.

    you could also take your x,y position and do a SetPixel(x,y, Color.red) then Apply. This will give you feedback by changing the actual pixel you clicked on, allowing you to debug much easier. Comment out the code once you have it working. For things like this where I’m converting between different types of coordinates, I like to give myself lots of visual debugging info while I work on it. Don’t delete it, just comment it out or put it inside and if statement so that you can turn your debugging stuff back on if you notice a bug.
     
    Kurt-Dekker likes this.
  3. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Well, I tried quite a bit and have not been able to get my color wheel idea working. I did try implementing a debug draw ray and it shows it properly going to the UI element from the Camera. The issue though is raycasts go through UI elements. They don't seem to be picked up despite having a raycast target option in the inspector. This means when I cast the ray, which at this point I believe is working fine, it gets the objects through the UI element. In this case it's getting the ground object behind the UI.

    So after reading some more on this I've come to learn that apparently raycasts don't hit UI elements which means it's working as intended, but doesn't help my situation at all. Apparently there are Graphic Ray Casts that are specifically for UI elements. But after reading about that and trying it myself I still have had not success, perhaps because of how I'm mixing my raycast from the camera to a UI element.

    Whatever the case, I have been unsuccessful up to this point. I did try using the textureCoord1 that you mentioned, but still had similar results. At this point I have wiped my code and am gonna try from scratch again. Hopefully I get it working, but we'll see. This one seems to be a tough one for me.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  5. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
  6. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    EDIT: Figured it out. I was so close. Equation was off with object rect width/height
     
    Last edited: Apr 9, 2020