Search Unity

Get pixel colour from image?

Discussion in 'Scripting' started by BubyMB, Jan 22, 2018.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys I have a bit of a doosey here.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.UI;
    7.  
    8. public class PieChartTriggers : MonoBehaviour
    9. {
    10.     Color[] Data;
    11.     public Image image;
    12.  
    13.     public int Width { get { return image.sprite.texture.width; } }
    14.     public int Height
    15.     {
    16.         get { return image.sprite.texture.height; }
    17.     }
    18.  
    19. void Awake()
    20.     {
    21.         image = GetComponent<Image>();
    22.         Data = image.sprite.texture.GetPixels();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (Input.GetMouseButton(0))
    28.         {
    29.  
    30.             Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    31.             screenPos = new Vector2(screenPos.x, screenPos.y);
    32.  
    33.             RaycastHit2D[] ray = Physics2D.RaycastAll(screenPos, Vector2.zero, 0.01f);
    34.             for (int i = 0; i < ray.Length; i++)
    35.             {
    36.                if (ray.collider.tag == "Circle")
    37.                 {
    38.                     screenPos -= ray.collider.gameObject.transform.position;
    39.                     int x = (int)(screenPos.x * Width);
    40.                     int y = (int)(screenPos.y * Height) + Height;
    41.  
    42.                    if (x > 0 && x < Width && y > 0 && y < Height)
    43.                     {
    44.                         Color color = Data[y * Width + x];
    45.                         Debug.Log("clicked " + color);
    46.                     }
    47.                     break;
    48.                 }
    49.             }
    50.         }
    51.     }
    52. }
    53.  
    54.  
    Found this script on forums problem is it was meant for sprite renderer and not Image.
    I have been trying to get the colour of the pixel from an image when i click, I have read up and found out that getpixel does not work on images, but works on rawImages but rawImages lacks the functions i need. Are any of you guys familiar with how to do this? I have been searching for a good two hours on how to go around this and haven't found anything.
    The script returns a null reference exception because apparently you can't use get pixel on images
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,232
    Do you have "Read/Write" enabled for the texture you're trying to access?
    Is the texture itself marked as "Sprite"?
    Which line of code does the error you're getting refers to?
     
    Last edited: Jan 22, 2018
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,993
    Does line 36 even compile? You're asking for the .collider property on an array of RaycastHit2D?

    Check your logs for errors... I don't think you're running the code you think you're running.
     
  4. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Yes, i have checked read and write and it is a sprite

    didn't think to read that the script stops at 22 anyways.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,993
    The whole thing (and every other script in your project) must compile successfully for any of it to run at all.
     
unityunity