Unity3d通用工具類之NGUI圖集分解

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

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


Unity3d通用工具類之NGUI圖集分解

由于最近需要一些美術(shù)資源嗎,但是無奈自己不會(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)來了,我們?cè)撊绾潍@取這張大貼圖中的小貼圖呢?

這里我寫了個(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);

}

}

}

打開NGUI的Atlas Maker:

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容