Unity3d通用工具類(lèi)之NGUI圖集分解

原文地址:http://www.cnblogs.com/CaomaoUnity3d/p/6043395.html

原文補(bǔ)充:Directory在System.IO 空間下,所以需要導(dǎo)入這個(gè)System.IO


Unity3d通用工具類(lèi)之NGUI圖集分解

由于最近需要一些美術(shù)資源嗎,但是無(wú)奈自己不會(huì)制作UI,所以就打算去網(wǎng)上的項(xiàng)目中直接找?guī)讖埧梢允褂玫馁N圖資源。

但是發(fā)現(xiàn)這些資源已經(jīng)被NGUI自帶的打包圖集工具打包好了,而且原小貼圖也已經(jīng)全部刪掉了,只剩下一個(gè)預(yù)制物。

那么這個(gè)預(yù)制物里面包含什么呢:

1.一張大圖集貼圖

2.大貼圖的材質(zhì)球

3.掛上UIAtla腳本的預(yù)制物

那么重點(diǎn)來(lái)了,我們?cè)撊绾潍@取這張大貼圖中的小貼圖呢?

這里我寫(xiě)了個(gè)小插件,我直接在NGUI源代碼里面改:

找到NGUI的源代碼:UIAtlasMaker

在OnGUI方法里面,我新添加了可以導(dǎo)出貼圖的代碼:

GUILayout.BeginHorizontal();

{

if(tex !=null)

{

if(GUILayout.Button("導(dǎo)出貼圖(PNG)",GUILayout.Width(120f)))

{

stringfilePath = EditorUtility.SaveFolderPanel("保存貼圖到指定文件夾","","");

ExportTexturePNGFromAtlas(filePath, NGUISettings.atlas);

}

}

}

GUILayout.EndHorizontal();

ExportTexturePNGFromAtlas():

staticvoidExportTexturePNGFromAtlas(stringfolderPath,UIAtlas atlas)

{

List exitSpritesList = atlas.spriteList;

Texture2D atlasTexture = NGUIEditorTools.ImportTexture(atlas.texture,true,false, !atlas.premultipliedAlpha);

intoldwith = atlasTexture.width;

intoldHeight = atlasTexture.height;

Color32[] oldPixels =null;

foreach(varesinexitSpritesList)

{

intxmin = Mathf.Clamp(es.x, 0, oldwith);

intymin = Mathf.Clamp(es.y, 0, oldHeight);

intnewWidth = Mathf.Clamp(es.width, 0, oldwith);

intnewHeight = Mathf.Clamp(es.height, 0, oldHeight);

if(newWidth == 0 || newHeight == 0)continue;

if(oldPixels ==null) oldPixels = atlasTexture.GetPixels32();

Color32[] newPixels =newColor32[newWidth * newHeight];

for(inty = 0; y < newHeight; ++y)

{

for(intx = 0; x < newWidth; ++x)

{

intnewIndex = (newHeight - 1 - y) * newWidth + x;

intoldIndex = (oldHeight - 1 - (ymin + y)) * oldwith + (xmin + x);

newPixels[newIndex] = oldPixels[oldIndex];

}

}

Texture2D t =newTexture2D(newWidth, newHeight);

t.SetPixels32(newPixels);

t.Apply();

byte[] bytes = t.EncodeToPNG();

Texture2D.DestroyImmediate(t);

t =null;

if(!Directory.Exists(folderPath))

{

Directory.CreateDirectory(folderPath);

}

using(FileStream fs =newFileStream(folderPath +"/"+ es.name +".png", FileMode.CreateNew))

{

BinaryWriter writer =newBinaryWriter(fs);

writer.Write(bytes);

}

}

}

打開(kāi)NGUI的Atlas Maker:

點(diǎn)擊導(dǎo)出貼圖,然后會(huì)彈出選擇保存貼圖到哪個(gè)文件夾,點(diǎn)擊選擇文件夾之后,小貼圖就導(dǎo)出成功了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一、NGUI 介紹 1.NGUI 插件安裝 首先創(chuàng)建一個(gè)新的 Unity 工程項(xiàng)目,導(dǎo)入 NGUI 插件資源包。 ...
    TonyWan_AR閱讀 10,026評(píng)論 0 8
  • 更新:【面試題含答案】http://bbs.9ria.com/thread-288394-1-1.html 高頻問(wèn)...
    好怕怕閱讀 4,804評(píng)論 3 52
  • 111. [動(dòng)畫(huà)系統(tǒng)]如何將其他類(lèi)型的動(dòng)畫(huà)轉(zhuǎn)換成關(guān)鍵幀動(dòng)畫(huà)? 動(dòng)畫(huà)->點(diǎn)緩存->關(guān)鍵幀 112. [動(dòng)畫(huà)]Unit...
    胤醚貔貅閱讀 13,203評(píng)論 3 89
  • 最近,看了幾篇文章,對(duì)其中的幾句話深有感觸。 一,心小,大城也小;心大,小城也大。 其實(shí),無(wú)所謂大城市,小城市,適...
    hellonewDaisy閱讀 251評(píng)論 0 0
  • 共 5024 字,讀完需 6 分鐘,速讀需 2 分鐘,首發(fā)于知乎專(zhuān)欄前端周刊。寫(xiě)在前面,筆者在做面試官這 2 年多...
    王仕軍閱讀 1,968評(píng)論 2 48