### Install SuperTiled2Unity Package Source: https://context7.com/seanba/supertiled2unity/llms.txt Instructions for installing the SuperTiled2Unity package into a Unity project. ```bash # Download from GitHub releases or itch.io # Extract to your Unity project: unzip super-tiled2unity.v2.4.0.zip -d /path/to/YourProject/Packages/ # Result should be: # YourProject/Packages/com.seanba.super-tiled2unity/ ``` -------------------------------- ### Accessing Predefined Object Type Properties Source: https://context7.com/seanba/supertiled2unity/llms.txt Demonstrates how to access properties that are predefined in Tiled's Object Types XML and automatically populated by SuperTiled2Unity. This example retrieves 'enemyHealth' and sets it on an Enemy component. ```csharp // 1. In Tiled: File -> Export Object Types... -> Save as ObjectTypes.xml in your Unity project // 2. In Unity: Edit -> Project Settings -> SuperTiled2Unity // Set "Object Types Xml" field to your exported ObjectTypes.xml file // 3. Click "View Custom Properties" to see imported object types // 4. Access predefined properties in code: using SuperTiled2Unity; using UnityEngine; public class ObjectTypeHandler : MonoBehaviour { void Start() { var props = GetComponent(); // Properties defined in ObjectTypes.xml are automatically // populated on objects of matching types if (props.TryGetCustomProperty("enemyHealth", out var healthProp)) { // This property was predefined in Tiled's Object Types Editor int health = int.Parse(healthProp.m_Value); GetComponent().SetHealth(health); } } } // Note: After modifying ObjectTypes.xml, use the "Reimport Tiled Assets" // button in Project Settings to update all existing map assets ``` -------------------------------- ### Read and Parse Custom Properties Source: https://context7.com/seanba/supertiled2unity/llms.txt Shows how to retrieve custom properties from a SuperCustomProperties component. Includes examples of checking for specific properties, parsing their values (int, bool), removing properties, and iterating through all available properties. ```csharp using SuperTiled2Unity; using UnityEngine; public class PropertyReader : MonoBehaviour { void Start() { // Get custom properties from any GameObject with the component SuperCustomProperties props = GetComponent(); if (props != null) { // Try to get a specific property by name if (props.TryGetCustomProperty("health", out CustomProperty healthProp)) { Debug.Log($"Property Name: {healthProp.m_Name}"); Debug.Log($"Property Type: {healthProp.m_Type}"); Debug.Log($"Property Value: {healthProp.m_Value}"); // Parse the value based on type if (healthProp.m_Type == "int") { int health = int.Parse(healthProp.m_Value); Debug.Log($"Parsed health: {health}"); } } // Check for boolean properties if (props.TryGetCustomProperty("isDestructible", out CustomProperty destructibleProp)) { bool isDestructible = bool.Parse(destructibleProp.m_Value); Debug.Log($"Is Destructible: {isDestructible}"); } // Remove a property after processing props.RemoveCustomProperty("tempData"); // Iterate through all properties foreach (CustomProperty prop in props.m_Properties) { if (!prop.IsEmpty) { Debug.Log($"Custom Property: {prop.m_Name} = {prop.m_Value} ({prop.m_Type})"); } } } } } ``` -------------------------------- ### Auto Custom TMX Importer Example Source: https://context7.com/seanba/supertiled2unity/llms.txt This importer automatically runs on all TMX imports, allowing modification of prefabs during the import process. It adds a BoxCollider2D to objects with the 'Collectible' type. ```csharp using SuperTiled2Unity; using SuperTiled2Unity.Editor; using UnityEngine; // This importer automatically runs on all TMX imports [AutoCustomTmxImporter()] public class MyAutoImporter : CustomTmxImporter { public override void TmxAssetImported(TmxAssetImportedArgs args) { SuperMap map = args.ImportedSuperMap; Debug.Log($"Auto-importing map: {map.name}"); // Process all objects with a specific type foreach (var obj in map.GetComponentsInChildren ()) { if (obj.m_Type == "Collectible") { // Add a custom component to collectibles obj.gameObject.AddComponent().isTrigger = true; } } } } ``` -------------------------------- ### Initialize Game Objects from Tiled Objects Source: https://context7.com/seanba/supertiled2unity/llms.txt Use this script to find and instantiate game objects based on Tiled object layers. It handles different Unity versions for finding objects and processes custom properties for enemy and treasure configurations. ```csharp using SuperTiled2Unity; using UnityEngine; using System.Linq; public class GameInitializer : MonoBehaviour { public GameObject playerPrefab; public GameObject enemyPrefab; public GameObject treasurePrefab; void Start() { // Find all objects from imported Tiled maps #if UNITY_2023_1_OR_NEWER var tiledObjects = FindObjectsByType(FindObjectsSortMode.None); #else var tiledObjects = FindObjectsOfType(); #endif // Spawn player at the designated spawn point var playerSpawn = tiledObjects.FirstOrDefault(o => o.m_TiledName == "PlayerSpawn"); if (playerSpawn != null) { Vector3 spawnPos = playerSpawn.transform.position; // Align to grid if needed spawnPos.x = Mathf.Round(spawnPos.x / 8f) * 8f; spawnPos.y = Mathf.Round(spawnPos.y / 8f) * 8f; Instantiate(playerPrefab, spawnPos, Quaternion.identity); } // Spawn enemies at all enemy spawn points var enemySpawns = tiledObjects.Where(o => o.m_Type == "EnemySpawn"); foreach (var spawn in enemySpawns) { var enemy = Instantiate(enemyPrefab, spawn.transform.position, Quaternion.identity); // Apply properties from Tiled var props = spawn.GetComponent(); if (props != null) { if (props.TryGetCustomProperty("health", out var hp)) enemy.GetComponent().health = int.Parse(hp.m_Value); if (props.TryGetCustomProperty("patrolPath", out var path)) enemy.GetComponent().patrolPathName = path.m_Value; } } // Process treasure chests var treasures = tiledObjects.Where(o => o.m_Type == "Treasure"); foreach (var treasure in treasures) { var chest = Instantiate(treasurePrefab, treasure.transform.position, Quaternion.identity); var props = treasure.GetComponent(); if (props.TryGetCustomProperty("contents", out var contents)) { chest.GetComponent().SetContents(contents.m_Value); } } } } // Placeholder components public class Enemy : MonoBehaviour { public int health; public string patrolPathName; } public class TreasureChest : MonoBehaviour { public void SetContents(string c) { } } ``` -------------------------------- ### Find SuperObjects by Name and Type Source: https://context7.com/seanba/supertiled2unity/llms.txt Demonstrates finding all SuperObjects in a scene, locating a specific object by name (e.g., 'Spawn'), and filtering objects by type (e.g., 'Enemy'). Includes accessing object properties and associated tile data. ```csharp using SuperTiled2Unity; using UnityEngine; using System.Linq; public class ObjectFinder : MonoBehaviour { void Start() { // Find all SuperObjects in the scene SuperObject[] allObjects = FindObjectsOfType(); // Find a specific spawn point by name SuperObject spawnPoint = allObjects.FirstOrDefault(obj => obj.m_TiledName == "Spawn"); if (spawnPoint != null) { Debug.Log($"Found spawn point at: {spawnPoint.transform.position}"); Debug.Log($" ID: {spawnPoint.m_Id}"); Debug.Log($" Type: {spawnPoint.m_Type}"); Debug.Log($" Size: {spawnPoint.m_Width} x {spawnPoint.m_Height}"); Debug.Log($" Rotation: {spawnPoint.m_Rotation}"); Debug.Log($" Visible: {spawnPoint.m_Visible}"); // If this object has an associated tile if (spawnPoint.m_SuperTile != null) { Debug.Log($" Tile ID: {spawnPoint.m_TileId}"); Debug.Log($" Tile Type: {spawnPoint.m_SuperTile.m_Type}"); } // Calculate the object's color considering layer hierarchy Color objectColor = spawnPoint.CalculateColor(); Debug.Log($" Calculated Color: {objectColor}"); } // Find all objects of a specific type var enemies = allObjects.Where(obj => obj.m_Type == "Enemy"); foreach (var enemy in enemies) { Debug.Log($"Enemy '{enemy.m_TiledName}' at ({enemy.m_X}, {enemy.m_Y})"); } } } ``` -------------------------------- ### Iterate Through SuperLayer Components Source: https://context7.com/seanba/supertiled2unity/llms.txt Shows how to retrieve all SuperLayer components within a map and log their properties, such as name, offset, parallax, opacity, visibility, and tint color. It also demonstrates calculating combined color and opacity. ```csharp using SuperTiled2Unity; using UnityEngine; public class LayerManager : MonoBehaviour { void Start() { SuperMap map = GetComponent(); // Get all layers in the map SuperLayer[] layers = map.GetComponentsInChildren(); foreach (SuperLayer layer in layers) { Debug.Log($"Layer: {layer.m_TiledName}"); Debug.Log($" Offset: ({layer.m_OffsetX}, {layer.m_OffsetY})"); Debug.Log($" Parallax: ({layer.m_ParallaxX}, {layer.m_ParallaxY})"); Debug.Log($" Opacity: {layer.m_Opacity}"); Debug.Log($" Visible: {layer.m_Visible}"); Debug.Log($" Tint Color: {layer.m_TintColor}"); // Calculate combined color considering parent layers Color combinedColor = layer.CalculateColor(); float combinedOpacity = layer.CalculateOpacity(); Debug.Log($" Combined Color: {combinedColor}"); Debug.Log($" Combined Opacity: {combinedOpacity}"); } } } ``` -------------------------------- ### Implement a Custom TMX Importer in C# Source: https://github.com/seanba/supertiled2unity/blob/master/docs/manual/extending-the-importer.md Use the AutoCustomTmxImporterAttribute to ensure your custom importer is always applied. Modify the imported prefab within the TmxAssetImported method. Ensure modifications are deterministic. ```csharp // 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); } } ``` -------------------------------- ### Placeholder Components for Importers Source: https://context7.com/seanba/supertiled2unity/llms.txt Placeholder MonoBehaviour components used by custom importers to demonstrate functionality. ParallaxLayer stores parallax speed, and EnemySpawner stores enemy type and spawn rate. ```csharp // Example placeholder components public class ParallaxLayer : MonoBehaviour { public float speed; } public class EnemySpawner : MonoBehaviour { public string enemyType; public float spawnRate; } ``` -------------------------------- ### Access SuperMap Component Properties Source: https://context7.com/seanba/supertiled2unity/llms.txt Demonstrates how to access and log properties of the SuperMap component attached to an imported Tiled map prefab. This includes map dimensions, tile size, orientation, and conversion of tile indices to grid cells. ```csharp using SuperTiled2Unity; using UnityEngine; public class MapInspector : MonoBehaviour { void Start() { // Get the SuperMap component from an imported map prefab SuperMap map = GetComponent(); // Access map properties Debug.Log($"Map: {map.name}"); Debug.Log($"Dimensions: {map.m_Width} x {map.m_Height} tiles"); Debug.Log($"Tile Size: {map.m_TileWidth} x {map.m_TileHeight} pixels"); Debug.Log($"Orientation: {map.m_Orientation}"); Debug.Log($"Render Order: {map.m_RenderOrder}"); Debug.Log($"Is Infinite: {map.m_Infinite}"); Debug.Log($"Background Color: {map.m_BackgroundColor}"); // Convert Tiled tile index to Unity grid cell position int tileIndex = 5; Vector3Int gridCell = map.TiledIndexToGridCell(tileIndex, 0, 0, map.m_Width); Debug.Log($"Tile index {tileIndex} maps to grid cell: {gridCell}"); } } ``` -------------------------------- ### Set Custom Transparency Sort Axis via Script Source: https://github.com/seanba/supertiled2unity/blob/master/docs/manual/sorting.md Use this C# code to dynamically set the camera's transparency sort mode and axis at runtime. Ensure the main camera is tagged appropriately. ```csharp var camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); camera.transparencySortMode = TransparencySortMode.CustomAxis; camera.transparencySortAxis = Vector3.up; ``` -------------------------------- ### Configuring Camera for Custom Axis Sorting Source: https://context7.com/seanba/supertiled2unity/llms.txt This C# script configures the main camera in Unity to use custom axis sorting based on the object's y-position for dynamic depth sorting, suitable for overhead games. It also notes the global settings available in Project Settings. ```csharp using UnityEngine; public class OverheadCameraSetup : MonoBehaviour { void Start() { // Configure the camera for custom axis sorting // This sorts sprites by y-position (higher y = rendered behind) Camera camera = Camera.main; camera.transparencySortMode = TransparencySortMode.CustomAxis; camera.transparencySortAxis = Vector3.up; // Note: You can also set these globally in: // Edit -> Project Settings -> Graphics -> Camera Settings // - Transparency Sort Mode: Custom Axis // - Transparency Sort Axis: (0, 1, 0) } } // In Tiled, ensure sprites and tiles that need dynamic sorting // share the same Sorting Layer and Order in Layer values // using unity:SortingLayer and unity:SortingOrder custom properties ``` -------------------------------- ### Accessing SuperTileset and SuperTile Data Source: https://context7.com/seanba/supertiled2unity/llms.txt This C# script demonstrates how to access and log various properties of a SuperTileset ScriptableObject and its individual SuperTile components, including dimensions, counts, sprites, collision data, and custom properties. Ensure a SuperTileset is assigned to the 'tileset' field in the Inspector. ```csharp using SuperTiled2Unity; using UnityEngine; using UnityEngine.Tilemaps; public class TilesetInspector : MonoBehaviour { public SuperTileset tileset; void Start() { if (tileset != null) { Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp Debug.Log($ ``` ```csharp // Get a specific tile by ID if (tileset.TryGetTile(5, out SuperTile tile)) { Debug.Log($"Tile 5 found:"); Debug.Log($" Type: {tile.m_Type}"); Debug.Log($" Size: {tile.m_Width} x {tile.m_Height}"); Debug.Log($" Sprite: {tile.m_Sprite?.name}"); Debug.Log($" Collider Type: {tile.m_ColliderType}"); Debug.Log($" Has Animation: {tile.m_AnimationSprites?.Length > 1}"); // Access tile custom properties foreach (var prop in tile.m_CustomProperties) { Debug.Log($" Property: {prop.m_Name} = {prop.m_Value}"); } // Access collision objects defined on the tile foreach (var collision in tile.m_CollisionObjects) { Debug.Log($" Collision: {collision.m_ShapeType}"); } } } } } ``` -------------------------------- ### Named Custom TMX Importer for Enemy Spawners Source: https://context7.com/seanba/supertiled2unity/llms.txt A selectable custom importer that adds an EnemySpawner component to objects with the 'EnemySpawner' type, populating its properties from custom Tiled properties. ```csharp // Named custom importer (selectable from dropdown in Unity inspector) [System.ComponentModel.DisplayName("Enemy Spawner Importer")] public class EnemySpawnerImporter : CustomTmxImporter { public override void TmxAssetImported(TmxAssetImportedArgs args) { var map = args.ImportedSuperMap; foreach (var obj in map.GetComponentsInChildren ()) { if (obj.m_Type == "EnemySpawner") { var props = obj.GetComponent(); var spawner = obj.gameObject.AddComponent(); if (props.TryGetCustomProperty("enemyType", out var typeProp)) { spawner.enemyType = typeProp.m_Value; } if (props.TryGetCustomProperty("spawnRate", out var rateProp)) { spawner.spawnRate = float.Parse(rateProp.m_Value); } } } } } ``` -------------------------------- ### Tiled Custom Import Properties for Unity Source: https://context7.com/seanba/supertiled2unity/llms.txt This XML snippet shows how to define custom properties within Tiled to control SuperTiled2Unity's import behavior. These properties can be applied to layers, tilesets, tiles, or objects to modify how they are translated into Unity assets and GameObjects. ```xml ``` -------------------------------- ### Ordered Auto Custom TMX Importer Source: https://context7.com/seanba/supertiled2unity/llms.txt This importer runs after default importers due to a higher order value. It processes layers and adds a ParallaxLayer component based on a 'parallaxSpeed' custom property. ```csharp // Ordered auto importer - runs after default (order 0) importers [AutoCustomTmxImporter(1)] public class MySecondaryImporter : CustomTmxImporter { public override void TmxAssetImported(TmxAssetImportedArgs args) { // This runs after MyAutoImporter due to higher order value var map = args.ImportedSuperMap; // Find and process layers foreach (var layer in map.GetComponentsInChildren ()) { var props = layer.GetComponent(); if (props != null && props.TryGetCustomProperty("parallaxSpeed", out var prop)) { float speed = float.Parse(prop.m_Value); // Add parallax behavior component var parallax = layer.gameObject.AddComponent(); parallax.speed = speed; } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.