Find and change property name in a shader
I want to change and set 3 things after setting the new material:
First The shader type to Unlit/Color
Second The albedo color to change it for example to: 255,0,0,255
Third The metallic value from 0 to 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
public bool locked;
public Color lockedColor = Color.red;
public Color unlockedColor = Color.green;
public Renderer rend;
private GameObject doorPlanes;
private void Start()
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
for (int i = 0; i < doorPlanes.Length; i++)
rend = doorPlanes[i].GetComponent<Renderer>();
if (locked)
rend.material.SetFloat("Metallic", 1);
rend.material.color = lockedColor;
else
rend.material.color = unlockedColor;
// Update is called once per frame
void Update ()
This line does nothing:
rend.material.SetFloat("Metallic", 1);
This is what I want to change:
c# unity3d
add a comment |
I want to change and set 3 things after setting the new material:
First The shader type to Unlit/Color
Second The albedo color to change it for example to: 255,0,0,255
Third The metallic value from 0 to 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
public bool locked;
public Color lockedColor = Color.red;
public Color unlockedColor = Color.green;
public Renderer rend;
private GameObject doorPlanes;
private void Start()
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
for (int i = 0; i < doorPlanes.Length; i++)
rend = doorPlanes[i].GetComponent<Renderer>();
if (locked)
rend.material.SetFloat("Metallic", 1);
rend.material.color = lockedColor;
else
rend.material.color = unlockedColor;
// Update is called once per frame
void Update ()
This line does nothing:
rend.material.SetFloat("Metallic", 1);
This is what I want to change:
c# unity3d
1
Have you tried"_Metallic"
?
– Ryolu
Oct 22 '18 at 3:47
add a comment |
I want to change and set 3 things after setting the new material:
First The shader type to Unlit/Color
Second The albedo color to change it for example to: 255,0,0,255
Third The metallic value from 0 to 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
public bool locked;
public Color lockedColor = Color.red;
public Color unlockedColor = Color.green;
public Renderer rend;
private GameObject doorPlanes;
private void Start()
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
for (int i = 0; i < doorPlanes.Length; i++)
rend = doorPlanes[i].GetComponent<Renderer>();
if (locked)
rend.material.SetFloat("Metallic", 1);
rend.material.color = lockedColor;
else
rend.material.color = unlockedColor;
// Update is called once per frame
void Update ()
This line does nothing:
rend.material.SetFloat("Metallic", 1);
This is what I want to change:
c# unity3d
I want to change and set 3 things after setting the new material:
First The shader type to Unlit/Color
Second The albedo color to change it for example to: 255,0,0,255
Third The metallic value from 0 to 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
public bool locked;
public Color lockedColor = Color.red;
public Color unlockedColor = Color.green;
public Renderer rend;
private GameObject doorPlanes;
private void Start()
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
for (int i = 0; i < doorPlanes.Length; i++)
rend = doorPlanes[i].GetComponent<Renderer>();
if (locked)
rend.material.SetFloat("Metallic", 1);
rend.material.color = lockedColor;
else
rend.material.color = unlockedColor;
// Update is called once per frame
void Update ()
This line does nothing:
rend.material.SetFloat("Metallic", 1);
This is what I want to change:
c# unity3d
c# unity3d
edited Nov 12 '18 at 17:25
Programmer
77.7k1089158
77.7k1089158
asked Oct 22 '18 at 3:36
Benzi AvrumiBenzi Avrumi
39110
39110
1
Have you tried"_Metallic"
?
– Ryolu
Oct 22 '18 at 3:47
add a comment |
1
Have you tried"_Metallic"
?
– Ryolu
Oct 22 '18 at 3:47
1
1
Have you tried
"_Metallic"
?– Ryolu
Oct 22 '18 at 3:47
Have you tried
"_Metallic"
?– Ryolu
Oct 22 '18 at 3:47
add a comment |
1 Answer
1
active
oldest
votes
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still useColor
with the 0 to 255 range but you must divide the values by 255f.rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52922082%2ffind-and-change-property-name-in-a-shader%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still useColor
with the 0 to 255 range but you must divide the values by 255f.rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
|
show 5 more comments
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still useColor
with the 0 to 255 range but you must divide the values by 255f.rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
|
show 5 more comments
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.
When you need to change a shader property but you don't know what to use for the proper name to use, select the material, click on its settings icon then click on "Select Shader". From there, you will see all the shader property names and their types and you will know which function and name to use to change the shader's properties.
With the default standard shader it looks something like this:
You need to know this otherwise you would need to ask new question for each property you want to change.
Your Renderer reference:
public Renderer rend;
To set it, the SetXXX
function is used:
rend.material.SetFloat("_Metallic", 1); //Metallic is a float
To get it the GetXXX
function is used:
float metallic = rend.material.GetFloat("_Metallic"); //Metallic is a float
To change or get the The albedo color to 255,0,0,255.
rend.material.SetColor("_Color", new Color32(255, 0, 0, 255));
Color color = rend.material.GetColor("_Color");
Notice that I used Color32
instead of Color
because Color32
takes values between 0
and 255
while Color
expects values between 0
and 1
.
To change the material's shader to "Unlit/Color", just find it then assign it to the material:
Shader shader = Shader.Find("Unlit/Color");
rend.material.shader = shader;
Note that the "Unlit/Color" shader doesn't have the _Metallic
property. It has one one property and that is "_Color". You can use the first method I described to determine which properties it has before attempting to change them.
What if the shader is used on many different objects. I want all the
objects to change when the property is changed on one of them.
To change all the objects using the-same material, use Renderer.sharedMaterial
instead of Renderer.material
. The shared material changes the original material and every other objects should pick that new material up as long as you have not called Renderer.material
on the material which actually makes a new copy for the said material and disconnects the renderer material from the original material.
edited Nov 30 '18 at 7:20
answered Oct 22 '18 at 7:42
ProgrammerProgrammer
77.7k1089158
77.7k1089158
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still useColor
with the 0 to 255 range but you must divide the values by 255f.rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
|
show 5 more comments
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still useColor
with the 0 to 255 range but you must divide the values by 255f.rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
For the Color I did this before your answer I used only Color like this: rend.material.color = new Color(255,0,0,255); And it's working fine. Just wondering since I didn't use Color32
– Benzi Avrumi
Oct 22 '18 at 8:22
1
1
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still use
Color
with the 0 to 255 range but you must divide the values by 255f. rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Nope. That's an undefined behavior. It may work and may not depending on the values and that's because of the reason I mentioned in my answer. There are tons of questions about this particular issue on this site. Just be aware of this. You can still use
Color
with the 0 to 255 range but you must divide the values by 255f. rend.material.color = new Color(255/255f,0/255f,0/255f,255/255f)
– Programmer
Oct 22 '18 at 8:29
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Thanks for the answer and comments.
– Benzi Avrumi
Oct 22 '18 at 8:45
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
Does this change all the materials using this shader, or just the one?
– Confused
Nov 30 '18 at 6:46
1
1
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
I've edited the answer to add that information for you
– Programmer
Nov 30 '18 at 7:21
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52922082%2ffind-and-change-property-name-in-a-shader%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you tried
"_Metallic"
?– Ryolu
Oct 22 '18 at 3:47