unity 材质球无法编辑_【每天一个Unity技巧】批量修改材质球的主贴图和添加法线贴图...

本文介绍了如何使用Unity编辑器脚本批量修改材质球的主贴图并添加法线贴图。通过选择深度资产,脚本会遍历每个材质球,根据主贴图名称替换为相同名字但不同后缀的贴图,并自动附加对应的法线贴图。最后,脚本保存更改并刷新资源库。

注释以后有空再写吧,加班到现在还没吃饭呢

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;

using System.IO;

using System.Text.RegularExpressions;

///

/// 根据材质球上的主贴图名字进行改为同名的其他后缀的贴图,并且加上法线贴图

///

public class Replace

{

[MenuItem("Tools/replace")]

public static void Rename()

{

Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//选择的所以对象

foreach (Object item in m_objects)

{

string path = AssetDatabase.GetAssetPath(item);

Debug.Log("材质球的位置:");

Debug.Log(path);

if (Path.GetExtension(path) != "")//判断路径是否为空

{

if (item.GetType() == typeof(Material))

{

Material m = (Material)item;

path = AssetDatabase.GetAssetPath(m.mainTexture);

Debug.Log("材质球上主贴图的位置:");

Debug.Log(path);

string[] strs = path.Split('.');

string afterpath = strs[0] + ".jpg";

Debug.Log("材质球上主贴图相应后缀的位置:");

Debug.Log(afterpath);

if (AssetDatabase.LoadAssetAtPath(afterpath))

m.mainTexture = AssetDatabase.LoadAssetAtPath(afterpath);

string strNormalMap = strs[0] + "_NRM.jpg";

Texture texturebump = AssetDatabase.LoadAssetAtPath(strNormalMap);

if (texturebump != null)

m.SetTexture("_BumpMap", texturebump);

}

}

}

AssetDatabase.SaveAssets();

AssetDatabase.Refresh();

Debug.Log("Complete!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值