-1

I did not find a direct function in Color section or since there is no direct C# Unity function for picking the color from post render. How is the best approach for picking the color in the mouse position?
I have done research and looks like there is posible to make a screenshot and then look into the texture calculating the mouse position.

Input.GetMouseButtonDown(0)

Application.CaptureScreenshot("Screenshot.png");

// get the color pixel in the same coordinates of the mouse position
Vector3 mouseCoordinates = Input.mousePosition;
myFinalColor = tex.GetPixel((int)mouseCoordinates.x, (int)mouseCoordinates.y);

Or do I have to make a second camera and attach it to a mesh render?

Alan Mattano
  • 860
  • 3
  • 14
  • 22
  • You really need to start accepting answers. If you don't find them helpful , you can nicely comment and tell them their answer is not working instead of ignoring them. – Programmer Apr 28 '16 at 09:57
  • some of the research require before asking the question on stackoverflow. – Muhammad Faizan Khan Apr 28 '16 at 10:28
  • Yes Programme Thanks. Soon as posible. Give enough time to wake up and analizza the data and confirm that the code really works well for choosing a correct answers. Yes Mohammad Faizan Khan I have look into the documentation for a week. And i have look to other similar questions with negative points here in stackoverflow. I do not have idea how to approach the problem. Thanks for pointing that out. – Alan Mattano Apr 28 '16 at 14:26

2 Answers2

2

You just need to use GetPixel(x,y)

This is very simple.

  • Save Screenshot to Texture2D for example MyTexture
  • And add .GetPixel( x postion of moue , Y position of mouse )
  • Save it to your color for GetScreenShot ( make your View to Texture2D )

        Color TheColorPicked;
        if (Input.GetMouseButtonDown(0))
        {
            TheColorPicked = MyTexture.GetPixel(Input.mousePosition.x,
                                                   Input.mousePosition.y);    
        }
    
ehsan wwe
  • 71
  • 2
  • 9
1

Yes you can make your own colour picker. Here is the code thanks to Git-hub this page.

using UnityEngine;
using System.Collections;

// relies on: http://forum.unity3d.com/threads/12031-create-random-colors?p=84625&viewfull=1#post84625

public class ColorPicker : MonoBehaviour {

  public bool useDefinedPosition = false;
  public int positionLeft = 0;
  public int positionTop  = 0;

  // the solid texture which everything is compared against
    public Texture2D colorPicker;

    // the picker being displayed
    private Texture2D displayPicker;

    // the color that has been chosen
    public Color setColor;
    private Color lastSetColor;

    public bool useDefinedSize = false;
    public int textureWidth  = 360;
    public int textureHeight = 120;

    private float saturationSlider = 0.0F;
    private Texture2D saturationTexture;

    private Texture2D styleTexture;

    public bool showPicker = false;

    void Awake() {
      if (!useDefinedPosition) {
        positionLeft = (Screen.width / 2) - (textureWidth / 2);
        positionTop  = (Screen.height / 2) - (textureHeight / 2);
      }

      // if a default color picker texture hasn't been assigned, make one dynamically
      if (!colorPicker) {
        colorPicker = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false);
        ColorHSV hsvColor;
      for (int i = 0; i < textureWidth; i++) {
        for (int j = 0; j < textureHeight; j++) {
          hsvColor = new ColorHSV((float)i, (1.0f / j) * textureHeight, 1.0f);
          colorPicker.SetPixel(i, j, hsvColor.ToColor());
        }
      }
      }
      colorPicker.Apply();
      displayPicker = colorPicker;

      if (!useDefinedSize) {
        textureWidth = colorPicker.width;
        textureHeight = colorPicker.height;
      }

      float v = 0.0F;
      float diff = 1.0f / textureHeight;
      saturationTexture = new Texture2D(20, textureHeight);
      for (int i = 0; i < saturationTexture.width; i++) {
        for (int j = 0; j < saturationTexture.height; j++) {
        saturationTexture.SetPixel(i, j, new Color(v, v, v));
        v += diff;
      }
      v = 0.0F;
      }
      saturationTexture.Apply();

      // small color picker box texture
      styleTexture = new Texture2D(1, 1);
      styleTexture.SetPixel(0, 0, setColor);
    }

    void OnGUI(){
      if (!showPicker) return;

      GUI.Box(new Rect(positionLeft - 3, positionTop - 3, textureWidth + 60, textureHeight + 60), "");

        if (GUI.RepeatButton(new Rect(positionLeft, positionTop, textureWidth, textureHeight), displayPicker)) {
        int a = (int)Input.mousePosition.x;
        int b = Screen.height - (int)Input.mousePosition.y;

        setColor = displayPicker.GetPixel(a - positionLeft, -(b - positionTop));
        lastSetColor = setColor;
        }

        saturationSlider = GUI.VerticalSlider(new Rect(positionLeft + textureWidth + 3, positionTop, 10, textureHeight), saturationSlider, 1, -1);
    setColor = lastSetColor + new Color(saturationSlider, saturationSlider, saturationSlider);
        GUI.Box(new Rect(positionLeft + textureWidth + 20, positionTop, 20, textureHeight), saturationTexture);

        if (GUI.Button(new Rect(positionLeft + textureWidth - 60, positionTop + textureHeight + 10, 60, 25), "Apply")) {
          setColor = styleTexture.GetPixel(0, 0);

        // hide picker
        showPicker = false;
        }

        // color display
        GUIStyle style = new GUIStyle();
        styleTexture.SetPixel(0, 0, setColor);
        styleTexture.Apply();

        style.normal.background = styleTexture;
        GUI.Box(new Rect(positionLeft + textureWidth + 10, positionTop + textureHeight + 10, 30, 30), new GUIContent(""), style);
    }

}

You can also find this helpfull asset store packages 1 2.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • Thx Mohammad I try Package (1) before but has CameraBgColor deprecate script and so I jump it. And (2) has Unix line ending and don't know why crush in Mono. There are olso others but do not pick the color from screen. – Alan Mattano Apr 28 '16 at 17:28
  • For this incomplete solution a second class is needed: "ColorHSV" Wonderful Script that probably is this one from MatthewW and translation into C# from Karsnen_2: http://forum.unity3d.com/threads/create-random-colors.12031/#post-84625 – Alan Mattano Apr 28 '16 at 19:14
  • Update the answer for future use! thanx – Muhammad Faizan Khan Apr 29 '16 at 04:17