- Home /
Shader can be set to refresh it?
I have a question about shader. i used the lab renderer in my project.
if i choose unit by mouse click, the gameobjects effect in game window will be changed immediately. but when i change it by code, the value is changed, But the effect did not change.
If I choose this object again, and the main points open shader detailed list, the effect will be changed, can anybody told me why? Why the use code to control effect will not change immediately?
if i change the texture by code , the effect will chane immediately!
thanks for help me
Answer by AlwaysSunny · Dec 06, 2016 at 01:38 PM
Thank you for including screenshots. Your situation is not 100% clear. Try to use your strongest English skills when posting.
It sounds like you might be discovering the difference between a material in your project's assets and an instance of that material. In most cases, materials get instantiated when you change them with code. Each object which changes its material with code gets its own instance of that material that lives in memory, and gets destroyed when exiting play mode.
http://answers.unity3d.com/questions/228744/material-versus-shared-material.html http://answers.unity3d.com/questions/684089/change-shader-property-at-runtime-1.html
To address ALL instances of a material, when you cannot use the sharedMaterial, you'll need a reference to every object using the original material. You can then address each instance of that material (on each object individually) to make your changes one by one.
foreach (GameObject go in myCollectionOfGameObjects) {
go.renderer.material.SetProperty("unlit", true);
}
Thank you for reply.This is the way I used to be.
I found that the unlit attribute is not the same as that of the other attributes.so change _Color can be. not the g_bUnlit. why?
Please never share screenshots of code. Always paste and format code with 101010 button ins$$anonymous$$d.
And please post comments as comments and not answers (converted).
Looks like it's an integer value, I didn't realize that. Have you tried this:
material.SetInt("g_bUnlit", 1);
In other words, not checking for the property, but just calling it without checking?
Answer by libufan · Dec 08, 2016 at 12:31 PM
when i called this,
material.SetInt("g_bUnlit", 1);
it will be checking, but the effect not changed. i must to choose the material, then effect will be changed immediately.