2016-03-01 80 views
8

令人驚訝的團結,多年來唯一方式簡單規模實際PNG是使用非常真棒庫http://wiki.unity3d.com/index.php/TextureScale在Unity5中縮放PNG? - Bountie

實例下

如何擴展使用Unity5功能的PNG ?現在必須有新的UI等方式。

因此,縮放實際像素(如在Color[])或字面上是一個PNG文件,也許從網上下載。

(順便說一句,如果你是新來統一組織,Resize通話無關,它只是改變數組的大小。)

public WebCamTexture wct; 

public void UseFamousLibraryToScale() 
    { 
    // take the photo. scale down to 256 
    // also crop to a central-square 

    WebCamTexture wct; 
    int oldW = wct.width; // NOTE example code assumes wider than high 
    int oldH = wct.height; 

    Texture2D photo = new Texture2D(oldW, oldH, 
      TextureFormat.ARGB32, false); 
    //consider WaitForEndOfFrame() before GetPixels 
    photo.SetPixels(0,0,oldW,oldH, wct.GetPixels()); 
    photo.Apply(); 

    int newH = 256; 
    int newW = Mathf.FloorToInt(
      ((float)newH/(float)oldH) * oldW); 

    // use a famous Unity library to scale 
    TextureScale.Bilinear(photo, newW,newH); 

    // crop to central square 256.256 
    int startAcross = (newW - 256)/2; 
    Color[] pix = photo.GetPixels(startAcross,0, 256,256); 
    photo = new Texture2D(256,256, TextureFormat.ARGB32, false); 
    photo.SetPixels(pix); 
    photo.Apply(); 
    demoImage.texture = photo; 

    // consider WriteAllBytes(
    // Application.persistentDataPath+"p.png", 
    // photo.EncodeToPNG()); etc 
    } 

只是順便說一句我突然想起我可能只是說說在這裏縮小比例(因爲您經常需要做一些事情來發布圖片,創建動態或其他任何內容)。我想,通常不需要放大圖片的大小;這是毫無意義的質量明智的。

賞金來了!

+2

目前仍沒有在推廣使用Unity功能的PNG的方式建造,Wiki上的代碼的最佳方式,但它可能是可能的整合魔鬼或FreeImage的做縮放或操縱如果更高級的東西是需要的。 – Chris

回答

4

如果您可以使用拉伸縮放,實際上通過使用臨時RenderTexture和Graphics.Blit可以實現更簡單的方式。如果你需要它成爲Texture2D,暫時交換RenderTexture.active並將其像素讀入Texture2D應該能夠實現。例如:

public Texture2D ScaleTexture(Texture src, int width, int height){ 
    RenderTexture rt = RenderTexture.GetTemporary(width, height); 
    Graphics.Blit(src, rt); 

    RenderTexture currentActiveRT = RenderTexture.active; 
    RenderTexture.active = rt; 
    Texture2D tex = new Texture2D(rt.width,rt.height); 

    tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); 
    tex.Apply(); 

    RenderTexture.ReleaseTemporary(rt); 
    RenderTexture.active = currentActiveRT; 

    return tex; 
} 
+0

老問題重新喚醒呃!迷人的我從來沒有想過。我不知道質量是什麼樣子?我會放棄它。 – Fattie

+0

哦沒有看到日期:p – dkrprasetya

+0

不用擔心!我實際上添加了一個賞金 - 也許你會贏得它默認情況下:) – Fattie