/// <summary> /// Creates a MapObject from node /// </summary> /// <param name="node">NanoXMLNode XML to parse</param> /// <param name="parentObjectLayer">This MapObject's MapObjectLayer parent</param> public MapObject(NanoXMLNode node, MapObjectLayer parentObjectLayer) : base(node) { if (node.GetAttribute("name") != null) { Name = node.GetAttribute("name").Value; } else { Name = "Object"; } if (node.GetAttribute("type") != null) { Type = node.GetAttribute("type").Value; } else Type = string.Empty; if (node.GetAttribute("visible") != null) { Visible = int.Parse(node.GetAttribute("visible").Value, CultureInfo.InvariantCulture) == 1; } else Visible = true; if (node.GetAttribute("gid") != null) GID = int.Parse(node.GetAttribute("gid").Value, CultureInfo.InvariantCulture); else GID = 0; ParentObjectLayer = parentObjectLayer; }
public void GenerateColliders() { if (GenerateTileCollisions) { tiledMap.GenerateTileCollisions(TileCollisionsIs2D, TileCollisionsIsTrigger, TileCollisionsZDepth, TileCollisionsWidth, TileCollisionsIsInner); } for (int i = 0; i < generateCollider.Length; i++) { if (generateCollider[i]) { MapObjectLayer collisionLayer = (MapObjectLayer)tiledMap.GetLayer(objectLayers[i]); if (collisionLayer != null) { List <MapObject> colliders = collisionLayer.Objects; foreach (MapObject colliderObjMap in colliders) { GameObject newColliderObject = null; if (colliderObjMap.Type.Equals(Map.Object_Type_NoCollider) == false) { newColliderObject = tiledMap.GenerateCollider(colliderObjMap, collidersIs2D[i], collidersIsTrigger[i], collidersZDepth[i], collidersWidth[i], collidersIsInner[i]); } tiledMap.AddPrefabs(colliderObjMap, newColliderObject, collidersIs2D[i], addTileNameToColliderName); } } else { Debug.LogError("There's no Layer \"" + objectLayers[i] + "\" in tile map."); } } } }
/// <summary> /// ParseMapXML the Window /// </summary> /// <param name="objectLayerNode">NanoXMLNode of the MapObjectLayer from with MapObject will be read</param> public static void Init(MapObjectLayer objectLayer) { // Get existing open window or if none, make a new one: TiledMapObjectsWindow window = (TiledMapObjectsWindow)EditorWindow.GetWindow(typeof(TiledMapObjectsWindow)); _objectLayer = objectLayer; window.title = "Map Object Layer"; window.RebuildObjectsProperties(); }
/// <summary> /// Creates a new MapObject. /// </summary> /// <param name="name">The name of the object.</param> /// <param name="type">The type of object to create.</param> /// <param name="bounds">The initial bounds of the object.</param> /// <param name="properties">The initial property collection or null to create an empty property collection.</param> /// <param name="rotation">This object's rotation</param> /// <param name="gid">Object's ID</param> /// <param name="parentObjectLayer">This MapObject's MapObjectLayer parent</param> /// <param name="points">This MapObject's Point list</param> public MapObject(string name, string type, Rect bounds, PropertyCollection properties, int gid, List<Vector2> points, float rotation, MapObjectLayer parentObjectLayer) : base(ObjectType.Box, bounds, rotation, points, properties) { if (string.IsNullOrEmpty(name)) throw new ArgumentException(null, "name"); Name = name; Type = type; Bounds = bounds; GID = gid; Points = points; Visible = true; Rotation = rotation; ParentObjectLayer = parentObjectLayer; }
/// <summary> /// Creates a MapObject from node /// </summary> /// <param name="node">NanoXMLNode XML to parse</param> /// <param name="parentObjectLayer">This MapObject's MapObjectLayer parent</param> public MapObject(NanoXMLNode node, MapObjectLayer parentObjectLayer) : base(node) { if (node.GetAttribute("name") != null) { Name = node.GetAttribute("name").Value; } else { Name = "Object"; } if (node.GetAttribute("type") != null) { Type = node.GetAttribute("type").Value; } else { Type = string.Empty; } if (node.GetAttribute("visible") != null) { Visible = int.Parse(node.GetAttribute("visible").Value, CultureInfo.InvariantCulture) == 1; } else { Visible = true; } if (node.GetAttribute("gid") != null) { GID = int.Parse(node.GetAttribute("gid").Value, CultureInfo.InvariantCulture); } else { GID = 0; } ParentObjectLayer = parentObjectLayer; NanoXMLNode propertiesNode = node["properties"]; if (propertiesNode != null) { Properties = new PropertyCollection(propertiesNode); } }
/// <summary> /// Returns the set of all objects in the map. /// </summary> /// <returns>A new set of all objects in the map.</returns> public IEnumerable <MapObject> GetAllObjects() { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { yield return(obj); } } }
/// <summary> /// Creates a new MapObject. /// </summary> /// <param name="name">The name of the object.</param> /// <param name="type">The type of object to create.</param> /// <param name="bounds">The initial bounds of the object.</param> /// <param name="properties">The initial property collection or null to create an empty property collection.</param> /// <param name="rotation">This object's rotation</param> /// <param name="gid">Object's ID</param> /// <param name="parentObjectLayer">This MapObject's MapObjectLayer parent</param> /// <param name="points">This MapObject's Point list</param> public MapObject(string name, string type, Rect bounds, PropertyCollection properties, int gid, List <Vector2> points, float rotation, MapObjectLayer parentObjectLayer) : base(ObjectType.Box, bounds, rotation, points) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException(null, "name"); } Name = name; Type = type; Bounds = bounds; Properties = properties ?? new PropertyCollection(); GID = gid; Points = points; Visible = true; Rotation = rotation; ParentObjectLayer = parentObjectLayer; }
/// <summary> /// Finds a collection of objects in the map using a delegate. /// </summary> /// <remarks> /// This method performs basically the same process as FindObject, but instead /// of returning the first object for which the delegate returns true, it returns /// a collection of all objects for which the delegate returns true. /// </remarks> /// <param name="finder">The delegate used to search for the object.</param> /// <returns>A collection of all MapObjects for which the delegate returned true.</returns> public IEnumerable <MapObject> FindObjects(MapObjectFinder finder) { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { if (finder(objLayer, obj)) { yield return(obj); } } } }
/// <summary> /// Finds an object in the map using a delegate. /// </summary> /// <remarks> /// This method is used when an object is desired, but there is no specific /// layer to find the object on. The delegate allows the caller to create /// any logic they want for finding the object. A simple example for finding /// the first object named "goal" in any layer would be this: /// /// var goal = map.FindObject((layer, obj) => return obj.Name.Equals("goal")); /// /// You could also use the layer name or any other logic to find an object. /// The first object for which the delegate returns true is the object returned /// to the caller. If the delegate never returns true, the method returns null. /// </remarks> /// <param name="finder">The delegate used to search for the object.</param> /// <returns>The MapObject if the delegate returned true, null otherwise.</returns> public MapObject FindObject(MapObjectFinder finder) { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { if (finder(objLayer, obj)) { return(obj); } } } return(null); }
private void Initialize(XDocument document, bool makeUnique, string fullPath, string mapPath, Material baseTileMaterial, int sortingOrder) { XElement mapNode = document.Root; Version = mapNode.Attribute("version").Value; Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attribute("orientation").Value, true); Width = int.Parse(mapNode.Attribute("width").Value, CultureInfo.InvariantCulture); Height = int.Parse(mapNode.Attribute("height").Value, CultureInfo.InvariantCulture); TileWidth = int.Parse(mapNode.Attribute("tilewidth").Value, CultureInfo.InvariantCulture); TileHeight = int.Parse(mapNode.Attribute("tileheight").Value, CultureInfo.InvariantCulture); if (_mapName == null) _mapName = "Map"; if (!mapPath.EndsWith("/")) mapPath = mapPath + "/"; MapObject = new GameObject(_mapName); MapObject.transform.parent = Parent.transform; MapObject.transform.localPosition = Vector3.zero; DefaultSortingOrder = sortingOrder; XElement propertiesElement = mapNode.Element("properties"); if (propertiesElement != null) Properties = new PropertyCollection(propertiesElement); TileSets = new List<TileSet>(); Tiles = new Dictionary<int, Tile>(); foreach (XElement tileSet in mapNode.Descendants("tileset")) { if (tileSet.Attribute("source") != null) { TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attribute("source").Value)); XDocument externalTileSet = XDocument.Parse(externalTileSetTextAsset.text); XElement externalTileSetNode = externalTileSet.Element("tileset"); TileSet t = new TileSet(externalTileSetNode, mapPath); TileSets.Add(t); foreach (KeyValuePair<int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } } else { TileSet t = new TileSet(tileSet, mapPath); TileSets.Add(t); foreach (KeyValuePair<int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } } } // Generate Materials for Map batching List<Material> materials = new List<Material>(); // Generate Materials int i = 0; for (i = 0; i < TileSets.Count; i++) { Material layerMat = new Material(baseTileMaterial); layerMat.mainTexture = TileSets[i].Texture; materials.Add(layerMat); } Layers = new List<Layer>(); i = 0; foreach (XElement layerNode in mapNode.Elements("layer").Concat( mapNode.Elements("objectgroup").Concat( mapNode.Elements("imagelayer"))) ) { Layer layerContent; int layerDepth = 1 - (LayerDepthSpacing * i); if (layerNode.Name == "layer") { layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials); } else if (layerNode.Name == "objectgroup") { layerContent = new MapObjectLayer(layerNode, this, layerDepth, materials); } else if (layerNode.Name == "imagelayer") { layerContent = new ImageLayer(layerNode, this, mapPath, baseTileMaterial); } else { throw new Exception("Unknown layer name: " + layerNode.Name); } // Layer names need to be unique for our lookup system, but Tiled // doesn't require unique names. string layerName = layerContent.Name; int duplicateCount = 2; // if a layer already has the same name... if (Layers.Find(l => l.Name == layerName) != null) { // figure out a layer name that does work do { layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount); duplicateCount++; } while (Layers.Find(l => l.Name == layerName) != null); // log a warning for the user to see Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name."); // save that name layerContent.Name = layerName; } layerContent.LayerDepth = layerDepth; Layers.Add(layerContent); namedLayers.Add(layerName, layerContent); i++; } }
void ContinueLoadingTiledMapAfterTileSetsLoaded() { // Generate Materials for Map batching List<Material> materials = new List<Material>(); // Generate Materials int i = 0; for (i = 0; i < TileSets.Count; i++) { Material layerMat = new Material(BaseTileMaterial); layerMat.mainTexture = TileSets[i].Texture; materials.Add(layerMat); } Layers = new List<Layer>(); i = 0; int tileLayerCount = 0; foreach (NanoXMLNode layerNode in MapNode.SubNodes) { if (!(layerNode.Name.Equals("layer") || layerNode.Name.Equals("objectgroup") || layerNode.Name.Equals("imagelayer"))) continue; Layer layerContent; int layerDepth = 1 - (LayerDepthSpacing * i); if (layerNode.Name.Equals("layer")) { bool makeUnique = GlobalMakeUniqueTiles; if (PerLayerMakeUniqueTiles != null && tileLayerCount < PerLayerMakeUniqueTiles.Length) makeUnique = PerLayerMakeUniqueTiles[tileLayerCount]; layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials); tileLayerCount++; } else if (layerNode.Name.Equals("objectgroup")) { layerContent = new MapObjectLayer(layerNode, this, layerDepth, materials); } else if (layerNode.Name.Equals("imagelayer")) { layerContent = new ImageLayer(layerNode, this, _mapPath, BaseTileMaterial); } else { throw new Exception("Unknown layer name: " + layerNode.Name); } // Layer names need to be unique for our lookup system, but Tiled // doesn't require unique names. string layerName = layerContent.Name; int duplicateCount = 2; // if a layer already has the same name... if (Layers.Find(l => l.Name == layerName) != null) { // figure out a layer name that does work do { layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount); duplicateCount++; } while (Layers.Find(l => l.Name == layerName) != null); // log a warning for the user to see Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name."); // save that name layerContent.Name = layerName; } layerContent.LayerDepth = layerDepth; Layers.Add(layerContent); namedLayers.Add(layerName, layerContent); i++; } if (OnMapFinishedLoading != null) OnMapFinishedLoading(this); }
public static GameObject[] GenerateColliders3DFromLayer(this Map map, MapObjectLayer objectLayer, bool collidersAreTrigger = false, string tag = "Untagged", int physicsLayer = 0, PhysicMaterial physicsMaterial = null, float collidersZDepth = 0, float collidersWidth = 1, bool collidersAreInner = false) { if (objectLayer != null) { List<GameObject> generatedGameObjects = new List<GameObject>(); List<MapObject> colliders = objectLayer.Objects; foreach (MapObject colliderObjMap in colliders) { // This function should not try to generate a collider where a prefab will be generated! if (colliderObjMap.HasProperty(Map.Property_PrefabName)) continue; // Also, do not generate collider for a TileObject if (colliderObjMap.GID > 0) continue; GameObject newColliderObject = null; if (colliderObjMap.Type.Equals(Map.Object_Type_NoCollider) == false) { newColliderObject = map.GenerateCollider3D(colliderObjMap, collidersAreTrigger, tag, physicsLayer, physicsMaterial, collidersZDepth, collidersWidth, collidersAreInner); } if (newColliderObject) generatedGameObjects.Add(newColliderObject); } return generatedGameObjects.ToArray(); } return null; }
public static GameObject[] GeneratePrefabsFromLayer(this Map map, MapObjectLayer objectLayer, bool addMapName = false, bool setNameAsObjectName = false) { return GeneratePrefabsFromLayer(map, objectLayer, XUniTMXConfiguration.Instance.GetTilePrefabsAnchorPoint(), addMapName, setNameAsObjectName); }
/// <summary> /// Adds prefabs referenced in a MapObjectLayer /// </summary> /// <param name="objectLayer">MapObjectLayer to add prefabs from</param> /// <param name="addMapName">true to add Map's name to generated prefabs</param> /// <returns>An array containing all generated Prebafs</returns> public static GameObject[] GeneratePrefabsFromLayer(this Map map, MapObjectLayer objectLayer, Vector2 anchorPoint, bool addMapName = false, bool setNameAsObjectName = false) { if (objectLayer != null) { List<GameObject> generatedPrefabs = new List<GameObject>(); List<MapObject> mapObjects = objectLayer.Objects; foreach (MapObject mapObj in mapObjects) { generatedPrefabs.Add(map.GeneratePrefab(mapObj, anchorPoint, objectLayer.LayerGameObject, addMapName, setNameAsObjectName)); } return generatedPrefabs.ToArray(); } return null; }
/// <summary> /// Generates Colliders from an MapObject Layer. Every Object in it will generate a GameObject with a Collider. /// </summary> /// <param name="objectLayer">MapObjectLayer</param> /// <param name="tag">Tag for the generated GameObjects</param> /// <param name="physicsLayer">Physics Layer for the generated GameObjects</param> /// <param name="collidersAreTrigger">true to generate Trigger mapObjects, false otherwhise.</param> /// <param name="is2DCollider">true to generate 2D mapObjects, false for 3D mapObjects</param> /// <param name="collidersZDepth">Z position of the mapObjects</param> /// <param name="collidersWidth">Width for 3D mapObjects</param> /// <param name="collidersAreInner">true to generate inner collisions for 3D mapObjects</param> /// <returns>An Array containing all generated GameObjects</returns> public static GameObject[] GenerateCollidersFromLayer(this Map map, MapObjectLayer objectLayer, bool is2DCollider = true, bool collidersAreTrigger = false, string tag = "Untagged", int physicsLayer = 0, float collidersZDepth = 0, float collidersWidth = 1, bool collidersAreInner = false) { if (is2DCollider) return map.GenerateColliders2DFromLayer(objectLayer, collidersAreTrigger, tag, physicsLayer, null, collidersZDepth); else return map.GenerateColliders3DFromLayer(objectLayer, collidersAreTrigger, tag, physicsLayer, null, collidersZDepth, collidersWidth, collidersAreInner); }
void Initialize(XmlDocument document, bool makeUnique, string fullPath, string mapPath) { XmlNode mapNode = document["map"]; Version = mapNode.Attributes["version"].Value; Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true); Width = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture); Height = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture); TileWidth = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture); TileHeight = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture); XmlNode propertiesNode = document.SelectSingleNode("map/properties"); if (propertiesNode != null) { Properties = new PropertyCollection(propertiesNode); //Property.ReadProperties(propertiesNode); } TileSets = new List <TileSet>(); Tiles = new Dictionary <int, Tile>(); foreach (XmlNode tileSet in document.SelectNodes("map/tileset")) { if (tileSet.Attributes["source"] != null) { //TileSets.Add(new ExternalTileSetContent(tileSet, context)); XmlDocument externalTileSet = new XmlDocument(); TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attributes["source"].Value)); //externalTileSet.Load(fullPath + "/" + tileSet.Attributes["source"].Value); externalTileSet.LoadXml(externalTileSetTextAsset.text); XmlNode externalTileSetNode = externalTileSet["tileset"]; //Debug.Log(externalTileSet.Value); TileSet t = new TileSet(externalTileSetNode, mapPath); TileSets.Add(t); foreach (KeyValuePair <int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } //this.Tiles.AddRange(t.Tiles); } else { TileSet t = new TileSet(tileSet, mapPath); TileSets.Add(t); foreach (KeyValuePair <int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } } } // Generate Materials for Map batching List <Material> materials = new List <Material>(); // Generate Materials int i = 0; for (i = 0; i < TileSets.Count; i++) { Material layerMat = new Material(Shader.Find("Unlit/Transparent")); layerMat.mainTexture = TileSets[i].Texture; materials.Add(layerMat); } Layers = new List <Layer>(); i = 0; foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup")) { Layer layerContent; float layerDepth = 1f - (LayerDepthSpacing * i); if (layerNode.Name == "layer") { layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials); //((TileLayer)layerContent).GenerateLayerMesh(MeshRendererPrefab); } else if (layerNode.Name == "objectgroup") { layerContent = new MapObjectLayer(layerNode, TileWidth, TileHeight); } else { throw new Exception("Unknown layer name: " + layerNode.Name); } // Layer names need to be unique for our lookup system, but Tiled // doesn't require unique names. string layerName = layerContent.Name; int duplicateCount = 2; // if a layer already has the same name... if (Layers.Find(l => l.Name == layerName) != null) { // figure out a layer name that does work do { layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount); duplicateCount++; } while (Layers.Find(l => l.Name == layerName) != null); // log a warning for the user to see Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name."); // save that name layerContent.Name = layerName; } layerContent.LayerDepth = layerDepth; Layers.Add(layerContent); namedLayers.Add(layerName, layerContent); i++; } }
void Initialize(XmlDocument document, bool makeUnique, string fullPath, string mapPath) { XmlNode mapNode = document["map"]; Version = mapNode.Attributes["version"].Value; Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true); Width = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture); Height = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture); TileWidth = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture); TileHeight = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture); XmlNode propertiesNode = document.SelectSingleNode("map/properties"); if (propertiesNode != null) { Properties = new PropertyCollection(propertiesNode);//Property.ReadProperties(propertiesNode); } TileSets = new List<TileSet>(); Tiles = new Dictionary<int, Tile>(); foreach (XmlNode tileSet in document.SelectNodes("map/tileset")) { if (tileSet.Attributes["source"] != null) { //TileSets.Add(new ExternalTileSetContent(tileSet, context)); XmlDocument externalTileSet = new XmlDocument(); TextAsset externalTileSetTextAsset = (TextAsset)Resources.Load(mapPath + Path.GetFileNameWithoutExtension(tileSet.Attributes["source"].Value)); //externalTileSet.Load(fullPath + "/" + tileSet.Attributes["source"].Value); externalTileSet.LoadXml(externalTileSetTextAsset.text); XmlNode externalTileSetNode = externalTileSet["tileset"]; //Debug.Log(externalTileSet.Value); TileSet t = new TileSet(externalTileSetNode, mapPath); TileSets.Add(t); foreach (KeyValuePair<int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } //this.Tiles.AddRange(t.Tiles); } else { TileSet t = new TileSet(tileSet, mapPath); TileSets.Add(t); foreach (KeyValuePair<int, Tile> item in t.Tiles) { this.Tiles.Add(item.Key, item.Value); } } } // Generate Materials for Map batching List<Material> materials = new List<Material>(); // Generate Materials int i = 0; for (i = 0; i < TileSets.Count; i++) { Material layerMat = new Material(Shader.Find("Unlit/Transparent")); layerMat.mainTexture = TileSets[i].Texture; materials.Add(layerMat); } Layers = new List<Layer>(); i = 0; foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup")) { Layer layerContent; float layerDepth = 1f - (LayerDepthSpacing * i); if (layerNode.Name == "layer") { layerContent = new TileLayer(layerNode, this, layerDepth, makeUnique, materials); //((TileLayer)layerContent).GenerateLayerMesh(MeshRendererPrefab); } else if (layerNode.Name == "objectgroup") { layerContent = new MapObjectLayer(layerNode, TileWidth, TileHeight); } else { throw new Exception("Unknown layer name: " + layerNode.Name); } // Layer names need to be unique for our lookup system, but Tiled // doesn't require unique names. string layerName = layerContent.Name; int duplicateCount = 2; // if a layer already has the same name... if (Layers.Find(l => l.Name == layerName) != null) { // figure out a layer name that does work do { layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount); duplicateCount++; } while (Layers.Find(l => l.Name == layerName) != null); // log a warning for the user to see Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name."); // save that name layerContent.Name = layerName; } layerContent.LayerDepth = layerDepth; Layers.Add(layerContent); namedLayers.Add(layerName, layerContent); i++; } }
void ContinueLoadingTiledMapAfterTileSetsLoaded() { Layers = new List<Layer>(); int i = 0; foreach (NanoXMLNode layerNode in MapNode.SubNodes) { if (!(layerNode.Name.Equals("layer") || layerNode.Name.Equals("objectgroup") || layerNode.Name.Equals("imagelayer"))) continue; Layer layerContent; int layerDepth = 1 - (XUniTMXConfiguration.Instance.LayerDepthSpacing * i); if (layerNode.Name.Equals("layer")) { layerContent = new TileLayer(layerNode, this, layerDepth, GlobalMakeUniqueTiles); } else if (layerNode.Name.Equals("objectgroup")) { layerContent = new MapObjectLayer(layerNode, this, layerDepth); } else if (layerNode.Name.Equals("imagelayer")) { layerContent = new ImageLayer(layerNode, this, _mapPath); } else { throw new Exception("Unknown layer name: " + layerNode.Name); } // Layer names need to be unique for our lookup system, but Tiled // doesn't require unique names. string layerName = layerContent.Name; int duplicateCount = 2; // if a layer already has the same name... if (Layers.Find(l => l.Name == layerName) != null) { // figure out a layer name that does work do { layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount); duplicateCount++; } while (Layers.Find(l => l.Name == layerName) != null); // log a warning for the user to see Debug.Log("Renaming layer \"" + layerContent.Name + "\" to \"" + layerName + "\" to make a unique name."); // save that name layerContent.Name = layerName; } layerContent.LayerDepth = layerDepth; Layers.Add(layerContent); namedLayers.Add(layerName, layerContent); i++; } if (OnMapFinishedLoading != null) OnMapFinishedLoading(this); }