在untiy ui的圖片格式選擇問題上我們一般會選擇sprite橡类,但是在遇到大尺寸圖片時sprite會占用非常大的存儲空間,增大包體芽唇,如果你把圖片格式改為texture會發(fā)現(xiàn)圖片大小減少很多顾画,但是Image不能選擇texture資源,這時可以選擇RawImage代替Image披摄,RawImage可以使用texture資源亲雪,但是RawImage使用SetNativeSize時發(fā)現(xiàn)由于untiy會對導(dǎo)入的圖片資源進行處理,如果選擇texture格式會強行將分辨率改為2的次方大小的疚膊,RawImage設(shè)置為初始尺寸后發(fā)現(xiàn)會按照導(dǎo)入后圖片的分辨率來設(shè)置而不是根據(jù)圖片的原始尺寸設(shè)置大小义辕,由于美術(shù)出圖時很少會按照2的次方大小來出圖,所有還需要切到windows中查看圖片分辨率寓盗,再去設(shè)置(寫好長灌砖,手好酸,好難過璧函,不想寫了,代碼很簡單基显,也不需要注釋)蘸吓,身為一個懶比,抵制這種操作撩幽,下面是代碼:
///rawImage拓展方法
//setActiveSize
[MenuItem("CONTEXT/RawImage/SetNativeSize")]
public static void SetRawImageNativeSize(){
GameObject[] gos = Selection.gameObjects;
//Debug.Log ("go name:" + gos [0].name);
System.DateTime startTime = System.DateTime.Now;
for (int i = 0; i < gos.Length; i++) {
GameObject go = gos [i];
Texture t = go.GetComponent<RawImage> ().texture;
if (t == null) {
Debug.LogError ("raw image 未選擇圖片");
return;
} else {
string path = AssetDatabase.GetAssetPath (t);
TextureImporter textureimporter = AssetImporter.GetAtPath (path) as TextureImporter;
TextureImporterType type = textureimporter.textureType;
textureimporter.textureType = TextureImporterType.Sprite;
AssetDatabase.ImportAsset(path);
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite> (path);
Rect rect = sprite.rect;
textureimporter.textureType = type;
AssetDatabase.ImportAsset (path);
(go.transform as RectTransform).sizeDelta = new Vector2(rect.width , rect.height);
}
}
System.DateTime endtime = System.DateTime.Now;
double time = (endtime - startTime).TotalSeconds ;
Debug.Log ("total seconds:" + time.ToString ("f2") + "s");
}