2

I have a method that takes any object as an argument and it should go through the properties, whatevery they are. I'm trying to get properties for a custom shader (created with ShaderForge). This is the method:

public void save(object objectToSave) {

    var newProperties = new List<PropertyInfo>(objectToSave.GetType().GetProperties());

    foreach (PropertyInfo property in newProperties)
    {
        object value = property.GetValue(objectToSave, null);
    }
}

When I pass an object of type Material (I'm passing the RenderSettings.skybox in my tests) with a custom shader as an argument, I get this error:

Material doesn't have a color property '_Color'

Maybe this is because objectToSave.GetType() returns the type Material instead of the custom parameters implemented by ShaderForge shader (which doesn't implement _Color). How can I tell the method this is a Material with custom shader so it doesn't try to find the regular fields and properties? How to make it get a list of the actual properties and fields?

When I open the material in Unity, it has these parameters. How do I access them via GetProperties?

Material settings

1 Answer 1

1

UnityEditor class has ShaderUtil, which contains the following methods:

  • GetPropertyCount - Get the number of properties in Shader s.

  • GetPropertyDescription - Get the description of the shader propery at index propertyIdx of Shader s.

  • GetPropertyName - Get the name of the shader propery at index propertyIdx of Shader s.
  • GetPropertyType - Get the ShaderProperyType of the shader propery at index propertyIdx of Shader s.
  • GetRangeLimits - Get Limits for a range property at index propertyIdx of Shader s.
  • GetTexDim - Gets texture dimension of a shader property.
  • IsShaderPropertyHidden - Is the shader propery at index propertyIdx of Shader s hidden?

To use it, you include "using UnityEditor" in your script and then access it by typing ShaderUtil.(methodname).

With these methods you can iterate through the custom shader properties. It's weird that you can't do this with GetProperties like you do on types, but this worked for me.

The type RANGE was a mystery to me, but what it does is it returns a float value of the actual value and ALSO the min and max values of the parameter so you know what its range is (because it's not a known type).

Please note that this is only for editor, so you have to use conditional #if XXXXX conditions around it, because UnityEditor class and this method won't work runtime.

Unity documentation on ShaderUtil: https://docs.unity3d.com/ScriptReference/ShaderUtil.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.