-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterialGenerator.cs
More file actions
62 lines (54 loc) · 2.11 KB
/
MaterialGenerator.cs
File metadata and controls
62 lines (54 loc) · 2.11 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
using UnityEngine;
using UnityEditor;
public class MaterialGenerator
{
[MenuItem("Assets/Create Material")]
private static void CreateMaterial()
{
Texture2D diffuseTexture = null;
Texture2D metallicTexture = null;
Texture2D normalTexture = null;
Texture2D aoTexture = null;
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 suffix = textureName.Substring(textureName.LastIndexOf('_') + 1);
switch (suffix)
{
case "D":
diffuseTexture = selected;
break;
case "M":
metallicTexture = selected;
break;
case "N":
normalTexture = selected;
break;
case "AO":
aoTexture = selected;
break;
default:
Debug.LogWarning("Unrecognized suffix: " + suffix);
break;
}
}
Material material = new Material(Shader.Find("Standard"));
// Set textures to corresponding slots
material.SetTexture("_MainTex", diffuseTexture);
material.SetTexture("_MetallicGlossMap", metallicTexture);
material.SetTexture("_BumpMap", normalTexture);
material.SetTexture("_OcclusionMap", aoTexture);
// Create material asset
string materialName = Selection.objects[0].name.Split('_')[0]; // Assumes all textures share the same prefix
string newAssetName = "Assets/Materials/" + materialName + "_Mat.mat"; // Modify this path if necessary
AssetDatabase.CreateAsset(material, newAssetName);
AssetDatabase.SaveAssets();
Debug.Log("Done! Created material: " + newAssetName);
}
}