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

How to Get the UV Color of the Sprite

Discussion in 'Scripting' started by Alexander21, Nov 9, 2017.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi All

    I am new bie to unity. In my project i am using the color palette. I have a sprite renderer for colour paleete

    When the user touch the color. i have to identify the selected color. How can i get the selected color.

    I think i have to get the uv color. So could any one help me. How can i get the color palette.

    Thanks and Regards
    karthikeyan.k
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    You can use GetPixel:
    Code (CSharp):
    1. sprite.texture.GetPixel(x, y);
    You have to check Read/Write Enabled in the import settings of your sprite under the Advanced rollout. Make sure your Sprite is uncompressed, otherwise the colors would be different.
     
  3. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Thanks, McDev02 For Your Post. I have used your Code. It is well worked in Texture. I have Sprite (2D and UI). Where i have many Sprites in the Single Texure.

    MainImage= TextureType= Sprite(2d and Ui) Sprite Mode= Multiple
    MainImage1
    MainImage2
    MainImage3

    Image N are the Sprites.....


    I have try to access the Image 3 but when i use the above code. The Main Texture is accessing . That means the MainImage Color is get loaded . Not MainImage3 Texture color is not loaded.

    how can i get the Color of the Sprite of the MainImage3

    I have used the Below Code. When i loaded the Sprite . The Texture image is getting loaded. How can i avoid it.

    how can i load the sprite image....

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ColorPicker1 : MonoBehaviour {
    6.  
    7.     public  GameObject  target;
    8.  
    9.     /*
    10.     /!\ Don't forget to make the texture readable
    11.     (Select your texture : in Inspector
    12.         [Texture Import Setting] > Texture Type > Advanced > Read/Write enabled > True  then Apply).
    13.     */
    14.  
    15.     public  Sprite colorPicker;
    16.     public Color MyPixel;
    17. //    public Texture2D colorPicker;
    18.  
    19.     public Rect colorPanelRect = new Rect(0,0,200,200);
    20.  
    21.     void OnGUI() {
    22.         GUI.DrawTexture(colorPanelRect, colorPicker.texture);
    23.         if (GUI.RepeatButton(colorPanelRect, ""))
    24.         {
    25.             Vector2 pickpos  = Event.current.mousePosition;
    26.             float aaa  = pickpos.x - colorPanelRect.x;
    27.  
    28.             float bbb  =  pickpos.y - colorPanelRect.y;
    29.  
    30.             int aaa2  = (int)(aaa * (colorPicker.rect.width / (colorPanelRect.width + 0.0f)));
    31.  
    32.             int bbb2  =  (int)((colorPanelRect.height - bbb) * (colorPicker.texture.height / (colorPanelRect.height + 0.0f)));
    33.  
    34.             Color col  = colorPicker.texture.GetPixel(aaa2, bbb2);
    35.  
    36.             Debug.Log(aaa2 + "||||||" + bbb2);
    37.             target.GetComponent<Renderer>().material.color = col;
    38.         }
    39.     }
    40. }
    41.  
    How can i access......
     
  4. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    How can i get the UV Color of the Sprite. When i use this code

    Code (CSharp):
    1. prite.texture.GetPixel(x, y);
    It access the Parent Texture. I have an Png file where the multiple Sprites are there. I have to access the colour of the third child. But when i use the above code. the parent texture color is displaying. But i have to display the child sprite colour.

    Could any one help me. How can i access the Sprite UV Colour of the sprite...