Exemplo n.º 1
0
        public void ClearTextures()
        {
            foreach (TileSet tileSet in TileSets)
            {
                foreach (TileData tile in tileSet.TileList)
                {
                    DestroyImmediate(tile.Texture);
                }
            }

            if (tileDataInScene != null)
            {
                DestroyImmediate(tileDataInScene.Texture);
                tileDataInScene = null;
            }
        }
Exemplo n.º 2
0
        public static GameObject CreateTile(Vector3 position, TileData tileData, float alpha, string sortingLayerName)
        {
            if (tileData == null || tileData.Sprite == null)
            {
                return null;
            }
            GameObject go = new GameObject();
            go.transform.position = position;

            Vector3 localScale = go.transform.localScale;
            if (tileData.FlipHorizontally)
            {
                localScale.x = -1;
            }
            else
            {
                localScale.x = 1;
            }

            if (tileData.FlipVertically)
            {
                localScale.y = -1;
            }
            else
            {
                localScale.y = 1;
            }
            go.transform.localScale = localScale;
            SpriteRenderer renderer = go.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
            renderer.sprite = tileData.Sprite;
            renderer.sortingLayerName = sortingLayerName;
            renderer.sortingOrder = tileData.OrderInLayer;
            Color color = renderer.color;
            renderer.color = new Color(color.r, color.g, color.b, alpha);
            return go;
        }
Exemplo n.º 3
0
        private void AddSpriteToTileSet(Sprite sprite)
        {
            if (sprite == null)
            {
                return;
            }
            if (TileDataList == null)
            {
                AddTileSet();
            }

            TileData data = new TileData();
            data.Sprite = sprite;
            TileDataList.Add(data);
        }
Exemplo n.º 4
0
        private Tile SetupTile(GameObject go, TileData tileData)
        {
            if (go == null || tileData == null)
            {
                return null;
            }
            go.transform.Rotate(0, 0, tileData.Rotation, Space.World);

            Tile tile = go.GetComponent(typeof(Tile)) as Tile;

            if (!tile)
            {
                tile = go.AddComponent(typeof(Tile)) as Tile;
            }

            Collider2D collider = go.GetComponent(typeof(Collider2D)) as Collider2D;
            if (collider)
            {
                DestroyImmediate(collider);
                collider = null;
            }
            tile.Collision = tileData.Collision;
            switch (tile.Collision)
            {
                case CollisionType.Box:
                    collider = go.AddComponent(typeof(BoxCollider2D)) as BoxCollider2D;
                    break;
                case CollisionType.Circle:
                    collider = go.AddComponent(typeof(CircleCollider2D)) as CircleCollider2D;
                    break;
                case CollisionType.Polygon:
                    collider = go.AddComponent(typeof(PolygonCollider2D)) as PolygonCollider2D;
                    break;
            }
            if (collider != null)
            {
                collider.sharedMaterial = tileData.PhysicsMaterial;
                collider.isTrigger = tileData.IsTrigger;
            }
            return tile;
        }
Exemplo n.º 5
0
 public static TileData GetTileDataFromTile(Tile tile)
 {
     if (tile == null)
     {
         return null;
     }
     TileData tileData = new TileData();
     SpriteRenderer renderer = tile.GetComponent<SpriteRenderer>();
     Collider2D collider = tile.GetComponent<Collider2D>();
     Vector3 localScale = tile.transform.localScale;
     tileData.FlipHorizontally = localScale.x == -1;
     tileData.FlipVertically = localScale.y == -1;
     tileData.Sprite = renderer.sprite;
     tileData.OrderInLayer = renderer.sortingOrder;
     tileData.Rotation = tile.transform.rotation.eulerAngles.z;
     tileData.Collision = tile.Collision;
     tileData.Tag = tile.tag;
     if (collider != null)
     {
         tileData.IsTrigger = collider.isTrigger;
         tileData.PhysicsMaterial = collider.sharedMaterial;
     }
     return tileData;
 }
Exemplo n.º 6
0
        public static bool IsTheSameTile(Tile tile, TileData tileData)
        {
            Vector3 localScale = tile.transform.localScale;
            SpriteRenderer renderer = tile.GetComponent<SpriteRenderer>();

            if (tile.tag != tileData.Tag)
            {
                return false;
            }

            if (renderer.sortingOrder != tileData.OrderInLayer)
            {
                return false;
            }

            if ((tileData.FlipHorizontally && localScale.x != -1) || (!tileData.FlipHorizontally && localScale.x != 1))
            {
                return false;
            }

            if ((tileData.FlipVertically && localScale.y != -1) || (!tileData.FlipVertically && localScale.y != 1))
            {
                return false;
            }

            if (renderer.sprite != tileData.Sprite)
            {
                return false;
            }

            if (tile.Collision != tileData.Collision)
            {
                return false;
            }

            if (Mathf.Abs(tile.transform.eulerAngles.z - tileData.Rotation) > 0.0001f)
            {
                return false;
            }

            Collider2D collider = tile.GetComponent<Collider2D>();
            if (collider != null)
            {
                if (collider.isTrigger != tileData.IsTrigger || collider.sharedMaterial != tileData.PhysicsMaterial)
                {
                    return false;
                }
            }

            return true;
        }