有时候材质做完后需要更改贴图,或者增加贴图,数量少的时候可以一张张添加和修改,数量多的时候就只能代码生成了。原理是通过名字的关联:主贴图和法线贴图大多数只是后缀的不同上,如果不是那是美术规范没做好啊,代码很简单,基本是编辑器类的代码。
这个是还没完善的特定版代码,如直接使用需要更改文件夹名字和贴图后缀。
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; using System.IO; using System.Text; [ExecuteInEditMode] public class Transferandmapnormal : EditorWindow { [MenuItem("Window/AutoAddNormal")] //打开一个新的窗口 static void AutoAddNormalinmaterials() { Rect wr = new Rect(0, 200, 500, 500); Transferandmapnormal window = (Transferandmapnormal)EditorWindow.GetWindowWithRect(typeof(Transferandmapnormal), wr, true, "AutoAddNormal"); window.Show(); } public List<Material> scensmaterial = new List<Material>(); public List<string> sourceTexturespath = new List<string>(); private string textureSuffix = "_NRM.jpg"; private string docPath = "DWSJ.fbm"; //public Material[] scensmaterial; //绘制窗口时调用 void OnGUI() { EditorGUILayout.LabelField("自动添加法线贴图或其他贴图", GUILayout.Width(200), GUILayout.Height(200)); GUILayout.BeginArea(new Rect(0, 80, 300, 80), "", "Box"); // textureSuffix = EditorGUILayout.TextField("输入贴图后缀:", textureSuffix); if (GUILayout.Button("获取场景材质", GUILayout.Width(200))) { if (scensmaterial != null) { scensmaterial.Clear(); } if (sourceTexturespath != null) { sourceTexturespath.Clear(); } GetScenesmaterial(); } if (GUILayout.Button("应用法线贴图到材质", GUILayout.Width(200))) { FindandapplyNormalmap(); } if (GUILayout.Button("关闭窗口", GUILayout.Width(200))) { this.Close(); } if (GUILayout.Button("除去法线贴图", GUILayout.Width(200))) { testpath(); } GUILayout.EndArea(); } private Material tempmaterial; //获取场景内的材质球 //获取材质球的贴图名称和路径 void GetScenesmaterial() { Object[] selection = Selection.GetFiltered(typeof(Material), SelectionMode.Editable | SelectionMode.TopLevel); if (selection.Length == 0) return; foreach (Material test in selection) { if (test.mainTexture != null) { scensmaterial.Add(test); sourceTexturespath.Add(docPath + "/" + test.mainTexture.name + textureSuffix); } else { scensmaterial.Add(null); sourceTexturespath.Add("0"); } } } //在相应的文件夹里获取法线贴图 //把法线贴图贴到material对应的位置 public List<Texture> tempTex; public void FindandapplyNormalmap() { for (int i = 0; i < scensmaterial.Count; i++) { if (!sourceTexturespath[i].Equals("0")) { //string path = AssetDatabase.GetAssetPath(targetObj); // Assets / Resources / DWSJ.fbm / DWSJ00B.jpg TextureImporter texture = (TextureImporter)AssetImporter.GetAtPath("Assets/Resources" + "/" + sourceTexturespath[i]); Debug.Log("Assets/Resources" + "/" + sourceTexturespath[i]); if (texture != null) { texture.textureType = TextureImporterType.Bump; AssetDatabase.ImportAsset("Assets/Resources" + "/" + sourceTexturespath[i]); string correctString=sourceTexturespath[i].Replace(".jpg",""); Debug.Log(correctString); Texture TempTexture =Resources.Load(correctString) as Texture; scensmaterial[i].SetTexture("_BumpMap", TempTexture); } } } AssetDatabase.Refresh(); } public void testpath() { Object[] selection = (Object[])Selection.objects; //合法性处理 if (selection.Length == 0) return; //批量导入贴图 foreach (Object obj in selection) { //取得每一张贴图 Texture texture = (Texture)obj; //获得贴图路径 string localpath = AssetDatabase.GetAssetPath(texture); Debug.Log(localpath); TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(localpath); //设置贴图类型 importer.textureType = TextureImporterType.Bump; //导入项目资源 AssetDatabase.ImportAsset(localpath); } AssetDatabase.Refresh(); } }
api解析:
[ExecuteInEditMode]//在编辑器模式下可调用
[MenuItem("Window/AutoAddNormal")]//在window工具栏下增AutoAddNormal选项
window.Show();//展示新的窗口
GUILayout.Button("应用法线贴图到材质", GUILayout.Width(200))//添加按钮
Object[] selection = Selection.GetFiltered(typeof(Material), SelectionMode.Editable | SelectionMode.TopLevel);//把选取的物体过滤掉非Material类型的object到object选项。
TextureImporter –textureimporeter类可以调各种图片输入器的参数,包括图片的类型也是在这里调节。
AssetDatabase.ImportAsset(localpath);//重新载入asset资源
importer.textureType = TextureImporterType.Bump;//将图片的类型改为法线
AssetDatabase.Refresh();//刷新资源
使用方法:
1.在project选取材质
2.点击按键2,批量获取材质和贴图路径
3.获取法线贴图路径并且贴在材质球上
注意事项:(1.法线贴图要放在Resources文件夹上, 2.这个脚本需要修改再使用,因为我还没做通用版本,有些是我自己的文件路径,所以使用需要修改
private string textureSuffix = "_NRM.jpg"; private string docPath = "DWSJ.fbm";这两句,替换到你自己的贴图后缀和文件名字。
)