- Home /
Replace shader at runtime
I have looked around in the docs on how to change all shaders in the scene during runtime and i wrote this and attached to my main camera:
using UnityEngine;
using System.Collections;
public class ReplaceShader : MonoBehaviour {
public Shader grayScale;
void Update()
{
Camera.main.SetReplacementShader(grayScale, "Grayscale");
}
}
When i try and run the game all that shows up is the default "blue background", the game is still running in the background but you can't see it through the camera, how could i fix this?
(Note I am beginner to coding and to unity)
-------------------------------------EDIT-------------------------------------------------
I found a solutions that kind of works, i changed this line:
Camera.main.SetReplacementShader(grayScale, "Grayscale");
to this:
Camera.main.SetReplacementShader ( grayScale, "RenderType" );
Now the camera shows the grayscale, but for some reason the shader is really dark? :/
I am not sure, but arent you supposed to assign shaders to the materials, not cameras?
I think you pretty much have to change the shader for each material.
You can access an objects material with gameObject.GetComponent[$$anonymous$$eshRenderer]().material.shader
and assign it there.
replace [] with the pointy parentheses, the comment section deletes them x.x
Hm i was hoping there would be a way to change all materials / shaders in the entire scene using the camera, but i will test this method first, thanks ! :)
I assume you've read through this http://docs.unity3d.com/$$anonymous$$anual/SL-ShaderReplacement.html ?
@sniper43 Ah thanks, In the shader i had to add this: Tags {"RenderType" = "Opaque" }
and now it works :D
Your answer
Follow this Question
Related Questions
How to combine a camera rendering an edge detection image effect with another camera 1 Answer
Using a camera replacement shader but keeping the underlying (original) colors? -4 Answers
Global Shader Question 1 Answer
Help with multi-purpose replacement shader for camera 1 Answer
Shader Replacement not working with a very simple shader. 0 Answers