批量選擇圖片并轉(zhuǎn)換為材質(zhì)
- 用Selection.GetFiltered來表示選擇到的物體
- 第一個參數(shù)表示僅檢索此類型的對象。
- 第二個參數(shù) SelectionMode.Assets 表示僅返回 Asset 目錄中的資源對象。
- 先檢查當前路徑下是否有此名稱的材質(zhì)存在,若存在則不生成新的材質(zhì)。
- 創(chuàng)建一個材質(zhì)實例。
- 將材質(zhì)創(chuàng)建為Asset資源。
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary> 照片轉(zhuǎn)換為對應的材質(zhì) </summary>
public class ImageTransMaterial : MonoBehaviour
{
[Header("材質(zhì)生成地址")]
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))) //判斷當前材質(zhì)是否存在
{
continue;
}
else
{
Material mat = new Material(Shader.Find("Legacy Shaders/Diffuse")); //實例一個新的材質(zhì)
mat.SetTexture("_MainTex", i as Texture2D); //設置材質(zhì)的貼圖
AssetDatabase.CreateAsset(mat,
materialPath + i.name + ".mat"); //創(chuàng)建材質(zhì),并設置材質(zhì)保存的位置 (需要修改儲存位置)
}
}
}
#endif
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-544279.html
文章來源:http://www.zghlxwxcb.cn/news/detail-544279.html
到了這里,關于Unity 批量選擇圖片并轉(zhuǎn)換為材質(zhì)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!