Monkey original, welcome reprint. Reproduced please specify: Reproduced from COCOS2DER-CSDN, thank you!
Original address: http://blog.csdn.net/cocos2der/article/details/46854411
Project, sometimes import some resources, it is necessary to create a material ball, if each time you create, it is very troublesome, the following is how to import resources when the material is automatically created ball.
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityeditor;usingSystem.IO; Public classbuildmaterial:unityeditor.assetmodificationprocessor{//The path of the generated material Private Static stringMaterialspath ="assets/resources/skin/";//Create menu button, manually invoke create material[MenuItem ("Hetools/buildmaterials")]Static voidCreatemateral () {object[] SelectObject = selection.objects; list<string> Path =Newlist<string> ();foreach(Object objinchSelectObject) {path. Add (Assetdatabase.getassetpath (obj)); }foreach(stringPinchPath) {createonemateral (P); } System.GC.Collect (); }//Monitor Assets Resource Add, find the specified directory themetile have the newly added texture, the material will be generated automatically Public Static void Onwillcreateasset(stringPath) {intindex = path. LastIndexOf (".");stringFile = path. Substring (index);string[] Patharr = path. Split ('/');if(Patharr [Patharr.length-3] !="Themetile")return; Createonemateral (path); System.GC.Collect (); }//Create material Ball Static voidCreateonemateral (stringP) {p = p.replace (". Meta",""); Debug.Log ("Createonemateral from Path:"+ p);intpos = P.lastindexof ('/');if(pos = =-1)return;string[] Strarr = P.split ('/');stringThemeidstr = Strarr [Strarr.length-2]; Texture Textur = (Texture) Assetdatabase.loadassetatpath (p,typeof(Texture)) asTexture;stringname = Strarr [Strarr.length-1];inty = name. IndexOf ('. '); Name = name. Substring (0, y); Material Mater =NewMaterial (Shader.find ("Mobile/vertexlit")); Mater.maintexture = Textur; Assetdatabase.createasset (Mater, Materialspath + themeidstr +"/"+ name +". Mat"); }}
Note that in the above code I have stipulated that only the specified directory added texture will automatically generate the material, so use it, please modify it yourself.
Here's a question:
When the map is imported, the automatically created material ball loses the texture map, and the menu button clicks to create the normal. The problem has not been solved, and any friend who knows the solution can tell me.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Automatically generate material material (Unity3d development 19)