[SOLVED]Editor Tool to create new materials

Hi,

i work on a tool to change all material objects in my level in edit time. I need to apply a different number of textures to each object, with my own shader.

Also i need to create this object in my level as one complete prefab to use it in other scenes. But when i do this, all metarials are lost in the new scene. In the origin scene all materials are still exist.

Has anybody an idea, what to do?

this is my example code from my editor script:

	public void OnWizardCreate () 
	{
		int counter = 0;
		
		foreach( Transform obj in Selection.transforms )
		{
			MeshFilter meshFilter = obj.GetComponent( typeof( MeshFilter) ) as MeshFilter;
			if( meshFilter != null )
			{
				Material mat = new Material( Shader.Find ( shaderNames[numberLayers] ) );

				mat.SetTexture( "_BaseTex", layer1Tex );
				meshFilter.renderer.sharedMaterial = mat;
                                counter++;

			}
		}
		
		EditorUtility.DisplayDialog( "Message", "materials replaced: " + counter, "OK" );
	}

Maybe it has to do with the different of sharedMaterial and material of the renderer object. But i don’t know the “exact” sense and what “realy” happen.

On the other side it makes sense to create a new material in the asset directory for all my dynamic generated materials, but how?

How can i create, for example, 60 materials and store them in the asset folder within a editor script?

Thanks, Horst

Assigning to the sharedMaterial property can modify an existing material, but I don’t think there is any way for it to create a completely new material. You could create lots of material files in the editor manually and then assign them to your objects and then set sharedMaterial from the script, but that doesn’t seem to save you much work.

Perhaps you could try sending an e-mail to Unity support about this (support@unity3d.com), but I think there is probably no way to get the result you want here, unfortunately.

Ok -now i know that there is no way to create a asset material by script.

But i guess i have found another way to solve my problem. I could make copies of a existing *.mat file in the asset folder of my unity 3d project and use them for the dynamic material generation tool.

Unity3D accept a copy of a *.mat file. Now i can make a MAC Batch file to create 100 *.mat files.

Thanks