using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; using System.IO; public class BuildMaterial : UnityEditor.AssetModificationProcessor { //The path of the generated Material private static string MaterialsPath = "Assets/Resources/Skin/"; // Create a menu button and manually call to create a material [MenuItem ("HETools/BuildMaterials")] static void CreateMateral () { //Get the list of objects Object[] selectObject = Selection.objects; List<string> path = new List<string> (); //Get the resource path from the object list foreach (Object obj in selectObject) { path.Add (AssetDatabase.GetAssetPath (obj)); } //Assign the path to the CreateOneMaterial function to create the material foreach (string p in path) { CreateOneMateral (p); } System.GC.Collect (); } // Monitor the addition of assets resources, and find that the specified directory ThemeTile has a newly added texture, and automatically generate materials public static void OnWillCreateAsset (string path) { int index = path.LastIndexOf ("."); //return file type string file = path.Substring (index); //Extract the separate file name of the path string[] pathArr = path.Split ('/'); / / Determine whether it is the specified resource folder if (pathArr [pathArr.Length - 3] != "ThemeTile") return; //Assign the path to the CreateOneMaterial function to create the material CreateOneMateral (path); System.GC.Collect (); } // create shader static void CreateOneMateral (string p) { //replace suffix name 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;//Load material texture string name = strArr [strArr.Length - 1]; int y = name.IndexOf ('.'); name = name.Substring (0, y); Material mater = new Material (Shader.Find ("Mobile/VertexLit"));//Create a new material and select the corresponding shader mater.mainTexture = textur;//Put the texture on the material AssetDatabase.CreateAsset (mater, MaterialsPath + themeIDStr + "/" + name + ".mat");//New material resource Debug.Log(AssetDatabase.GetAssetPath(mater)); } }This does not automatically generate materials, but I don't know where the code problem is. Does anyone know if they can help? This code is not written by myself, but copied from a certain big guy on the Internet.
Thoughts on automatically generating materials
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324739415&siteId=291194637
Ranking