-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterialGeneratorMultiple.cs
More file actions
71 lines (60 loc) · 2.4 KB
/
MaterialGeneratorMultiple.cs
File metadata and controls
71 lines (60 loc) · 2.4 KB
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
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class MaterialGeneratorMultiple
{
[MenuItem("Assets/Create Materials Multiple ")]
private static void CreateMaterials()
{
Dictionary<string, List<Texture2D>> textureSets = new Dictionary<string, List<Texture2D>>();
foreach (Object o in Selection.objects)
{
if (o.GetType() != typeof(Texture2D))
{
Debug.LogError("This isn't a texture: " + o);
continue;
}
Texture2D selected = o as Texture2D;
string textureName = selected.name;
string prefix = textureName.Substring(0, textureName.LastIndexOf('_'));
if (!textureSets.ContainsKey(prefix))
{
textureSets[prefix] = new List<Texture2D>();
}
textureSets[prefix].Add(selected);
}
foreach (var textureSet in textureSets)
{
string prefix = textureSet.Key;
List<Texture2D> textures = textureSet.Value;
Material material = new Material(Shader.Find("Standard"));
foreach (Texture2D texture in textures)
{
string suffix = texture.name.Substring(texture.name.LastIndexOf('_') + 1);
switch (suffix)
{
case "D":
material.SetTexture("_MainTex", texture);
break;
case "M":
material.SetTexture("_MetallicGlossMap", texture);
break;
case "N":
material.SetTexture("_BumpMap", texture);
break;
case "AO":
material.SetTexture("_OcclusionMap", texture);
break;
default:
Debug.LogWarning("Unrecognized suffix: " + suffix);
break;
}
}
// Create material asset
string newAssetName = "Assets/Materials/" + prefix + "_Mat.mat"; // Modify this path if necessary
AssetDatabase.CreateAsset(material, newAssetName);
AssetDatabase.SaveAssets();
Debug.Log("Done! Created material: " + newAssetName);
}
}
}