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就可以了。