1. 程式人生 > 其它 >2d圖片匯入後像素變大(變小)了 imported image is smaller than in explorer

2d圖片匯入後像素變大(變小)了 imported image is smaller than in explorer

技術標籤:javaopencvpythonunityvue

現象

使用Sprite.Create函式通過讀取圖片來建立Sprite

  • Editor:顯示正常
  Texture2D texture = Resources.Load<Texture2D>("Textures" + Path.DirectorySeparatorChar + SpritePath);
  sprite = Sprite.Create(texture, Rect.MinMaxRect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  • 打包PC:圖片比原尺寸(100 * 100畫素)大一些

原因

  • 建立專案時,template選擇2D,當你匯入任何png圖片時,在圖片匯入設定裡Texture Type會預設設定成Sprite(2D and UI),Unity官方原文:In 2D Project mode: Any images you import are assumed to be 2D images (Sprites ) and set to Sprite mode.

Sprite.png

  • 其他非2D選項時,則會預設設定為Default,且Non Power of 2為ToNearest Texture Type設定為Default,且Non Power of 2為ToNearest時,100 * 100畫素的圖片實際上是128 * 128畫素

Default.png


但是在資源瀏覽器裡檢視圖片檔案屬性時是100 * 100畫素的。 imageProperty.png

  • 官方對Non Power of 2(NPOT)的解釋如下:
Non Power of 2If the Texture has a non-power of two (NPOT) dimension size, this defines a scaling behavior at import time. See documentation on Importing Textures for more information on non-power of two sizes. This is set to None by default.
NoneTexture dimension size stays the same.
To nearestThe Texture is scaled to the nearest power-of-two dimension size at import time. For example, a 257x511 px Texture is scaled to 256x512 px. Note that PVRTC formats require Textures to be square (that is width equal to height), so the final dimension size is upscaled to 512x512 px.
To largerThe Texture is scaled to the power-of-two dimension size of the largest dimension size value at import time. For example, a 257x511 px Texture is scaled to 512x512 px.
To smallerThe Texture is scaled to the power-of-two dimension size of the smallest dimension size value at import time. For example, a 257x511 px Texture is scaled to 256x256 px.

設定為ToNearest時,100 * 100畫素的圖片實際上會被縮放到最接近2的冪的尺寸,也就是128 * 128

解決方法

  • 將Texture Type改為Sprite(2D and UI),建議所有用於Sprite用途的圖片都選擇此項,體積更小
  • Texture Type保持為Default,將Non Power of 2設定為None(不建議,因為體積更大,即使在相同Compression Quality下)

參考: