Copy/paste saved properties of a material - Editor script?

Before i do my own, does anyone have an editor script in their possession (and is willing to share) that copies all the saved properties of a material (Texture, _Color, _SpecColor, _Emission, …) to a buffer and lets the user paste them onto another material?

Like the copy/paste transform from the Unify wiki, but for material values.

This would be great :slight_smile:
woodn

I don’t know of an example, but it can’t be hard:

Hey Jessy

Thanks for answering and your tipp!

I think i almost got it! The selected material changes to the values of the copied material, but it doesn’t “stick” or change the material in the assets. I just can see the change in the scene view, but not in the assets, where it should change it. Do i have to “save” it somehow to the selected asset??

Here’s what i got so far:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class MaterialValuesCopier : ScriptableObject
{
	private static Material mat;

    [MenuItem ("Custom/Copy Material Values")]
    static void DoRecord()
    {
    	foreach (Material m in Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets))
        {
        	mat = m;
        }
    }
 
    [MenuItem ("Custom/Paste Material Values")]
    static void DoApply()
    {
		foreach (Material m in Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets))
        {
        	m.CopyPropertiesFromMaterial(mat);
        }
    }
}

ok, this seems to be the exact same problem, but no solution unfortunately :frowning:

http://forum.unity3d.com/viewtopic.php?t=47781

here’s my workaround :wink:

any comments or ideas for improvement?

using UnityEngine;
using UnityEditor;
using System.Collections;


// This script copies a Material from the assets (incl. shader, texture, colors,...) 
// and applies it ( with all the defined values below) to another material in the assets.

// It's a workaround for "CopyPropertiesFromMaterial()", which i couldn't get to work :-(

// Select one material in the project folder, use Custom > Copy Material Values 
// from the menu, then select another material in the project folder and use 
// Custom > Paste Material Values from the menu. Use undo if you made a mistake.

// Feel free to extend the script below if you need stuff like positioning and tiling of 
// textures transfered to another material.

// oh, and this is my first c# script, so it might be st00pid!

// [url]http://forum.unity3d.com/viewtopic.php?t=56687[/url]


public class MaterialValuesCopier : ScriptableObject
{
	private static Material mat;
	
	private static Shader shad;
	
	private static Color mcol;
	private static Color mspec;
	private static Color memis;
	private static Color mref;
	private static float mshine;
	
	private static Texture mtex;
	private static Texture mbump;
	private static Texture mlight;
	private static Texture mcube;
	

    [MenuItem ("Custom/Copy Material Values")]
    static void DoRecord()
    {
    	
    	foreach (Material m in Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets))
        {
        	Undo.RegisterUndo (m, "Material Copy Change");
        	mat = m;
        	
        	shad = m.shader;
        	        	
        	if(m.HasProperty("_Color"))
        	mcol = m.GetColor("_Color");
        	
        	if(m.HasProperty("_SpecColor"))
        	mspec = m.GetColor("_SpecColor");
        	
        	if(m.HasProperty("_Emission"))
        	memis = m.GetColor("_Emission");
        	
        	if(m.HasProperty("_ReflectColor"))
        	mref = m.GetColor("_ReflectColor");
        	
        	if(m.HasProperty("_Shininess"))
        	mshine = m.GetFloat("_Shininess");
        	
        	if(m.HasProperty("_MainTex"))
        	mtex = m.GetTexture("_MainTex");
        	
        	if(m.HasProperty("_BumpMap"))
        	mbump = m.GetTexture("_BumpMap");
        	
        	if(m.HasProperty("_LightMap"))
        	mlight = m.GetTexture("_LightMap");
        	
        	if(m.HasProperty("_Cube"))
        	mcube = m.GetTexture("_Cube");
        	
        	
        }
    }
 
    [MenuItem ("Custom/Paste Material Values")]
    static void DoApply()
    { 
		foreach (Material mn in Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets))
        {
        	Undo.RegisterUndo (mn, "Material Paste Change");
        	mn.shader = shad;
        	
        	if(mn.HasProperty("_Color"))
        	mn.SetColor("_Color", mcol);
        	
        	if(mn.HasProperty("_SpecColor"))
        	mn.SetColor("_SpecColor", mspec);
        	
        	if(mn.HasProperty("_Emission"))
        	mn.SetColor("_Emission", memis);
        	
        	if(mn.HasProperty("_ReflectColor"))
        	mn.SetColor("_ReflectColor", mref);
        	
        	if(mn.HasProperty("_Shininess"))
        	mn.SetFloat("_Shininess", mshine);
        	
        	if(mn.HasProperty("_MainTex"))
        	mn.SetTexture("_MainTex", mtex);
        	
        	if(mn.HasProperty("_BumpMap"))
        	mn.SetTexture("_BumpMap", mbump);
        	
        	if(mn.HasProperty("_LightMap"))
        	mn.SetTexture("_LightMap", mlight);
        	
        	if(mn.HasProperty("_Cube"))
        	mn.SetTexture("_Cube", mcube);
        }
    }
}

I’ve been playing around with CopyPropertiesFromMaterial() for a while now. It does seem worthless. As soon as you use

EditorApplication.SaveAssets()

or save the project, then whatever you did with it is undone. I’m sorry I recommended that to you; I had no idea how much of our time it would waste. :x

don’t worry, it was actually a good trick to get me started. :wink:

still the workaround isn’t as great as that function (if it would work) might would be…