void BuildGameObject() { LayerGameObject.transform.parent = Parent; LayerGameObject.transform.localPosition = new Vector3(Position.x, Position.y, this.LayerDepth); LayerGameObject.isStatic = true; LayerGameObject.SetActive(Visible); SpriteRenderer tileRenderer = LayerGameObject.AddComponent <SpriteRenderer>(); tileRenderer.sprite = Sprite.Create(Texture, new Rect(0, 0, Texture.width, Texture.height), Vector2.up, TileWidth); tileRenderer.sprite.name = Texture.name; tileRenderer.sortingOrder = SortingOrder; // Use Layer's name as Sorting Layer tileRenderer.sortingLayerName = this.Name; //tileRenderer.material = new Material(baseMaterial); //tileRenderer.material.mainTexture = Texture; }
// Renders the tile vertices. // Basically, it reads the tiles and creates its 4 vertexes (forming a rectangle or square according to settings) or sprite private void GenerateLayer() { int startX = Width - 1; int endX = -1; int startY = Height - 1; int endY = -1; int directionX = -1; int directionY = -1; //float zOffset = 0.001f; // To create the tiles, we must follow the order dictated by map.MapRenderOrder // Not unique tiles have inverted bottom-top top-bottom render order :X switch (BaseMap.MapRenderOrder) { case RenderOrder.Right_Down: if (MakeUniqueTiles) { startY = Height - 1; endY = -1; directionY = -1; } else { startX = 0; endX = Width; directionX = 1; startY = 0; endY = Height; directionY = 1; } break; case RenderOrder.Right_Up: if (MakeUniqueTiles) { startY = 0; endY = Height; directionY = 1; } else { startX = 0; endX = Width; directionX = 1; startY = Height - 1; endY = -1; directionY = -1; } break; case RenderOrder.Left_Up: if (MakeUniqueTiles) { startX = 0; endX = Width; directionX = 1; startY = 0; endY = Height; directionY = 1; } else { startY = Height - 1; endY = -1; directionY = -1; } break; case RenderOrder.Left_Down: if (MakeUniqueTiles) { startX = 0; endX = Width; directionX = 1; } else { startY = 0; endY = Height; directionY = 1; } break; } // If we are not going to generate unique tiles, then we need a mesh, a mesh filter and a mesh renderer if (!MakeUniqueTiles) { CreateLayerMesh(startX, endX, startY, endY, directionX, directionY); } else { CreateUniqueTiles(startX, endX, startY, endY, directionX, directionY); } LayerGameObject.transform.parent = BaseMap.MapObject.transform; LayerGameObject.transform.localPosition = new Vector3(0, 0, this.LayerDepth); LayerGameObject.isStatic = true; LayerGameObject.SetActive(Visible); }
/// <summary> /// Creates a Map Object Layer from node /// </summary> /// <param name="node">XML node to parse</param> /// <param name="tiledMap">MapObjectLayer parent Map</param> /// <param name="layerDepth">This Layer's zDepth</param> /// <param name="materials">List of Materials containing the TileSet textures</param> public MapObjectLayer(NanoXMLNode node, Map tiledMap, int layerDepth, List <Material> materials) : base(node) { if (node.GetAttribute("color") != null) { // get the color string, removing the leading # string color = node.GetAttribute("color").Value.Substring(1); // get the RGB individually string r = color.Substring(0, 2); string g = color.Substring(2, 2); string b = color.Substring(4, 2); // convert to the color Color = new Color( (byte)int.Parse(r, NumberStyles.AllowHexSpecifier), (byte)int.Parse(g, NumberStyles.AllowHexSpecifier), (byte)int.Parse(b, NumberStyles.AllowHexSpecifier)); } LayerGameObject.transform.parent = tiledMap.MapObject.transform; LayerGameObject.transform.localPosition = new Vector3(0, 0, this.LayerDepth); LayerGameObject.isStatic = true; LayerGameObject.SetActive(Visible); Objects = new List <MapObject>(); foreach (NanoXMLNode objectNode in node.SubNodes) { if (!objectNode.Name.Equals("object")) { continue; } MapObject mapObjectContent = new MapObject(objectNode, this); mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight, tiledMap.Orientation); mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name; // Object names need to be unique for our lookup system, but Tiled // doesn't require unique names. string objectName = mapObjectContent.Name; int duplicateCount = 2; // if a object already has the same name... if (Objects.Find(o => o.Name.Equals(objectName)) != null) { // figure out a object name that does work do { objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount); duplicateCount++; } while (Objects.Find(o => o.Name.Equals(objectName)) != null); // log a warning for the user to see //Debug.LogWarning("Renaming object \"" + mapObjectContent.Name + "\" to \"" + objectName + "\" in layer \"" + Name + "\" to make a unique name."); // save that name mapObjectContent.Name = objectName; } mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials); AddObject(mapObjectContent); } }