Fixing sprite atlas metadata errors in Unity / Godot / Phaser — offline, without re-exporting
A sprite atlas is two artifacts that have to agree: the bitmap sheet itself, and the metadata file that tells the engine where each frame sits. When the two drift apart, the symptoms look the same in Unity, Godot, and Phaser: sprites show the wrong region, a strip slides a few pixels sideways, frames clip at the edge, or the editor reports a region it cannot find. The usual reach-for fix is…
A sprite atlas consists of two essential parts: the bitmap sheet and the accompanying metadata file that defines where each frame resides within the sheet. When these two components become misaligned, the consequences are similar across Unity, Godot, and Phaser, manifesting as sprites displaying the incorrect region, strips sliding laterally, frames clipping at the edges, or the editor failing to locate a region.
The typical remedy is to re-export the sheet from the original art project, which entails rebuilding the project, retracing steps through the art tool, and may leave the faulty metadata unchanged. The majority of these issues stem from metadata discrepancies, while the bitmap itself remains unaffected. A meticulous offline edit to the JSON or XML file can rectify these problems.
This guide delves into four recurring fault patterns observed in Unity, Godot, and Phaser exports. For each scenario, it elucidates how to identify the fault using existing data and specifies the precise field to modify, without altering the source art. Prior to making any modifications, duplicate the metadata file and preserve the original bitmap.
Editing metadata is straightforward and reversible, yet an unauthorized overwrite may inadvertently delete frame names referenced by your animations. Open the sheet simultaneously in an image viewer and the JSON or XML file to cross-reference a rectangle with the pixels it encapsulates. Tackle one sheet individually, and after each adjustment, re-run the same verification instead of amalgamating multiple changes before comparing the outcome.
Understanding the structure of atlas metadata A JSON sheet, used by Phaser and numerous Unity sprite-sheet exporters, contains a frames dictionary. Each entry includes x, y, w, and h, representing the rectangle on the sheet, alongside a meta.size block detailing the sheet's width and height. Conversely, an XML sheet, utilized by Godot and older Unity workflows, comprises one node per frame, bearing a name attribute and x, y, w, h rect, plus a reference to the atlas bitmap.
All subsequent checks begin with these fields. Identifying missing keys in JSON frames When the engine signals that a frame is absent or several sprites render blank, despite the sheet appearing complete in an image viewer, the issue likely lies in JSON frames lacking essential keys. Open the JSON file and examine every entry in the frames dictionary.
Each entry must contain numeric x, y, w, and h values. Any missing or non-numeric fields indicate the engine cannot properly position that slice. Additionally, verify that all rectangles remain within the dimensions specified in the meta.size block. Function check_json_atlas(path): data = json.loads(path.read_text(encoding="utf-8")) size = data["meta"]["size"] sheet_w, sheet_h = size["w"], size["h"] problems = [] for name, frame in data["frames"].items(): for key in ("x", "y", "w", "h"): if key not in frame or not isinstance(frame[key], (int, float)): problems.append(f"{name}: missing or non-numeric {key}") x, y, w, h = (frame.get(k, 0) for k in ("x", "y", "w", "h")) if x + w > sheet_w or y + h > sheet_h: problems.append(f"{name}: rect outside sheet {sheet_w}x{sheet_h}") return problems Resolving missing width or height in XML frames In the event of an XML frame name mismatch or atlas index discrepancy, leading to sprites appearing under incorrect names or animations playing the wrong frames, you must first parse the XML and list each frame name alongside its rectangle.
Identify any duplicate names and ensure every name referenced by your animation keys exists within the list. A frequent mistake involves duplicating a rectangle across multiple names, causing the wrong slice to be assigned to each label. To rectify this, rename the frame node's name attribute to match the animation key, retaining the rectangle unchanged unless the region itself is incorrect.
Fixing misaligned frameWidth leading to strip misalignment When every frame after the first shifts horizontally by a constant offset, causing rows to drift apart the lower they sit, and the first frame appears correct, the frameWidth value in the metadata may be erroneous. Comparing the frame width in the metadata against the actual slice width on the bitmap reveals the issue.
Calculate how many columns fit across meta.size.width and verify that each frame's x coordinate aligns with column * frameWidth, and the y coordinate with row * frameHeight. Function grid_mismatches(frames, frame_w, frame_h, sheet_w): bad = [] for name, f in frames.items(): col = round(f["x"] / frame_w) row = round(f["y"] / frame_h) expect_x, expect_y = col * frame_w, row * frame_h if f["x"] != expect_x or f["y"] != expect_y: bad.append((name, f["x"], f["y"], expect_x, expect_y)) columns = sheet_w // frame_w return bad, columns To correct this, adjust frameWidth to accurately reflect the actual slice width, ensuring all frames align correctly within the atlas.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to