自动生成材质Material

项目中,有时候导入一些资源时候,需要对应创建材质球,如果每次自己动手创建,还是挺麻烦的,下面是如何导入资源时候自动创建材质球。 
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 path = new List (); 
foreach (Object obj in selectObject) { 
path.Add (AssetDatabase.GetAssetPath (obj)); 

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 (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"); 

}



注意,上面代码中我是规定了只有指定的目录添加texture才会自动生成材质,所以使用时候,请自行修改下。 
这里发现了个问题: 
导入贴图时候,自动创建出来的材质球丢失了纹理图,而采用菜单按钮点击创建出来的正常。问题还没有解决,有哪位朋友知道解决办法可以告诉我下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值