Last active
April 13, 2020 18:30
-
-
Save gekidoslair/c810277b6a2409dce35f7965cbf45dbc to your computer and use it in GitHub Desktop.
Batch apply textures to materials
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
[System.Serializable] | |
public class MaterialEntry | |
{ | |
public Material material; | |
public List<Texture2D> textures; | |
} | |
public class TextureProcessor | |
{ | |
public List<MaterialEntry> materialEntries = new List<MaterialEntry>(); | |
public void Process() | |
{ | |
foreach (var entry in materialEntries) | |
{ | |
ProcessTexturesForMaterial(entry); | |
} | |
} | |
private static void ProcessTexturesForMaterial(MaterialEntry entry) | |
{ | |
foreach (var texture in entry.textures) | |
{ | |
SetTexture(texture, entry.material); | |
} | |
AssetDatabase.Refresh(); | |
} | |
/// <summary> | |
/// Grab the end of the string for our naming convention and see if it matches a known convention | |
/// </summary> | |
/// <param name="texture"></param> | |
/// <param name="material"></param> | |
private static void SetTexture(Texture2D texture, Material material) | |
{ | |
var split = texture.name.Split('_'); | |
var idx = split.Length - 1; | |
// Debug.Log("Postfix for texture: " + texture.name + " : " + split[idx]); | |
var suffix = split[idx].ToLower(); | |
switch (suffix) | |
{ | |
case "normal": | |
case "nrm": | |
{ | |
// make sure the texture is a normal map | |
SetTextureForProperty("_BumpMap", texture, material); | |
break; | |
} | |
case "ao": | |
case "occlusion": | |
{ | |
SetTextureForProperty("_OcclusionMap", texture, material); | |
break; | |
} | |
case "metallic": | |
case "metallicalpha": | |
{ | |
SetTextureForProperty("_MetallicGlossMap", texture, material); | |
break; | |
} | |
case "diffuse": | |
case "basecolor": | |
{ | |
SetTextureForProperty("_MainTex", texture, material); | |
break; | |
} | |
case "hdrp": | |
{ | |
SetTextureForProperty("_MaskMap", texture, material); | |
break; | |
} | |
case "roughness": | |
{ | |
SetTextureForProperty("_SpecGlossMap", texture, material); | |
break; | |
} | |
case "specular": | |
{ | |
SetTextureForProperty("_SpecGlossMap", texture, material); | |
break; | |
} | |
default: | |
{ | |
Debug.Log("Unknown texture for material: " + texture.name + " suffix: " + suffix); | |
break; | |
} | |
} | |
//var textPropNames = material.GetTexturePropertyNames(); | |
//foreach (var textProp in textPropNames) | |
//{ | |
// Log("Found texture property: " + textProp); | |
//} | |
} | |
/// <summary> | |
/// For the specified material, set the given property with the provided texture | |
/// </summary> | |
/// <param name="textProp">property you want to map</param> | |
/// <param name="texture">the texture you want to assign</param> | |
/// <param name="material">the material you are working with</param> | |
private static void SetTextureForProperty(string textProp, Texture2D texture, Material material) | |
{ | |
Debug.Log("property: " + textProp + " texture: " + texture.name); | |
material.SetTexture(textProp, texture); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment