Unity3D 一键更改预设的图片
现在的大多的公司已经拥有的自己的项目,对于一般的公司来说,重新研发的成本太大,所有大部分的公司都会选择拿之前写好的项目还换皮。这是一种体力活,为了省时省力,在一些重复的资源操作我们可以用插件一键完成,比如一键替换字体,图片,文字等等。
//一键修改预设里面的图片
[MenuItem("Custom Editor/Build/PCAssetbundle/ChangeIconName")]
static void BuildPCChangeIconName()
{
//存放预设目录
string path = uiAssetsPath_ + "/";
List<GameObject> files = new List<GameObject>();
string[] filePaths = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
for (int i = 0; i < filePaths.Length; ++i)
{
if (filePaths[i].Contains(".meta"))
continue;
string filePath = cut2AssetFolder(filePaths[i]);
GameObject file = AssetDatabase.LoadAssetAtPath(filePath, typeof(GameObject)) as GameObject;
if (file != null)
files.Add(file);
}
List<string> allText = new List<string>();
for (int i = 0; i < files.Count; i++)
{
GameObject go = files[i];
Transform[] grandFa = go.GetComponentsInChildren<Transform>(true);
//遍历每个预设所有的的UISprite
foreach (Transform child in grandFa)
{
UISprite spr = child.GetComponent<UISprite>();
if (spr != null && spr.spriteName == "jinbi") //取出你的目标
{
string str = "预设名字:" + go.name + " 图片名:" + spr.name + " 引用的图集名" + spr.spriteName + " 图片宽:" + spr.width;
UnityEngine.Debug.Log(str);
allText.Add(str);
if (spr.width > 40)
{
spr.MakePixelPerfect();
}
else
{
spr.spriteName = "xiaojinbi"; //修改为另一张图片
spr.MakePixelPerfect();
}
}
}
EditorUtility.SetDirty(go);
}
AssetDatabase.SaveAssets();
if (allText.Count > 0)
{
//打印更改预设记录
FileStream fs = new FileStream("MyLog.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < allText.Count; i++)
{
sw.WriteLine(allText[i]);
}
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
把脚本放在unity的Editor目录下,在unity菜单下运行ChangeIconName就可以了。
当然了,修改字体也是一样的,把UISprite 换成UIlabel就可以了。