using System.Collections; using System.Collections.Generic; using UnityEngine; namespace UnityEditor { public class SetTerainMat { [MenuItem("Assets/Generate Material")] public static void CreateMaterial() { if (Selection.activeGameObject == null) return; Debug.Log(Selection.activeGameObject.name); foreach (Transform transform in Selection.activeGameObject.transform) { string name = transform.gameObject.name; Material material = new Material(Shader.Find("Unlit/Texture")); material.name = name; string path = "Assets/Art/Textrue/Terrain/" + name + ".jpg"; UnityEngine.Object tex = AssetDatabase.LoadAssetAtPath(path); Texture2D texture = tex as Texture2D; if (texture != null) { material.SetTexture("_MainTex", texture); } Terrain terrain = transform.gameObject.GetComponent(); terrain.materialTemplate = material; string sapath = "Assets/Art/Material/Terrain/" + name+".mat"; AssetDatabase.CreateAsset(material, sapath); AssetDatabase.SaveAssets(); } AssetDatabase.Refresh(); } } }