private void ProcessAttributes(XElement xTileset) { m_TilesetScript.name = xTileset.GetAttributeAs <string>("name"); m_TilesetScript.m_TileWidth = xTileset.GetAttributeAs <int>("tilewidth"); m_TilesetScript.m_TileHeight = xTileset.GetAttributeAs <int>("tileheight"); m_TilesetScript.m_Spacing = xTileset.GetAttributeAs <int>("spacing"); m_TilesetScript.m_Margin = xTileset.GetAttributeAs <int>("margin"); m_TilesetScript.m_TileCount = xTileset.GetAttributeAs <int>("tilecount"); m_TilesetScript.m_TileColumns = xTileset.GetAttributeAs <int>("columns"); var xTileOffset = xTileset.Element("tileoffset"); if (xTileOffset != null) { var x = xTileOffset.GetAttributeAs <float>("x", 0.0f); var y = xTileOffset.GetAttributeAs <float>("y", 0.0f); m_TilesetScript.m_TileOffset = new Vector2(x, y); } var xGrid = xTileset.Element("grid"); if (xGrid != null) { m_TilesetScript.m_GridOrientation = xGrid.GetAttributeAs <GridOrientation>("orientation"); var w = xGrid.GetAttributeAs <float>("width", 0.0f); var h = xGrid.GetAttributeAs <float>("height", 0.0f); m_TilesetScript.m_GridSize = new Vector2(w, h); } m_TilesetScript.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xTileset.Element("properties")); }
private void ProcessTileElement(XElement xTile) { int index = xTile.GetAttributeAs <int>("id", -1); SuperTile tile; if (m_TilesetScript.TryGetTile(index, out tile)) { // A tile may have a type associated with it tile.m_Type = xTile.GetAttributeAs("type", ""); // Tiles can have custom properties (and properties inherited from their Type) tile.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xTile.Element("properties")); tile.m_CustomProperties.AddPropertiesFromType(tile.m_Type, m_Importer.SuperImportContext); // Does the tile have any animation data? var xAnimation = xTile.Element("animation"); if (xAnimation != null) { ProcessAnimationElement(tile, xAnimation); } // Does the tile have an object group? var xObjectGroup = xTile.Element("objectgroup"); if (xObjectGroup != null && xObjectGroup.HasElements) { ProcessObjectGroupElement(tile, xObjectGroup); } } }
private void FillCustomObjectTypes(SuperSettingsImporter importer) { m_CustomObjectTypes = new List <CustomObjectType>(); if (m_ObjectTypesXml != null) { XDocument xdoc = XDocument.Parse(m_ObjectTypesXml.text); if (xdoc.Root.Name != "objecttypes") { importer.ReportError("'{0}' is not a valid object types xml file.", m_ObjectTypesXml.name); return; } // Create a dependency on our Object Types xml file so that settings are automatically updated if it changes importer.AddAssetPathDependency(AssetDatabase.GetAssetPath(m_ObjectTypesXml)); // Import the data from the objecttype elements foreach (var xObjectType in xdoc.Descendants("objecttype")) { var cot = new CustomObjectType(); cot.m_Name = xObjectType.GetAttributeAs("name", "NoName"); cot.m_Color = xObjectType.GetAttributeAsColor("color", Color.gray); cot.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xObjectType); m_CustomObjectTypes.Add(cot); } } }
private void ProcessProperties(XElement xObject) { var xProperties = xObject.Element("properties"); if (xProperties != null) { var properties = CustomPropertyLoader.LoadCustomPropertyList(xProperties); m_ObjectTemplate.m_CustomProperties = properties; } }
public void AddSuperCustomProperties(GameObject go, XElement xProperties, SuperTile tile, string typeName) { // Load our "local" properties first var component = go.AddComponent <SuperCustomProperties>(); var properties = CustomPropertyLoader.LoadCustomPropertyList(xProperties); // Do we have any properties from a tile to add? if (tile != null) { properties.CombineFromSource(tile.m_CustomProperties); } // Add properties from our object type (this should be last) properties.AddPropertiesFromType(typeName, SuperImportContext); // Sort the properties alphabetically component.m_Properties = properties.OrderBy(p => p.m_Name).ToList(); AssignUnityTag(component); AssignUnityLayer(component); }
// Invoke this to ensure that Xml Object Types are up-to-date // Our importers that depend on Object Types from tiled will want to call this early in their import process internal void RefreshCustomObjectTypes() { m_CustomObjectTypes = new List <CustomObjectType>(); m_ParseXmlError = string.Empty; if (m_ObjectTypesXml != null) { try { XDocument xdoc = XDocument.Parse(m_ObjectTypesXml.text); if (xdoc.Root.Name != "objecttypes") { m_ParseXmlError = string.Format("'{0}' is not a valid object types xml file.", m_ObjectTypesXml.name); } // Import the data from the objecttype elements foreach (var xObjectType in xdoc.Descendants("objecttype")) { var cot = new CustomObjectType(); cot.m_Name = xObjectType.GetAttributeAs("name", "NoName"); cot.m_Color = xObjectType.GetAttributeAsColor("color", Color.gray); cot.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xObjectType); m_CustomObjectTypes.Add(cot); } } catch (XmlException xe) { m_ParseXmlError = string.Format("'{0}' is not a valid XML file.\n\nError: {1}", m_ObjectTypesXml.name, xe.Message); m_CustomObjectTypes.Clear(); } catch (Exception e) { m_ParseXmlError = e.Message; m_CustomObjectTypes.Clear(); } } }
private void ProcessObjectGroupElement(SuperTile tile, XElement xObjectGroup) { // Object groups on tiles come from the Tiled Collision Editor if (xObjectGroup.Elements().Any()) { // We'll be adding collision objects to our tile tile.m_CollisionObjects = new List <CollisionObject>(); foreach (var xObject in xObjectGroup.Elements("object")) { var collision = new CollisionObject(); // Template may fill in a bunch of properties m_Importer.ApplyTemplateToObject(xObject); collision.m_ObjectId = xObject.GetAttributeAs("id", 0); collision.m_ObjectName = xObject.GetAttributeAs("name", string.Format("Object_{0}", collision.m_ObjectId)); collision.m_ObjectType = xObject.GetAttributeAs("type", ""); collision.m_Position.x = xObject.GetAttributeAs("x", 0.0f); collision.m_Position.y = xObject.GetAttributeAs("y", 0.0f); collision.m_Size.x = xObject.GetAttributeAs("width", 0.0f); collision.m_Size.y = xObject.GetAttributeAs("height", 0.0f); collision.m_Rotation = xObject.GetAttributeAs("rotation", 0.0f); // Are there any properties on the collision object? collision.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xObject.Element("properties")); collision.m_CustomProperties.AddPropertiesFromType(collision.m_ObjectType, m_Importer.SuperImportContext); var xPolygon = xObject.Element("polygon"); var xPolyline = xObject.Element("polyline"); if (xPolygon != null) { // Get points and make sure they are CCW var points = xPolygon.GetAttributeAsVector2Array("points"); if (PolygonUtils.SumOverEdges(points) < 0) { points = points.Reverse().ToArray(); } collision.MakePointsFromPolygon(points); } else if (xPolyline != null) { // Get points and make sure they are CCW var points = xPolyline.GetAttributeAsVector2Array("points"); if (PolygonUtils.SumOverEdges(points) < 0) { points = points.Reverse().ToArray(); } collision.MakePointsFromPolyline(points); } else if (xObject.Element("ellipse") != null) { if (collision.m_Size.x == 0) { m_Importer.ReportError("Invalid ellipse (Tile ID ='{0}') in tileset '{1}' has zero width", tile.m_TileId, m_TilesetScript.name); m_TilesetScript.m_HasErrors = true; } else if (collision.m_Size.y == 0) { m_Importer.ReportError("Invalid ellipse (Tile ID ='{0}') in tileset '{1}' has zero height", tile.m_TileId, m_TilesetScript.name); m_TilesetScript.m_HasErrors = true; } else { collision.MakePointsFromEllipse(m_Importer.SuperImportContext.Settings.EdgesPerEllipse); } } else { // By default, objects are rectangles if (collision.m_Size.x == 0) { m_Importer.ReportError("Invalid rectangle (Tile ID ='{0}') in tileset '{1}' has zero width", tile.m_TileId, m_TilesetScript.name); m_TilesetScript.m_HasErrors = true; } else if (collision.m_Size.y == 0) { m_Importer.ReportError("Invalid rectangle (Tile ID ='{0}') in tileset '{1}' has zero height", tile.m_TileId, m_TilesetScript.name); m_TilesetScript.m_HasErrors = true; } else { collision.MakePointsFromRectangle(); } } // Do not add if there are no points if (!collision.Points.IsEmpty()) { AssignCollisionObjectProperties(collision, tile); collision.RenderPoints(tile, m_TilesetScript.m_GridOrientation, m_TilesetScript.m_GridSize); tile.m_CollisionObjects.Add(collision); } } } }