SuperTiled2Unity Documentation

repository·master·Indexed 20 days ago

https://github.com/seanba/supertiled2unity

A Unity package (version 2.4.0) that enables seamless integration between the Tiled Map Editor and Unity. It utilizes Unity's Scripted Importers to automatically sync map changes without manual export. Key features include support for custom properties via the 'unity:' prefix, prefab replacements, custom importers via CustomTmxImporter, and flexible sorting modes (Stacked and Custom Sort Axis) to manage rendering order.

Tokens
2.8K
Snippets
5
Records
14
Agent score
72%

What's inside SuperTiled2Unity

  1. Overview of SuperTiled2Unity

    master

    SuperTiled2Unity is a Unity Package designed to import maps from the Tiled Map Editor directly into Unity projects.

    Key features include:

    • Scripted Importers: Uses Unity's Scripted Importer system (available in Unity 2018 and later) to keep the import process contained within the Unity project across all platforms.
    • Automatic Updates: Changes made to maps in Tiled are automatically reflected in Unity upon saving the map file. There is no manual export step required.
  2. Handle Tile Objects and Sorting

    master

    Tiled 'object layers' containing tile objects are imported as individual sprites rather than part of a Tilemap.

    • In Stacked mode: These sprites are assigned a sort order based on their position in the layer. Because this order depends on the number of objects, it can make predicting the order of subsequent layers difficult. To fix this, use the unity:SortingLayer property on the layer immediately following the TileObjects layer to reset the sorting context.
    • In Custom Sort Axis mode: Imported sprites are not assigned incremented sorting orders; instead, they are sorted by their position on the custom axis (e.g., Y-position).
  3. Choose a Sorting Mode in SuperTiled2Unity

    master

    When importing a map, SuperTiled2Unity provides two primary sorting modes to determine how layers and objects are rendered in Unity:

    • Stacked: The default mode. It matches the rendering order of layers and objects exactly as they appear in your Tiled Map Editor file. This is ideal for side-scroller games where the rendering order of objects relative to the map is static.
    • Custom Sort Axis: Used for overhead-style games where objects (like a player) need to dynamically move in front of or behind tiles based on their position (e.g., their Y-position).
  4. Create a Custom Importer to transform prefabs

    master

    Custom Importers provide a programmatic way to completely transform the generated prefab during the import process. This is the most powerful extension method but requires writing C# code.

    To implement a custom importer, inherit from CustomTmxImporter and override the TmxAssetImported method.

    Important Requirements:

    • Determinism: Any modifications made to the GameObjects or components must be deterministic; the same prefab must be produced every time the import runs.
    • Accessing the Map: Use args.ImportedSuperMap to access the root of the imported prefab.
    • Application: You can use the [AutoCustomTmxImporter] attribute to force the importer to always be applied, or omit it to select the importer from a drop-down list in the Unity Inspector.
      // The AutoCustomTmxImporterAttribute will force this importer to always be applied.
      // Leave this attribute off if you want to choose a custom importer from a drop-down list instead.
      [AutoCustomTmxImporter()]
       public class MyTmxImporter : CustomTmxImporter
       {
           public override void TmxAssetImported(TmxAssetImportedArgs args)
           {
               // Note: args.ImportedSuperMap is the root of the imported prefab
               // You can modify the gameobjects and components any way you wish here
               // Howerver, the results must be deterministic (i.e. the same prefab is created each time)
               var map = args.ImportedSuperMap;
               Debug.LogFormat("Map '{0}' has been imported.", map.name);
           }
       }
  5. Install SuperTiled2Unity

    master

    SuperTiled2Unity is a collection of Unity scripts designed to import files from the Tiled Map Editor into Unity projects. It is distributed as a Unity Package and can be downloaded from itch.io.

    Download via: https://seanba.itch.io/supertiled2unity
  6. Support Tiled Object Types in SuperTiled2Unity

    master

    Tiled's predefined properties (defined via the Object Types Editor) are not recognized by SuperTiled2Unity by default. To enable support for these predefined properties, you must export the Object Types XML file from Tiled and link it to your Unity project settings.

    1. Export Object Types from Tiled

    1. In Tiled, go to the File menu.
    2. Select Export Object Types....
    3. Save the resulting XML file inside your Unity project's Assets directory.

    2. Configure SuperTiled2Unity Project Settings

    1. In Unity, go to Edit -> Project Settings....
    2. Locate the SuperTiled2Unity settings section.
    3. Find the Object Types Xml field.
    4. Drag and drop your exported XML file into this field (or use the asset selector).

    3. Verify Imported Properties

    1. Click the View Custom Properties button located just below the XML field in the settings window.
    2. This opens the Custom Object Types Properties window, which lists all imported custom object types, their properties, and their custom colors.

    Once configured, any updated map assets in your project will include these predefined properties in their SuperCustomProperties instances.

  7. Assign Unity Sorting Layers to Tiled Layers

    master

    By default, SuperTiled2Unity assigns all tile layers to Unity's Default sorting layer with increasing Order in Layer values. To use custom Unity Sorting Layers, add a custom property to your layer in Tiled:

    1. In Tiled, select the layer.
    2. Add a custom property with the name unity:SortingLayer.
    3. Set the value to the exact name of the Sorting Layer you created in Unity's Tag Manager.

    This allows you to group tiles and sprites into shared sorting layers (e.g., a 'Sky' layer or 'Background' layer) to manage complex rendering orders more explicitly.

  8. Implement Dynamic Sorting with a Custom Sort Axis

    master

    To achieve an overhead view where sprites dynamically sort against tiles (e.g., a player moving behind a column), follow these three steps:

    1. Import the map using the Custom Sort Axis sorting mode in the SuperTiled2Unity importer.
    2. Configure Unity Project Settings: Set the Transparency Sort Mode to Custom Axis and set the Transparency Sort Axis to an axis with increasing values (typically Vector3.up for Y-axis sorting).
    3. Match Sorting Properties: Ensure that both your Unity sprites and your Tiled tiles are assigned the same Sorting Layer and have the same Order in Layer values. If these do not match, the dynamic sorting will not work correctly.
    // Example: Setting the transparency sort axis via script
    var camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    camera.transparencySortMode = TransparencySortMode.CustomAxis;
    camera.transparencySortAxis = Vector3.up;
  9. Modify imported maps using Prefab Replacements

    master

    Prefab Replacements are the simplest method for modifying imported Tiled maps. They work by replacing a Tiled Object in your map with a Prefab Instance from your Unity project.

    Key Features:

    • Spawners: Ideal for creating object spawners.
    • Property Mapping: Any custom properties defined on your Tiled Object that match the name of a field in a component on your target prefab will be automatically applied to that prefab.