How can I make an objects hue change by using a slider?
First you need a material. If you don’t have one from another application, you can make one in the editor:
From the menu bar: Assets->Create->Material
This creates a material in your project view. Drag the material onto a model in your scene (if you need something to test with, you can create a box via the menu: GameObject->Create Other->Cube)
Once you’ve dragged your material onto the object, you should be able to click on the white box in the inspector window labeled ‘Main Color’. You should have all the color slider controls you desire =).
If you were talking about making a slider and adjusting hue in code (if you were making your own editor, for example), you would change a game object’s material’s color via:
myGameObject.renderer.material.color = Color.red; //or
myGameObject.renderer.material.color.r = 1.0f;
myGameObject.renderer.material.color.g = 0.0f;
myGameObject.renderer.material.color.b = 0.0f;
For the slider bar I would probably start with something like a horizontal slider bar:
If you want to rotate the hue component of a single colour then there is some code for this on the Unify wiki. It uses the HSB model, which is OK as long as you don’t want to change the saturation and brightness - the brightness in particular is badly behaved with this colour model.
If you want to rotate the hues of all pixels in an image (like Photoshop’s hue/saturation adjustment) then you will need to access the Texture2D class to alter the object’s texture. I think I have some code somewhere that I could let you have, but it would need tidying up a bit before posting. Let me know if you need something like this.
Here’s another way to do it:
-
Make a bitmap color swatch with bands of the range of hues you want to apply to your object. They can be anything (see the attachment).
-
Make a custom GUI style and assign that color swatch as the background texture to show.
-
Display a slider with that custom style as the slider background with code something like this in your OnGUI function:
var newTintColor : Color;
var newTint : int;
newTint = HorizontalSlider(selectedTint, 1.0, 99.0, "tintramp_horizontalslider", "ramp_horizontalsliderthumb");
newTintColor = guiSkin.customStyles[8].normal.background.GetPixel(selectedTint, 1);
- When you’re ready, set the base color of the material of object you want to tint to the newTintColor. (Code not shown).
The benefit of this is #1 – the colors are arbitrarily set by the bitmap you use as the background texture of the slider, so you can choose any color scheme visually rather than having to come up with numbers to represent the colors you want to apply.
Thanks all!
I’m having problems getting this to work. I have some clouds that I want to display in the background of my game. The clouds are just planes scaled to the aspect ratio of a .png I made (using a particles/alpha blended material), and when I set them up I call this:
var col=Color(0.5,0.5,0.5,cloudMinAlpha+cloudAlphaVariance*s);
renderer.material.color=col;
This runs fine but the alpha channel isn’t varying with the s variable. In the inspector I can see that the value doesn’t change… but when I change the tint value manually it works fine. Ideas?
Offhand I’d guess that s is an int when it should be a float, but without seeing the rest of the code, it’s not really possible to say.
–Eric
I’ve also tried setting it to (1.0,0,0,1.0) to see if there’s any change… This should tint it red, but there’s still no change.