Batch select pictures and convert them to materials
- Use Selection.GetFiltered to represent the selected object
- The first parameter means to retrieve only objects of this type.
- The second parameter, SelectionMode.Assets, means only returning resource objects in the Asset directory.
- First check whether there is a material with this name under the current path, and if it exists, no new material will be generated.
- Create a material instance.
- Create materials as Asset resources.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class ImageTransMaterial : MonoBehaviour
{
[Header("材质生成地址")]
public string materialPath;
#if UNITY_EDITOR
[MenuItem("GameObject/Create Materials")]
public void SetMat()
{
Object[] m_objects = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets);
foreach (var i in m_objects)
{
Debug.Log(i.name);
if (AssetDatabase.LoadAssetAtPath( materialPath + i.name + ".mat",
typeof(Material)))
{
continue;
}
else
{
Material mat = new Material(Shader.Find("Legacy Shaders/Diffuse"));
mat.SetTexture("_MainTex", i as Texture2D);
AssetDatabase.CreateAsset(mat,
materialPath + i.name + ".mat");
}
}
}
#endif
}