Search Unity

How to query property flags from C# for each shader property

Discussion in 'Shaders' started by Nabren, Feb 11, 2019.

  1. Nabren

    Nabren

    Joined:
    Mar 7, 2014
    Posts:
    61
    I have been trying to search the API and Google and so far haven't come up with anything.

    The problem seems simple enough. From a C# script I want to check whether the shader on a given material has a certain flag (e.g., PerRendererData). This seems to be exactly what I want:

    https://docs.unity3d.com/ScriptReference/MaterialProperty.PropFlags.PerRendererData.html

    However, I am not sure how to create a MaterialProperty as that appears to only come from a ShaderGUI implementation which would only run when selected in the inspector. I want a separate editor script that can enumerate all materials in the project at the same time and is run from a menu item.

    I can create a SerializedObject from a Material reference as such:

    var serializedMaterial = new SerializedObject(material);

    But I am still not sure how to convert the properties from a SerializedProperty to a MaterialProperty or if that's even possible.

    Obviously I can get creative and just get the shader asset path and parse the shader text myself, but I was hoping there would be a cleaner way to do this with existing API. I feel like I am missing something obvious. The ShaderUtil class also doesn't seem to support this.

    Any suggestions? Thanks!

    EDIT: After looking at ShaderUtil more closely, it looks like this function is exactly what I want:

    internal static extern MaterialProperty GetMaterialProperty(
    UnityEngine.Object[] mats,
    string name);

    Only, it's internal. I can probably call it through reflection, but I still hold out hope for a much cleaner way.
     
    Last edited: Feb 11, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
  3. Nabren

    Nabren

    Joined:
    Mar 7, 2014
    Posts:
    61