- Home /
Get color of pixel at mouse click of a GUITexture
I was wondering how you would get the color of the pixel that the mouse click on a GUITexture
Does a GUITexture have a collider? I thought that Raycasts only work then there are colliders?
Answer by robertbu · May 28, 2013 at 02:08 PM
Here is a bit of code. Your texture must be marked as Read/Write enabled in the texture importer. Also The width and height in the Pixel Inset settings of the GUITexture must match the real width and height of the image:
#pragma strict
private var tex : Texture2D;
private var v3Corner : Vector3;
function Start () {
var gt = GetComponent(GUITexture);
tex = gt.texture;
var v3Pos = Camera.main.ViewportToScreenPoint(transform.position);
v3Corner = Vector3(v3Pos.x+gt.pixelInset.x, v3Pos.y+gt.pixelInset.y, 0.0);
}
function OnMouseDown() {
var v3Pixel = Input.mousePosition - v3Corner;
Debug.Log(tex.GetPixel(v3Pixel.x, v3Pixel.y));
}
hello , thanks for the answer but how can i make it r/w ?
Select the texture in the inspector. Change its type to 'Advanced', then turn on the Read/Write Enabled flag.
hello is was a wonderful solution but i want this code on c# and i cant covert this ligne to c#
var gt = GetComponent(GUITexture); help !
I wrote this code nearly a year ago and would do it differently today. Replace lines 6 and 7 with:
tex = guiTexture.texture;
Just for information purposes, that line would be written in C# as:
GUITexture gt = GetComponent<GUITexture>();
Answer by PORUSH MANJHI · May 27, 2013 at 04:36 AM
you can download colorpicker from assetstore.
there is another question with an answer to that here on forums i jsut posted there just now. search my name and "screen.width" in google. its a magic treasure hunt, the net :D
Your answer
Follow this Question
Related Questions
Change colour of guiTexture from green to red. 1 Answer
How can I change all of a textures pixels to one color? 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Transferring Color from script on another GameObject to a GUI.Box 1 Answer
How to change a guiTexture color when a float is a certain number? 1 Answer