Exemple #1
0
    //-------------------------------------------------------------------------
    static void addColliderGameObjectsForOTTileMapLayer(Transform layerNode, Component otTileMap, object otTileMapLayer, int layerIndex)
    {
        // read tileMapSize = OTTileMap.mapSize (UnityEngine.Vector2)
        System.Type otTileMapType = otTileMap.GetType();
        FieldInfo   fieldMapSize  = otTileMapType.GetField("mapSize");

        if (fieldMapSize == null)
        {
            Debug.LogError("Detected a missing 'mapSize' member variable at OTTileMap component - Is your Orthello package up to date? 2D ColliderGen might probably not work correctly with this version.");
            return;
        }
        Vector2 tileMapSize   = (UnityEngine.Vector2)fieldMapSize.GetValue(otTileMap);
        int     tileMapWidth  = (int)tileMapSize.x;
        int     tileMapHeight = (int)tileMapSize.y;
        // read mapTileSize = OTTileMap.mapTileSize (UnityEngine.Vector2)
        FieldInfo fieldMapTileSize = otTileMapType.GetField("mapTileSize");

        if (fieldMapTileSize == null)
        {
            Debug.LogError("Detected a missing 'mapTileSize' member variable at OTTileMap component - Is your Orthello package up to date? 2D ColliderGen might probably not work correctly with this version.");
            return;
        }
        Vector2 mapTileSize  = (UnityEngine.Vector2)fieldMapTileSize.GetValue(otTileMap);
        Vector3 mapTileScale = new Vector3(1.0f / tileMapSize.x, 1.0f / tileMapSize.y, 1.0f / tileMapSize.x);

        System.Collections.Generic.Dictionary <int, object> tileSetAtTileIndex = new System.Collections.Generic.Dictionary <int, object>();


        Vector2 bottomLeftTileOffset = new Vector2(-0.5f, -0.5f);

        // read tileIndices = otTileMapLayer.tiles (int[])
        System.Type otTileMapLayerType = otTileMapLayer.GetType();
        FieldInfo   fieldTiles         = otTileMapLayerType.GetField("tiles");

        if (fieldTiles == null)
        {
            Debug.LogError("Detected a missing 'tiles' member variable at OTTileMapLayer component - Is your Orthello package up to date? 2D ColliderGen might probably not work correctly with this version.");
            return;
        }
        int[] tileIndices = (int[])fieldTiles.GetValue(otTileMapLayer);
        System.Collections.Generic.Dictionary <int, Transform> groupNodeForTileIndex = new System.Collections.Generic.Dictionary <int, Transform>();
        Transform tileGroupNode = null;

        object tileSet = null;

        for (int y = 0; y < tileMapHeight; ++y)
        {
            for (int x = 0; x < tileMapWidth; ++x)
            {
                int tileIndex = tileIndices[y * tileMapWidth + x];
                if (tileIndex != 0)
                {
                    if (groupNodeForTileIndex.ContainsKey(tileIndex))
                    {
                        tileGroupNode = groupNodeForTileIndex[tileIndex];
                        tileSet       = tileSetAtTileIndex[tileIndex];
                    }
                    else
                    {
                        // create a group node
                        GameObject newTileGroup = UndoAware.CreateGameObject("Tile Type " + tileIndex);
                        newTileGroup.transform.parent        = layerNode;
                        newTileGroup.transform.localPosition = Vector3.zero;
                        newTileGroup.transform.localScale    = Vector3.one;
                        tileGroupNode = newTileGroup.transform;
                        groupNodeForTileIndex[tileIndex] = tileGroupNode;
                        // get tileset for tile index
                        tileSet = AlphaMeshCollider.GetOTTileSetForTileIndex(otTileMap, tileIndex);
                        tileSetAtTileIndex[tileIndex] = tileSet;
                    }
                    // read tileSet.tileSize (Vector2)
                    System.Type otTileSetType = tileSet.GetType();
                    FieldInfo   fieldTileSize = otTileSetType.GetField("tileSize");
                    if (fieldTileSize == null)
                    {
                        Debug.LogError("Detected a missing 'tileSize' member variable at OTTileSet class - Is your Orthello package up to date? 2D ColliderGen might probably not work correctly with this version.");
                        return;
                    }
                    Vector2 tileSize         = (UnityEngine.Vector2)fieldTileSize.GetValue(tileSet);
                    Vector3 tileScale        = new Vector3(mapTileScale.x / mapTileSize.x * tileSize.x, mapTileScale.y / mapTileSize.y * tileSize.y, mapTileScale.z);
                    Vector2 tileCenterOffset = new Vector3(tileScale.x * 0.5f, tileScale.x * 0.5f);

                    // add a GameObject for each enabled tile with name "tile y x"
                    GameObject alphaMeshColliderNode = UndoAware.CreateGameObject("tile " + y + " " + x);
                    alphaMeshColliderNode.transform.parent = tileGroupNode;
                    AlphaMeshCollider alphaMeshColliderComponent = UndoAware.AddComponent <AlphaMeshCollider>(alphaMeshColliderNode);
                    alphaMeshColliderComponent.SetOTTileMap(otTileMap, layerIndex, x, y, tileMapWidth);

                    // set the position of the tile collider according to its (x,y) pos in the map.
                    alphaMeshColliderNode.transform.localPosition = new Vector3(x * mapTileScale.x + bottomLeftTileOffset.x + tileCenterOffset.x, (tileMapSize.y - 1 - y) * mapTileScale.y + bottomLeftTileOffset.y + tileCenterOffset.y, 0.0f);
                    alphaMeshColliderNode.transform.localScale    = tileScale;
                }
            }
        }
    }