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); }
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() { 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); }