Skip to content

Instantly share code, notes, and snippets.

@mikelovesrobots
Created March 3, 2015 21:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikelovesrobots/e526809506a17b4db65b to your computer and use it in GitHub Desktop.
Save mikelovesrobots/e526809506a17b4db65b to your computer and use it in GitHub Desktop.
Bulk Material Creator for Unity3d. Just select your textures, then select from the menu Assets > Bulk Material Creator, assign a shader and hit create. MIT License.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class BulkMaterialCreator : ScriptableWizard
{
public Shader Shader;
[MenuItem("Assets/Bulk Material Creator")]
static void CreateWizard() {
ScriptableWizard.DisplayWizard("Bulk Material Creator",typeof(BulkMaterialCreator));
}
void OnWizardUpdate() {
}
void OnWizardCreate() {
foreach (var obj in Selection.objects) {
if (obj.GetType() == typeof(Texture2D)) {
var texture = (Texture2D)obj;
var material = GenerateMaterial(texture);
var path = GetDirectory(obj) + "/" + material.name + ".mat";
AssetDatabase.CreateAsset(material, path);
}
}
}
private Material GenerateMaterial(Texture2D texture) {
var material = new Material(Shader);
material.name = texture.name;
material.mainTexture = texture;
return material;
}
private string GetDirectory(Object obj) {
var path = AssetDatabase.GetAssetPath(obj);
if (path.Contains('/')) {
path = path.Substring(0, path.LastIndexOf('/'));
}
return path;
}
}
@ShayBox
Copy link

ShayBox commented Aug 18, 2022

I modified this script to allow changing which property to apply the texture to and add a suffix to the material name

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class BulkMaterialCreator : ScriptableWizard {

	public string PropertyName = "_MainTex";
	public string Suffix;
	public Shader Shader;

	[MenuItem(itemName: "Assets/Bulk Material Creator")]
	public static void CreateWizard() => DisplayWizard(title: "Bulk Material Creator", klass: typeof(BulkMaterialCreator));

	public void OnWizardUpdate() { }

	public void OnWizardCreate() {
		foreach (var obj in Selection.objects) {
			if (obj.GetType() == typeof(Texture2D)) {
				Texture2D texture = (Texture2D)obj;
				Material material = GenerateMaterial(texture);
				string path = GetDirectory(obj) + "/" + material.name + ".mat";
				AssetDatabase.CreateAsset(asset: material, path);
			}
		}
	}

	private Material GenerateMaterial(Texture2D texture) {
		Material material = new Material(shader: Shader);
		material.name = texture.name + Suffix;
		material.SetTexture(name: PropertyName, value: texture);
		return material;
	}

	private string GetDirectory(Object obj) {
		string path = AssetDatabase.GetAssetPath(assetObject: obj);
		if (path.Contains(value: '/')) {
			path = path.Substring(startIndex: 0, length: path.LastIndexOf(value: '/'));
		}
		return path;
	}
}

Useful for VRChat MatCap Lit shader using property _MatCap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment