using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; using System.IO; public class BuildMaterial : UnityEditor.AssetModificationProcessor { //生成出的Material的路径 private static string MaterialsPath = "Assets/Resources/Skin/"; // 创建菜单按钮,手工调用创建材质 [MenuItem ("HETools/BuildMaterials")] static void CreateMateral () { //获取物体列表 Object[] selectObject = Selection.objects; List<string> path = new List<string> (); //从物体列表中获取资源路径 foreach (Object obj in selectObject) { path.Add (AssetDatabase.GetAssetPath (obj)); } //将路径赋给CreateOneMateral函数以创建材质 foreach (string p in path) { CreateOneMateral (p); } System.GC.Collect (); } // 监控assets资源添加,发现指定目录ThemeTile有新增加的texture,就自动生成材质 public static void OnWillCreateAsset (string path) { int index = path.LastIndexOf ("."); //返回文件类型 string file = path.Substring (index); //提取路径的单独文件名称 string[] pathArr = path.Split ('/'); //判断是否为指定资源文件夹 if (pathArr [pathArr.Length - 3] != "ThemeTile") return; //将路径赋给CreateOneMateral函数以创建材质 CreateOneMateral (path); System.GC.Collect (); } // 创建材质球 static void CreateOneMateral (string p) { //替换后缀名 p = p.Replace (".meta", ""); Debug.Log ("CreateOneMateral from path: " + p); int pos = p.LastIndexOf ('/'); if (pos == -1) return; string[] strArr = p.Split ('/'); string themeIDStr = strArr [strArr.Length - 2]; Texture textur = (Texture)AssetDatabase.LoadAssetAtPath (p, typeof(Texture)) as Texture;//加载材质贴图 string name = strArr [strArr.Length - 1]; int y = name.IndexOf ('.'); name = name.Substring (0, y); Material mater = new Material (Shader.Find ("Mobile/VertexLit"));//新建材质并选择相应的着色器 mater.mainTexture = textur;//将贴图放到材质上 AssetDatabase.CreateAsset (mater, MaterialsPath + themeIDStr + "/" + name + ".mat");//新建材质资源 Debug.Log(AssetDatabase.GetAssetPath(mater)); } }这个并不能实现自动生成材质,但是不知道代码问题在哪,有哪位大佬知道能帮帮忙吗?这个代码也不是我自己写的,是在网上复制某一个大佬的。
自动生成材质的思考
猜你喜欢
转载自blog.csdn.net/Nonbady/article/details/80061010
今日推荐
周排行