【Unity】SubstanceParinterから書き出したテクスチャーを自動でマテリアルにする
2020-12-01 2021-02-08
SubstancePainterから書き出したテクスチャーをひとつずつUnityでマテリアルにするのがとても大変なので,テクスチャーをインポートしたディレクトリを指定してあげるだけで,自動的にマテリアルを作成して,テクスチャーを貼り付けてくれるUnityのエディタ拡張。
書き出しのテンプレートを変えたら,適宜スクリプトを変更する必要がある。

Env.
Unity 2019.4.8f1
Substance Painter 2020
SourceCode
SubstanceTexturesImporter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.IO; using System.Collections.Generic; namespace dev.kemomimi.utils { public class SubstanceTexturesImporter : EditorWindow { string inputRawDirectory = ""; [MenuItem("Utils/Substance Tex Importer")] private static void Create() { GetWindow<SubstanceTexturesImporter>("Substance Tex Importer"); } void OnGUI() { GUILayout.Label("Target directory"); inputRawDirectory = GUILayout.TextField(inputRawDirectory); GUILayout.Space(10); if (GUILayout.Button("Apply")) { var targetFullPath = Application.dataPath.Substring(0, Application.dataPath.Length - 7) + "/" + inputRawDirectory; var pathes = Directory.GetFiles(targetFullPath); if (!Directory.Exists(inputRawDirectory + "/_Mats")) { Directory.CreateDirectory(inputRawDirectory + "/_Mats"); } Dictionary<string, List<string>> temp = new Dictionary<string, List<string>>(); foreach (string path in pathes) { if (Path.GetExtension(path) != ".meta") { var fileName = Path.GetFileNameWithoutExtension(path); var lastUnderBarPos = fileName.LastIndexOf("_"); var materialName = fileName.Substring(0, lastUnderBarPos); var texType = fileName.Substring(lastUnderBarPos, fileName.Length - lastUnderBarPos); if (!temp.ContainsKey(materialName)) { temp.Add(materialName, new List<string>()); } temp[materialName].Add(texType); } } var count = 0; var max = temp.Keys.Count; foreach (var materialName in temp.Keys) { var material = new Material(Shader.Find("Standard")); AssetDatabase.CreateAsset(material, inputRawDirectory + "/_Mats/" + materialName + ".mat"); EditorUtility.DisplayProgressBar("Create materials", $"progress {count} / {max}", (float)count / (float)max); count++; foreach (var texType in temp[materialName]) { var texPath = inputRawDirectory + "/" + materialName + texType + ".png"; var texture = AssetDatabase.LoadAssetAtPath<Texture>(texPath); var propName = string.Empty; switch (texType) { case "_AlbedoTransparency": propName = "_MainTex"; break; case "_Normal": //NormalMapの場合必要に応じてTextureTypeを変更 TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; if (textureImporter.textureType != TextureImporterType.NormalMap) { textureImporter.textureType = TextureImporterType.NormalMap; textureImporter.SaveAndReimport(); } propName = "_BumpMap"; break; case "_MetallicSmoothness": propName = "_MetallicGlossMap"; break; default: Debug.LogError($"Unknown tex type : {texType}"); break; } if (!string.IsNullOrEmpty(propName)) { material.SetTexture(propName, texture); } } } EditorUtility.ClearProgressBar(); } } } } #endif |
How to use

テクスチャーが置かれているディレクトリのパスを右クリックのCopyPathで取得して,

Utils/Substance Tex Importerで開いたウィンドウのTarget directoryに貼り付けして,Applyをクリックすることで,Target directoryの中に_Matsフォルダが作成されてマテリアルが生成される。