unity脚本获取AlwayIncludedShaders

大纲

  • 大致原理
  • 具体

大致原理

AlwaysIncludedShadersunityProject Setting->Graphics中有展示。但是无论是去查看RenderSettingsGraphicsSettingsEditorGraphicsSettings下都没有这个的一个字段或者方法!

png

所以最终的解决就是使用放射的方式去访问!

具体

GraphicsSettings.GetGraphicsSettings()返回的是一个UnityEngine.Object的对象,但时这个对象里就有我们需要的AlwaysIncludedShaders

通过查看unity反射的代码,可以看到以下的内容:

png

var graphicsSettings = GraphicsSettings.GetGraphicsSettings();
var serializedObject = new SerializedObject(graphicsSettings);
SerializedProperty serializedProperty = serializedObject.FindProperty("m_AlwaysIncludedShaders");

这样就能获取到m_AlwaysIncludedShaders的数组了!

然后我们能通过SerializedPropertym_AlwaysIncludedShaders进行读写了!

完整代码!

var graphicsSettings = GraphicsSettings.GetGraphicsSettings();
var serializedObject = new SerializedObject(graphicsSettings);
SerializedProperty serializedProperty = serializedObjeFindProperty("m_AlwaysIncludedShaders");
if (serializedProperty.isArray)
{
    for (int x = 0; x < serializedProperty.arraySize; x++)
    {
        SerializedProperty property = serializedProperGetArrayElementAtIndex(x);
        var sh = property.objectReferenceValue;
        Debug.LogError(sh);
    }
}