but that just makes my texture black. I imagine I want to change the materials shader but is there a shader that just renders in greyscale? If so what's it called and where can I get it?
I don't know if a grayscale shader is included w/ Unity, but this should be pretty easy with a custom shader. Create a new shader, then double-click it to open it in the editor. It might look like this:
Depending on what you're doing, you might prefer to make the actual texture grayscale:
function Start () {
var texClone = Instantiate (renderer.material.mainTexture);
renderer.material.mainTexture = texClone;
MakeGrayscale (texClone);
}
function MakeGrayscale (tex : Texture2D) {
var texColors = tex.GetPixels();
for (i = 0; i < texColors.Length; i++) {
var grayValue = texColors*.grayscale;*
<em>texColors _= Color(grayValue, grayValue, grayValue, texColors*.a);*_</em>
<em>_*}*_</em>
<em>_*tex.SetPixels(texColors);*_</em>
<em>_*tex.Apply();*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<p>However that will probably use more memory, since the texture has to be marked readable, and be RGB24 or ARGB32. On the other hand it will work on anything and not need a pixel shader.</p>*_</em>
This will result in a little slider on the shader options which you can drag to modify it. However, the real prize from this is that it allows you to script the effect amount. To set it, you can simply call this on the game object you’re drawing:
And in the OnGUI code. We only draw during the Repaint event.
Note: Unity3D calls OnGUI for events that happen. It is typical that OnGUI gets called twice, first with
a Layout event which is determining all the GUI… calls and their screen position layout, and then
with a Repaint event where the GUI items actually get drawn. You also may get other events in OnGUI like mouse and keyboard events. (That is how GUI and GUILayout get the job done so transparently.)
// Only do the draw on the Repaint event.
if(Event.current.type.Equals(EventType.Repaint))
{
// The craftingItemMaterial is a MyShaders_GUI_Desaturated shader.
Graphics.DrawTexture(
itemRect,
item_icon_texture,
craftingItemMaterial );
}