当前位置: 首页>>代码示例>>C#>>正文


C# TileSet.AddCollision方法代码示例

本文整理汇总了C#中TileSet.AddCollision方法的典型用法代码示例。如果您正苦于以下问题:C# TileSet.AddCollision方法的具体用法?C# TileSet.AddCollision怎么用?C# TileSet.AddCollision使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TileSet的用法示例。


在下文中一共展示了TileSet.AddCollision方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateColliderMesh

    public Mesh CreateColliderMesh()
    {
        // We use the currentLayer ID to order them on the Z axis.
        // Be aware that on the collider mesh it depends on the freezeColliderLayers
        // variable, this way you can avoid having colliders on different z axis.
        int currentLayerID = 0;

        // UsedVertices is used to maintain a count of the vertices between
        // layers so when you call renderTriangles, it nows where to start.
        int usedVertices = 0;

        TileSet tileset = null;
        List<Vector3> vertices = new List<Vector3> ();
        List<int> triangles = new List<int> ();
        Mesh mesh = new Mesh ();

        XmlDocument xmldoc = new XmlDocument ();
        xmldoc.Load (new StringReader (tilemap.text));
        XmlNodeList nodelist = xmldoc.DocumentElement.ChildNodes;

        // Works the almost the same way as the createMesh method (builds the
        // tileset and layer objects, then builds the mesh with those
        // but only checks on the collision names plus it doesn't build model
        // uv since its not neccesary.
        foreach (XmlNode outerNode in nodelist) {
            switch (outerNode.Name) {
            case "tileset":
                if (outerNode.Attributes ["name"].InnerText.IndexOf ("Collision") != -1) {
                    XmlNode imageNode = outerNode.SelectSingleNode ("image");
                    int firstGID = int.Parse (outerNode.Attributes ["firstgid"].InnerText);
                    int width = int.Parse (outerNode.Attributes ["tilewidth"].InnerText);
                    int height = int.Parse (outerNode.Attributes ["tileheight"].InnerText);
                    int imageWidth = int.Parse (imageNode.Attributes ["width"].InnerText);
                    int imageheight = int.Parse (imageNode.Attributes ["height"].InnerText);
                    tileset = new TileSet (firstGID, width, height, imageWidth, imageheight);

                    // On the TMX editor, we set some attributes on our collider tiles so when parsing the
                    // tileset, we'll be adding them to our tileset object.
                    foreach (XmlNode innerNode in outerNode.ChildNodes) {
                        if (innerNode.Name == "tile") {
                            int tileID = 0;
                            if (int.TryParse (innerNode.Attributes ["id"].InnerText, out tileID)) {
                                XmlNode propertyNodes = innerNode.SelectSingleNode ("properties");
                                foreach (XmlNode propertyNode in propertyNodes) {
                                    if (propertyNode.Attributes ["name"].InnerText == "col") {
                                        tileset.AddCollision (tileID, propertyNode.Attributes ["value"].InnerText);
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case "layer":
                if (outerNode.Attributes ["name"].InnerText.IndexOf ("collision_") != -1) {
                    XmlNode dataNode = outerNode.SelectSingleNode ("data");
                    int layerWidth = int.Parse (outerNode.Attributes ["width"].InnerText);
                    int layerHeight = int.Parse (outerNode.Attributes ["height"].InnerText);
                    string csvData = dataNode.InnerText;
                    Layer currentLayer = new Layer (tileset, csvData, currentLayerID, layerWidth, layerHeight);
                    vertices.AddRange (currentLayer.renderColVertices ());
                    triangles.AddRange (currentLayer.renderTriangles (usedVertices, usedVertices + currentLayer.vertexCount));
                    usedVertices += currentLayer.vertexCount;
                    // In case we freeze the layers on collider they wont move on the z axis.
                }
                if (!freezeLayersOnCollider)
                    currentLayerID++;
                break;
            case "objectgroup":
                if (outerNode.Attributes ["name"].InnerText.IndexOf ("CollisionObjects") != -1) {
                    foreach (XmlNode objectNode in outerNode.ChildNodes) {
                        int colliderWidth = 0;
                        int colliderHeight = 0;

                        if (objectNode.Attributes.GetNamedItem ("width") != null) {
                            colliderWidth = int.Parse (objectNode.Attributes ["width"].InnerText);
                            colliderHeight = int.Parse (objectNode.Attributes ["height"].InnerText);
                        }

                        int colliderX = int.Parse (objectNode.Attributes ["x"].InnerText);
                        int colliderY = int.Parse (objectNode.Attributes ["y"].InnerText);
                        colliderX += (colliderWidth / 2 + 16);
                        colliderY = - (colliderY + colliderHeight / 2);

                        GameObject newCollider = null;

                        if (objectNode.HasChildNodes) {
                            // Can be polyline, ellipse or polygon, as boxes have no child.
                            string objectType = objectNode.FirstChild.Name;
                            Debug.Log ("Has children: " + objectType);
                            switch (objectType) {
                            case "ellipse":
                                newCollider = CreateEllipseCollider (colliderX, colliderY, colliderWidth, colliderHeight);
                                break;
                            case "polyline":
                                XmlNode polylineData = objectNode.FirstChild;
                                newCollider = CreatePolylineCollider (polylineData, colliderX, colliderY);
                                break;
                            case "polygon":
                            default:
//.........这里部分代码省略.........
开发者ID:kjellski,项目名称:TiledMapLoader,代码行数:101,代码来源:TileMap.cs


注:本文中的TileSet.AddCollision方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。