Automatically generate material material (Unity3d development 19)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.