LoginSignup
6
4

More than 5 years have passed since last update.

マテリアルについているテクスチャを取得する

Posted at

エディタ拡張でマテリアルのテクスチャを取得する方法。シェーダのプロパティ名が既知の場合はマテリアルからプロパティ名で取得すればいいが、不明な場合にマテリアルが参照されているすべてのテクスチャをとってくる。

正しいシェーダがついている場合

var count = ShaderUtil.GetPropertyCount(mat.shader);
for (int i = 0; i < count; i++)
{
    var pt = ShaderUtil.GetPropertyType(mat.shader, i);
    if (pt == ShaderUtil.ShaderPropertyType.TexEnv)
    {
        var pn = ShaderUtil.GetPropertyName(mat.shader, i);
        var tex = mat.GetTexture(pn);
        if (tex)
        {
            Debug.Log(tex.name);
        }
    }
}

シェーダがついていない、または別のシェーダがついている場合

いったん、SerializedObjectにしてすべてのプロパティからテクスチャっぽいものを探す。

var so = new SerializedObject(mat);
SerializedProperty sp = so.GetIterator();
while (sp.NextVisible(true))
{
    if (sp.type.Contains("Texture"))
    {
        var tex = sp.objectReferenceValue as Texture;
        if (tex)
        {
            Debug.Log(tex.name);
        }
    }
}
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4