SpriteFromDisk: 2x2 DXT5 placeholder texture fails on newer Unity (sizes must be a multiple of 4)

Aperta Adatta ai principianti
#17 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
game-dev

Direzione di ricerca

Il placeholder viene creato in SpriteLoadRequest_SpriteFromDisk.Prefix in IRBTModUtils/Patches/GifSupportPatches.cs, intorno alla riga 54, dove viene chiamato new Texture2D(2, 2, TextureFormat.DXT5, false) prima di LoadImage. Leggi quel metodo per confermare che il placeholder serve solo al costruttore e che LoadImage lo ridimensiona. Cambia il formato in RGBA32 e verifica che gli sprite PNG e JPG continuino a caricarsi. Il lavoro è completato quando il costruttore non genera più eccezioni su un runtime Unity 6 e i ritratti vengono visualizzati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

This is an issue the affects the recent UCU version of Battletech, because its running on Unity 6, however updating the texture to avoid this issue is a very small change that I hope the maintainers are ok with.

Summary

SpriteLoadRequest_SpriteFromDisk.Prefix loads PNG and JPG sprites into a placeholder texture created as 2×2 DXT5:

https://github.com/BattletechModders/IRBTModUtils/blob/960f789282b08751e777ec2808af544ffc0ecca8/IRBTModUtils/IRBTModUtils/Patches/GifSupportPatches.cs#L54

texture2D = new Texture2D(2, 2, TextureFormat.DXT5, false);
if (!texture2D.LoadImage(numArray))

Unity 2018 (retail BattleTech) accepts this. Newer Unity versions reject compressed textures whose dimensions aren't a multiple of 4. The constructor throws, so every PNG/JPG sprite loaded from disk through this patch fails. The size of the image itself doesn't matter; 512×512 PNGs fail too.

This came up running BattleTech Advanced on BattleTech UCU, a Unity 6 port of the game (downstream report: InternetDadCo/BattleTech-UCU#3). It doesn't affect retail players today, but it will break any mod setup on a newer Unity runtime. The fix is a one-line change and is safe on Unity 2018 too.

Error

Unity [ERROR] Compressed GraphicsFormat RGBA Compressed DXT5|BC3 sRGB requires a texture size that is a multiple of 4
<mod> [WARNING] Unable to load image at: ...\Mods\BT Advanced Pilots\sprite\guiTxrPort_Gigs.png
Data.DataManager [ERROR] UnityEngine.UnityException: Failed to create texture because of invalid parameters.
  at UnityEngine.Texture2D.Internal_Create (...)
  at UnityEngine.Texture2D..ctor (System.Int32 width, System.Int32 height, UnityEngine.TextureFormat textureFormat, System.Boolean mipChain)
  at IRBTModUtils.Patches.SpriteLoadRequest_SpriteFromDisk.Prefix (...)

In one BTA session this happened 45 times, for retail backer portraits and BTA pilot and event portraits. Downstream, the null sprite crashes PilotDef.DownsampleSprite during SGRoomManager.OnSimGameInitialize. The sim-game UI never finishes initialising, so the ship navigation menus stop responding.

Suggested fix

Use an uncompressed placeholder format:

texture2D = new Texture2D(2, 2, TextureFormat.RGBA32, false);

LoadImage resizes the texture to the image's size anyway, so the placeholder's format and size only matter for the constructor. If DXT5 compression is wanted for memory reasons, call texture2D.Compress(true) after LoadImage when width % 4 == 0 && height % 4 == 0.

Verified workaround

As a test, I patched the shipped IRBTModUtils.dll (REL_2.0.0) at the IL level. I changed the ldc.i4.s 12 (DXT5) operand of this constructor call to 4 (RGBA32), which is equivalent to the change above. After the patch, all sprites load, portraits display, and all sim-game menus work normally on the Unity 6 runtime.

Lingua principale
C#
Stelle
7
Fork
4
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di BattletechModders/IRBTModUtils

Tutte le issue di BattletechModders/IRBTModUtils

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.