Created
April 23, 2019 05:15
-
-
Save beardordie/dd1850e51f48bbb983cb7ace8609b59b to your computer and use it in GitHub Desktop.
Unity Editor script. Select multiple texture files for one material in Project window, then click Tools -> Create Materials For Textures. A new material will be created. Adjust Smoothness and other settings to match expected render.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
// Original source: http://answers.unity.com/answers/746039/view.html | |
public class CreateMaterialsForTextures : ScriptableWizard | |
{ | |
[MenuItem("Tools/CreateMaterialsForTextures")] | |
static void CreateMaterials() | |
{ | |
int selectedTextures = 0; | |
try | |
{ | |
AssetDatabase.StartAssetEditing(); | |
var textures = Selection.GetFiltered(typeof(Texture), SelectionMode.Assets).Cast<Texture>(); | |
foreach (var tex in textures) | |
{ | |
selectedTextures++; | |
string texturePath = AssetDatabase.GetAssetPath(tex); | |
string assetFolderPath = texturePath.Substring(0, texturePath.LastIndexOf("/")); | |
string textureFileName = texturePath.Substring(texturePath.LastIndexOf("/")).ToLower(); | |
string materialFilePath = assetFolderPath + "/NewMaterial.mat"; | |
Material mat; | |
if (AssetDatabase.LoadAssetAtPath(materialFilePath, typeof(Material)) != null) | |
{ | |
mat = (Material)AssetDatabase.LoadAssetAtPath(materialFilePath, typeof(Material)); | |
} else | |
{ | |
mat = new Material(Shader.Find("Standard")); | |
} | |
if(textureFileName.Contains("diffuse") || textureFileName.Contains("albedo") ) | |
{ | |
mat.mainTexture = tex; | |
} | |
else if (textureFileName.Contains("normal")) | |
{ | |
mat.SetTexture("_BumpMap", tex); | |
mat.EnableKeyword("_NORMALMAP"); | |
} | |
else if (textureFileName.Contains("height")) | |
{ | |
mat.SetTexture("_ParallaxMap", tex); | |
mat.EnableKeyword("_PARALLAXMAP"); | |
} | |
else if(textureFileName.Contains("metallic") || (textureFileName.Contains("glossiness"))) | |
{ | |
mat.SetTexture("_MetallicGlossMap", tex); | |
mat.EnableKeyword("_METALLICGLOSSMAP"); | |
} | |
else if (textureFileName.Contains("occlusion") || textureFileName.Contains("ambient") || textureFileName.Contains("_ao") || textureFileName.Contains(" ao")) | |
{ | |
mat.SetTexture("_OcclusionMap", tex); | |
} | |
else if (textureFileName.Contains("reflection") || textureFileName.Contains("specular")) | |
{ | |
mat.SetTexture("_SpecGlossMap", tex); | |
mat.EnableKeyword("_SPECGLOSSMAP"); | |
} | |
if (AssetDatabase.LoadAssetAtPath(materialFilePath, typeof(Material)) == null) | |
{ | |
AssetDatabase.CreateAsset(mat, materialFilePath); | |
} | |
} | |
} | |
finally | |
{ | |
AssetDatabase.StopAssetEditing(); | |
AssetDatabase.SaveAssets(); | |
if(selectedTextures==0) | |
{ | |
Debug.LogError("No textures were selected. Material not created."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment