Unityでマテリアルのプロパティをバッチ変更する方法

Unityがfbxリソースファイルをインポートするときは、マテリアルの特定のプロパティを変更する必要があります。実際、特定のプロパティを同じ値に変更するのは比較的簡単です。すべてのマテリアルを選択してから、インスペクター。すべての材料特性を変更します。

たくさんの素材を変更する場合、それぞれのプロパティは異なりますか?私はそれを行うためにここにスクリプトを書きました。具体的な手順は次のとおりです。

1.マテリアルディレクトリに従ってすべてのマテリアルパスを取得します

2.マテリアルパスに従ってすべてのマテリアルをロードし、各マテリアルのプロパティを変更します。

3.変更した資料を保存します。

以下は、参考のために私がどのようにベーキング材料を変更しているかの例を示しています。主な例は、エミッションの下のカラーマップがアルベドマップと一致していることです。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ModifyMatrial : MonoBehaviour
{
    [SerializeField]
    private string MatrialsDir;

    public List<string> GetMaterialsPath(string targetFilePath, string searchType)
    {
        List<string> matrialPaths = new List<string>();

        if (Directory.Exists(targetFilePath))
        {
            string[] guids;
            //搜索
            guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath });
            foreach (string guid in guids)
            {
                string source = AssetDatabase.GUIDToAssetPath(guid);
                matrialPaths.Add(source);
            }
        }

        return matrialPaths;
    }

    public void ModifyMatrials()
    {
        AssetDatabase.DisallowAutoRefresh();

        List<string> matrialPaths = GetMaterialsPath(MatrialsDir, "Material");
        foreach(var one in matrialPaths)
        {
            Material mat = AssetDatabase.LoadAssetAtPath<Material>(one);
            if(mat != null)
            {
                Texture mainTex = mat.GetTexture("_MainTex");
                mat.SetTexture("_EmissionMap", mainTex);
            }
        }

         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
         AssetDatabase.AllowAutoRefresh();
    }
}

おすすめ

転載: blog.csdn.net/grace_yi/article/details/122731729