Esempio n. 1
0
    public void UpdateNeighborTiles(Vector3 position, float width, LayerMask layer, Vector3 pivotOffset)
    {
        List <Collider2D> neighborColliders = GetNeighbors(position, width, layer);

        for (int i = 0; i < neighborColliders.Count; i++)
        {
            TileMask mask  = GetTileMask(neighborColliders[i].transform.position - pivotOffset, width, layer);
            int      index = GetTilePrefab(mask);

            GameObject obj = (GameObject)PrefabUtility.InstantiatePrefab(tiles[index]) as GameObject;
            obj.transform.position   = neighborColliders[i].transform.position;
            obj.transform.rotation   = neighborColliders[i].transform.rotation;
            obj.transform.parent     = neighborColliders[i].transform.parent;
            obj.transform.localScale = new Vector3(width, width, width);
            obj.name = mask.ToString();
            if (neighborColliders[i].GetComponent <SpriteRenderer>() != null && obj.GetComponent <SpriteRenderer>() != null)
            {
                obj.GetComponent <SpriteRenderer>().color = neighborColliders[i].GetComponent <SpriteRenderer>().color;
            }



            if (obj.GetComponent <BoxCollider2D>() == null)
            {
                obj.AddComponent <BoxCollider2D>();
            }
            GameObject.DestroyImmediate(neighborColliders[i].gameObject);
        }
    }
Esempio n. 2
0
    public GameObject GetTile(Vector3 position, float width, Color color, GameObject parent)
    {
        if (tiles == null)
        {
            return(null);
        }


        GameObject newTile;

        TileMask mask  = GetTileMask(position, width, layerMask);
        int      index = GetTilePrefab(mask);

        RaycastHit2D hit = Utility.GetFrontmostRaycastHit(position, tag, layerMask);

        if (hit.collider != null)
        {
            newTile = hit.collider.gameObject;
        }
        else
        {
            newTile = (GameObject)PrefabUtility.InstantiatePrefab(tiles[index]);
        }

        newTile.transform.localScale = new Vector3(width, width, width);
        newTile.transform.position   = position;
        newTile.name             = mask.ToString();
        newTile.transform.parent = parent.transform;



        Vector3 pivotOffset = Vector3.zero;

        if (newTile.GetComponent <SpriteRenderer>() != null)
        {
            //  newTile.GetComponent<SpriteRenderer>().color = color;
            Sprite  sprite = newTile.GetComponent <SpriteRenderer>().sprite;
            Vector2 pivot  = sprite.pivot;
            pivotOffset = new Vector3(pivot.x / sprite.rect.width - .5f, pivot.y / sprite.rect.height - .5f, 0);
            position   += pivotOffset;
            newTile.transform.position += pivotOffset;
        }
        if (newTile.GetComponent <BoxCollider2D>() == null)
        {
            newTile.AddComponent <BoxCollider2D>();
        }

        UpdateNeighborTiles(position, width, layerMask, pivotOffset);
        return(newTile);
    }
Esempio n. 3
0
    private int GetPrefabHelper(TileMask mask)
    {
        string maskStr = mask.ToString();
        var    result  = System.Convert.ToString((int)mask, 2).PadLeft(4, '0');

        int index = 0;

        for (int i = 0; i < tiles.Count; i++)
        {
            if (tiles[i].name == result)
            {
                index = i;
            }
        }
        return(index);
    }