/
/
/
public Texture2D colorPicker;
public int ImageWidth = 600;
public int ImageHeight = 388;
public Color initialColor;
public Color col;
public bool wasClicked = false;
void Start()
{
initialColor = renderer.material.color;
}
void OnMouseDown()
{
wasClicked = true;
}
void OnGUI()
{
if (wasClicked)
{
if (GUI.Button(new Rect(20, 50, 600, 388), colorPicker))
{
if (GUI.RepeatButton(new Rect(10, 10, ImageWidth, ImageHeight), colorPicker))
{
Vector2 pickpos = Event.current.mousePosition;
int aaa = Convert.ToInt32(pickpos.x);
int bbb = Convert.ToInt32(pickpos.y);
col = colorPicker.GetPixel(aaa,41-bbb);
Debug.Log(col);
}
wasClicked = false;
}
if(Input.GetKeyDown(KeyCode.Alpha1))
{
renderer.material.color = col;
}
if(Input.GetButtonDown("Fire2"))
{
renderer.material.color = initialColor;
}
}
}
}
/
/
/
Any Errors?
Neg.
Don’t test for Input in OnGUI.
What is it you are trying to accomplish with this?
Not sure what you mean…
changing the color of an object…lord.
What is it you want the code to do actually? I have a general idea from what I read in above example, but what end result do you want on the screen?
In the future when posting a question about code that doesn’t include errors, explain what you want the code to do and what it is actually doing.
I want to select the object then use the colorpicker to select a color then apply the color.
Do you want to do this from within the game or from just the editor?
from within the game.
It worked before I made it focused on what object was selected, but it seems if the colorpicker is behind the colorpicker gui. like it isn’t registering the repeatbutton…
I think this is what you were going for, correct? Let me know if you have any problems with it.
using UnityEngine;
using System.Collections;
using System;
public class ColorPicker : MonoBehaviour {
public Texture2D colorPicker;
public int ImageWidth = 600;
public int ImageHeight = 388;
public Color initialColor;
public Color pickedColor;
public bool wasClicked = false;
void Start()
{
initialColor = renderer.material.color;
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
renderer.material.color = pickedColor;
}
if (Input.GetButtonDown("Fire2"))
{
renderer.material.color = initialColor;
}
}
void OnMouseDown()
{
wasClicked = true;
}
void OnGUI()
{
if (wasClicked)
{
Rect buttonRect = new Rect(20, 50, ImageWidth, ImageHeight);
if (GUI.Button(buttonRect, colorPicker))
{
Vector2 pickpos = Event.current.mousePosition - new Vector2(buttonRect.xMin, buttonRect.yMin);
Int32 x = Convert.ToInt32(pickpos.x * ((float)colorPicker.width / (float)ImageWidth));
Int32 y = Convert.ToInt32(pickpos.y * ((float)colorPicker.height / (float)ImageHeight));
pickedColor = colorPicker.GetPixel(x, y);
wasClicked = false;
}
}
}
}
Yep, thats it.
Thanks for the help.
No problem! Good luck with it!
LordAelfric is correct, Never put input commands… Other then the get events( like you did correctly) in the OnGUI methods. Because OnGUI() will always do the press atleast twice…
So these imput’s should always be thrown in Update one of them.
if(Input.GetKeyDown(KeyCode.Alpha1))
if(Input.GetButtonDown(“Fire2”))
I’ll remember that, thanks a lot guys…