Applying a palette to a Unity scene
This part of the tutorial is Unity-specific, and is mostly not applicable to other platforms.
Applying color to an object
When you have a palette of colors, you'll want to apply the colors to parts of your scene. Applying a color to a mesh using SetColor can be done fairly straightforward:
rend = someObject.GetComponent<Renderer>();
rend.material.SetColor("_Color", Color.red);
Instead of setting the color to Color.red, you can use one of the colors from the palette you generated. This could be colorPalette[i] in a for-loop, or when using a different color space (for example with the ColorHSL struct, provided in the Unitypackage) it is important to get the rgb values to input into the material.
Using propertyblocks
Instead of getting the renderer and setting the color of the material, you could also use a propertyblock. This requires a slight change to your shader:
and a slight change to the code as well:
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
rend = someObject.GetComponent<Renderer>();
rend.GetPropertyBlock(propertyBlock);
propertyBlock.SetColor("_Color", Color.red);
rend.SetPropertyBlock(propertyBlock);
When applying color to a lot of objects at once, this will stop material instancing and lower calculation time. This tutorial gives a more in-depth explaination of this.
Determining which object gets which color
Determining which color is applied to which object can be important for having a coherent result.
There's two main things to consider here: how you want the result to look, and how you want to structure your Unity scene.
A method I like to use is sort all gameObjects that will share color into the same parent object, and reference the parent objects to the script that applies the color.
}
This requires little code and allows for intuitively moving gameobjects between the colors, but it also has a couple of drawbacks. For example, the scene becomes cluttered and parts that would usually be together, may have to be put in completely different parents to get the right colors. The objects are also somewhat tied to the specific spot in the palette that they were grouped into, as moving it to another parent makes the scene get even more incomprehensible quickly.
Another way of determining the color per object, is to have a script attached to all (groups of) objects, that manage which color of the palette is applied to the object(s).
This allows for more flexibility and a more orderly scene, but requires scripts attached to way more gameObjects.
The manager script can have a much more simplified code:
And the object (group) that it calls upon handles the rest:
}
Changing other colors
Some things other than gameobjects can have their color changed by script, including (but not limited to) fog and skyboxes.
These are very useful for generating color palettes for environments, as they have big impact on the mood of a resulting image.
Changing fog through code requires only one line:
And the skybox is relatively simple as well, as you can change the properties of its material like you can with gameObjects, as long as you reference it in the inspector:
To get the most out of this, you might need a custom skybox. There is one in the Unitypackage that has two color inputs and shows a gradient between the two, but anything else can work too.