LittleTool之批量修改材质

时间:2023-03-09 15:25:31
LittleTool之批量修改材质
using UnityEngine;
using System.Collections;
using UnityEditor; public class ChangeMaterial : EditorWindow
{ static string path = "Assets/_Materials/";
static string shaderName="Custom/CurvedWorld"; static string tempName1="_Curvature";//材质球参数,需要手动修改
static string tempName2="_Axis";
static float temp1=;
static float temp2=; public static string [] postfix =
{
".png",".jpg",".tga","psd"
}; [MenuItem ("Custom/ChangeMaterial")]
public static void Change()
{
if (Selection.activeGameObject != null) { foreach (GameObject go in Selection.gameObjects) {
Renderer render = go.GetComponentInChildren<Renderer> ();
if (render != null) { Texture texture = GetTexture (go.name);//根据对象名获取图片
if (texture != null) {
Material newMat = new Material (Shader.Find(shaderName)); newMat.SetFloat(tempName1,temp1);
newMat.SetFloat (tempName2,temp2); AssetDatabase.CreateAsset (newMat, path + go.name+".mat");
render.sharedMaterial=newMat;
render.sharedMaterial.mainTexture = texture;
Debug.Log ("成功!");
} else {
Debug.Log ("失败!");
}
}
}
}
}
static Texture GetTexture(string name)
{
foreach(string str in postfix)
{
Texture texture = AssetDatabase.LoadAssetAtPath("Assets/_Textures/" + name+str,typeof(Texture)) as Texture;
if(texture != null)
return texture;
}
return null;
}
}

注意:

  1.将要用到的模型都制作为预制体!!!

  2.手动修改参数