I’m having issues with trying to implement a color picker. I’m using the following code attached to the object:
public Texture2D texture2D = null;
public Color LastPickedColor = new Color(1,1,1,1);
void Awake()
{
texture2D = GetComponent<UITexture>().mainTexture as Texture2D;
}
void OnClick()
{
Vector3 textureWheelPos = UICamera.currentCamera.WorldToScreenPoint(transform.position);
Vector2 pos = new Vector2(textureWheelPos.x, textureWheelPos.y);
pos = UICamera.lastTouchPosition - pos;
int x = (int)pos.x;
int y = (int)pos.y;
LastPickedColor = texture2D.GetPixel(x, y);
Debug.Log(pos);
Debug.Log("X: " + x + "; Y: " + y + "; Red: " + LastPickedColor.r * 255 + ", Green: " + LastPickedColor.g * 255 + ", Blue: " + LastPickedColor.b * 255 + ", Alpha: " + LastPickedColor.a * 255);
}
I’m getting sporadic results in the colors i’m getting and am completely stumped as to what is going on. Is anyone able to shed some light on this?
Sorry to re-open that…
I’m trying to create a color picker for NGUI and i found on those forums some useful solutions for the basic GUI of Unity. I’m currently trying to implement it for NGUI but i’m facing some troubles : when i click to pick a color (we’ll name it color1), i receive another color : color2.
Moreover, in the image i use (added to this message) color2 is placed a little bit up and right compare to color1.
I’m guessing it’s due to some coordinates problem but i can’t find out why…
Here’s the code i’m using to change the color of some cars :
using UnityEngine;
using System;
using System.Collections;
public class ColorPickNGUI : MonoBehaviour {
public int paramX=0;
public int paramY=0;
public UITexture uiTexture;
private Texture2D colorPicker;
private int imageWidth;
private int imageHeight;
private Material matCar;
private MeshRenderer meshCar;
private Car car;
public void Start(){
imageWidth=uiTexture.width;
imageHeight=uiTexture.height;
colorPicker=(Texture2D)uiTexture.mainTexture;
}
public void setCar(Car newCar){
car=newCar;
}
public Car getCar(){
return car;
}
public void OnClick(){
Vector3 pickpos=Input.mousePosition;
int aaa = Convert.ToInt32(pickpos.x);
int bbb = Convert.ToInt32(pickpos.y);
Color col= colorPicker.GetPixel(aaa+paramX,bbb+paramY);
// "col" is the color value that Unity is returning.
// Here you would do something with this color value, like
// set a model's material tint value to this color to have it change
// colors, etc, etc.
//
// Right now we are just printing the RGBA color values to the Console
meshCar=car.getMeshRenderer();
matCar=meshCar.materials[1];
matCar.SetColor("_BaseColor",col);
matCar.name="newLambert";
car.getMeshRenderer().materials[1]=matCar;
}
}
Does anyone have an idea for that ?
Thanks a lot !
