public static void InstantiatePrefabs(TileLayer layer)
    {
        if(layer.tileset == null) return;
        if(layer.tileset.tiles == null) return;

        Transform t = layer.transform.FindChild("TileInstances");
        if(t!=null) {
            DestroyImmediate(t.gameObject);
        }
        t = layer.transform.FindChild("BoxColliders");
        if(t!=null) {
            DestroyImmediate(t.gameObject);
        }

        GameObject instances = new GameObject("TileInstances");
        Transform instancesTransform = instances.transform;
        instancesTransform.parent = layer.transform;
        instancesTransform.localPosition = new Vector3(0, 0, 0);
        instancesTransform.localScale = new Vector3(1, 1, 1);
        instancesTransform.localRotation = new Quaternion(0,0,0,0);

        GameObject boxes = new GameObject("BoxColliders");
        Transform boxesTransform = boxes.transform;
        boxesTransform.parent = layer.transform;
        boxesTransform.localPosition = new Vector3(0, 0, 0);
        boxesTransform.localScale = new Vector3(1, 1, 1);
        boxesTransform.localRotation = new Quaternion(0,0,0,0);

        GameObject prefab;
        GameObject instance;
        GameObject box;
        BoxCollider2D collider;
        Hashtable skip = new Hashtable();

        if(layer.tileSpacing == new Vector2(-1,-1) || layer.tileSpacing == new Vector2(0,0)) layer.tileSpacing = layer.tileSize;

        for(int i = 0; i < layer.layerSize.x; i++) {
            for(int j = 0; j < layer.layerSize.y; j++) {
                UniTileTile tile = layer.GetTile(i, j);
                if(tile!=null) {
                    TileInstance inst = layer.GetTileData(i, j);
                    prefab = tile.prefab;
                    if(prefab != null) {
        #if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
                        instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
        #else
                        instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
        #endif
                        Vector3 scale = instance.transform.localScale;
                        instance.transform.parent = instancesTransform;
                        instance.transform.localScale = scale;
                        Quaternion q = new Quaternion(0,0,0,0);
                        q.eulerAngles = new Vector3(0, 0, (int)inst.rotation * 90);
                        instance.transform.localRotation = q;
                        instance.transform.localPosition = (Vector3)TileLayerUtil.TransformPoint(new Vector2(i * layer.tileSpacing.x + tile.prefabOffset.x, j * layer.tileSpacing.y + tile.prefabOffset.y), new Vector2(i * layer.tileSpacing.x + layer.tileSpacing.x / 2f, j * layer.tileSpacing.y +  + layer.tileSpacing.y / 2f), inst.flippedHorizontally, inst.flippedVertically, inst.rotation);
                        if(inst.flippedHorizontally) {
                            instance.transform.localScale = new Vector3(-instance.transform.localScale.x, instance.transform.localScale.y, instance.transform.localScale.z);
                        }
                        if(inst.flippedVertically) {
                            instance.transform.localScale = new Vector3(instance.transform.localScale.x, -instance.transform.localScale.y, instance.transform.localScale.z);
                        }
                    }
                    if(layer.collidable && tile.boxCollider) {
                        if(skip[new Vector2(i, j)] == null) {
                            if(tile.boxPrefab!=null)
        #if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
                                box = (GameObject)PrefabUtility.InstantiatePrefab(tile.boxPrefab);
        #else
                                box = (GameObject)PrefabUtility.InstantiatePrefab(tile.boxPrefab);
        #endif
                            else
                                box = new GameObject("BoxCollider");

                            box.layer = tile.boxLayer;
                            box.tag = tile.boxTag;
                            collider = box.GetComponent<BoxCollider2D>();
                            if(!collider) collider = box.AddComponent<BoxCollider2D>();
                            collider.sharedMaterial = tile.boxMaterial;
                            box.transform.parent = boxesTransform;
                            box.transform.localScale = new Vector3(1, 1, 1);
                            box.transform.localRotation = new Quaternion(0,0,0,0);
                            box.transform.localPosition = new Vector3(i * layer.tileSpacing.x, j * layer.tileSpacing.y, 0);

                            int sizeX = 1;
                            int sizeY = 1;
                            while(skip[new Vector2(i, j + sizeY)]==null && layer.CanMergeBoxColliders(i, j, i, j+sizeY)) {
                                skip[new Vector2(i, j + sizeY)] = true;
                                sizeY++;
                            }

                            if(sizeY == 1) {
                                while(skip[new Vector2(i + sizeX, j)]==null && layer.CanMergeBoxColliders(i, j, i+sizeX, j)) {
                                    skip[new Vector2(i + sizeX, j)] = true;
                                    sizeX++;
                                }
                            } else {
                                sizeX = 1;
                                bool columnOk = true;
                                int column = 1;
                                while(columnOk) {
                                    columnOk = true;
                                    for(int k=0;k<sizeY;k++) {
                                        if(skip[new Vector2(i + column, j + k)]!=null || !layer.CanMergeBoxColliders(i,j,i+column, j+k)) {
                                            columnOk = false;
                                            break;
                                        }
                                    }
                                    if(columnOk) {
                                        column++;
                                    }
                                }
                                sizeX = column;
                                for(int k=0;k<sizeX;k++) {
                                    for(int l=0;l<sizeY;l++) {
                                        skip[new Vector2(i + k, j + l)] = true;
                                    }
                                }
                            }

                            if(tile.customBoxDimensions) {
                                Rect rect = TileLayerUtil.TransformRect(tile.boxDimensions, layer.tileSize / 2f, inst.flippedHorizontally, inst.flippedVertically, inst.rotation);
                                float x = rect.width;
                                float y = rect.height;
                                if(rect.x == 0 && rect.xMax == layer.tileSpacing.x) {
                                    x = layer.tileSpacing.x * sizeX;
                                }
                                if(rect.y == 0 && rect.yMax == layer.tileSpacing.y) {
                                    y = layer.tileSpacing.y * sizeY;
                                }
                                collider.size = new Vector3(x, y, tile.boxDepth);
                                collider.offset = new Vector3(collider.size.x / 2f + rect.x, collider.size.y / 2f + rect.y);
                            } else {
                                collider.size = new Vector3(layer.tileSpacing.x * sizeX, layer.tileSpacing.y * sizeY, tile.boxDepth);
                                collider.offset = new Vector3(collider.size.x / 2f, collider.size.y / 2f);
                                //collider.center = new Vector3(layer.tileSpacing.x * sizeX / 2f, layer.tileSpacing.y * sizeY / 2f);
                            }

                        }
                    }
                }
            }
        }
Example #2
0
    public static void InstantiatePrefabs(TileLayer layer)
    {
        if (layer.tileset == null)
        {
            return;
        }
        if (layer.tileset.tiles == null)
        {
            return;
        }

        Transform t = layer.transform.FindChild("TileInstances");

        if (t != null)
        {
            DestroyImmediate(t.gameObject);
        }
        t = layer.transform.FindChild("BoxColliders");
        if (t != null)
        {
            DestroyImmediate(t.gameObject);
        }

        GameObject instances          = new GameObject("TileInstances");
        Transform  instancesTransform = instances.transform;

        instancesTransform.parent        = layer.transform;
        instancesTransform.localPosition = new Vector3(0, 0, 0);
        instancesTransform.localScale    = new Vector3(1, 1, 1);
        instancesTransform.localRotation = new Quaternion(0, 0, 0, 0);

        GameObject boxes          = new GameObject("BoxColliders");
        Transform  boxesTransform = boxes.transform;

        boxesTransform.parent        = layer.transform;
        boxesTransform.localPosition = new Vector3(0, 0, 0);
        boxesTransform.localScale    = new Vector3(1, 1, 1);
        boxesTransform.localRotation = new Quaternion(0, 0, 0, 0);

        GameObject    prefab;
        GameObject    instance;
        GameObject    box;
        BoxCollider2D collider;
        Hashtable     skip = new Hashtable();

        if (layer.tileSpacing == new Vector2(-1, -1) || layer.tileSpacing == new Vector2(0, 0))
        {
            layer.tileSpacing = layer.tileSize;
        }

        for (int i = 0; i < layer.layerSize.x; i++)
        {
            for (int j = 0; j < layer.layerSize.y; j++)
            {
                UniTileTile tile = layer.GetTile(i, j);
                if (tile != null)
                {
                    TileInstance inst = layer.GetTileData(i, j);
                    prefab = tile.prefab;
                    if (prefab != null)
                    {
#if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
                        instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
#else
                        instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
#endif
                        Vector3 scale = instance.transform.localScale;
                        instance.transform.parent     = instancesTransform;
                        instance.transform.localScale = scale;
                        Quaternion q = new Quaternion(0, 0, 0, 0);
                        q.eulerAngles = new Vector3(0, 0, (int)inst.rotation * 90);
                        instance.transform.localRotation = q;
                        instance.transform.localPosition = (Vector3)TileLayerUtil.TransformPoint(new Vector2(i * layer.tileSpacing.x + tile.prefabOffset.x, j * layer.tileSpacing.y + tile.prefabOffset.y), new Vector2(i * layer.tileSpacing.x + layer.tileSpacing.x / 2f, j * layer.tileSpacing.y +  +layer.tileSpacing.y / 2f), inst.flippedHorizontally, inst.flippedVertically, inst.rotation);
                        if (inst.flippedHorizontally)
                        {
                            instance.transform.localScale = new Vector3(-instance.transform.localScale.x, instance.transform.localScale.y, instance.transform.localScale.z);
                        }
                        if (inst.flippedVertically)
                        {
                            instance.transform.localScale = new Vector3(instance.transform.localScale.x, -instance.transform.localScale.y, instance.transform.localScale.z);
                        }
                    }
                    if (layer.collidable && tile.boxCollider)
                    {
                        if (skip[new Vector2(i, j)] == null)
                        {
                            if (tile.boxPrefab != null)
#if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
                            { box = (GameObject)PrefabUtility.InstantiatePrefab(tile.boxPrefab); }
#else
                            { box = (GameObject)PrefabUtility.InstantiatePrefab(tile.boxPrefab); }
#endif
                            else
                            {
                                box = new GameObject("BoxCollider");
                            }

                            box.layer = tile.boxLayer;
                            box.tag   = tile.boxTag;
                            collider  = box.GetComponent <BoxCollider2D>();
                            if (!collider)
                            {
                                collider = box.AddComponent <BoxCollider2D>();
                            }
                            collider.sharedMaterial     = tile.boxMaterial;
                            box.transform.parent        = boxesTransform;
                            box.transform.localScale    = new Vector3(1, 1, 1);
                            box.transform.localRotation = new Quaternion(0, 0, 0, 0);
                            box.transform.localPosition = new Vector3(i * layer.tileSpacing.x, j * layer.tileSpacing.y, 0);

                            int sizeX = 1;
                            int sizeY = 1;
                            while (skip[new Vector2(i, j + sizeY)] == null && layer.CanMergeBoxColliders(i, j, i, j + sizeY))
                            {
                                skip[new Vector2(i, j + sizeY)] = true;
                                sizeY++;
                            }

                            if (sizeY == 1)
                            {
                                while (skip[new Vector2(i + sizeX, j)] == null && layer.CanMergeBoxColliders(i, j, i + sizeX, j))
                                {
                                    skip[new Vector2(i + sizeX, j)] = true;
                                    sizeX++;
                                }
                            }
                            else
                            {
                                sizeX = 1;
                                bool columnOk = true;
                                int  column   = 1;
                                while (columnOk)
                                {
                                    columnOk = true;
                                    for (int k = 0; k < sizeY; k++)
                                    {
                                        if (skip[new Vector2(i + column, j + k)] != null || !layer.CanMergeBoxColliders(i, j, i + column, j + k))
                                        {
                                            columnOk = false;
                                            break;
                                        }
                                    }
                                    if (columnOk)
                                    {
                                        column++;
                                    }
                                }
                                sizeX = column;
                                for (int k = 0; k < sizeX; k++)
                                {
                                    for (int l = 0; l < sizeY; l++)
                                    {
                                        skip[new Vector2(i + k, j + l)] = true;
                                    }
                                }
                            }

                            if (tile.customBoxDimensions)
                            {
                                Rect  rect = TileLayerUtil.TransformRect(tile.boxDimensions, layer.tileSize / 2f, inst.flippedHorizontally, inst.flippedVertically, inst.rotation);
                                float x    = rect.width;
                                float y    = rect.height;
                                if (rect.x == 0 && rect.xMax == layer.tileSpacing.x)
                                {
                                    x = layer.tileSpacing.x * sizeX;
                                }
                                if (rect.y == 0 && rect.yMax == layer.tileSpacing.y)
                                {
                                    y = layer.tileSpacing.y * sizeY;
                                }
                                collider.size   = new Vector3(x, y, tile.boxDepth);
                                collider.offset = new Vector3(collider.size.x / 2f + rect.x, collider.size.y / 2f + rect.y);
                            }
                            else
                            {
                                collider.size   = new Vector3(layer.tileSpacing.x * sizeX, layer.tileSpacing.y * sizeY, tile.boxDepth);
                                collider.offset = new Vector3(collider.size.x / 2f, collider.size.y / 2f);
                                //collider.center = new Vector3(layer.tileSpacing.x * sizeX / 2f, layer.tileSpacing.y * sizeY / 2f);
                            }
                        }
                    }
                }
            }
        }