Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FlamingFox1307 · Jun 29, 2020 at 07:11 AM · switch-caseelse if

Is there a better way to do connected textures for a tileset?

I'm trying to make connected textures for a tile set, and I'm wondering if there's a better way than using a hundred else if statements. Would it be possible to use a switch-case with this?

 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 // Tile that displays a Sprite when it is alone and a different Sprite when it is orthogonally adjacent to the same NeighourTile
 [CreateAssetMenu]
 public class Block : TileBase
 {
     public Sprite block;
     public Sprite blockR;
     public Sprite blockRL;
     public Sprite blockL;
     public Sprite blockD;
     public Sprite blockRD;
     public Sprite blockLD;
     public Sprite blockUD;
     public Sprite blockU;
     public Sprite blockUR;
     public Sprite blockUL;
     public Sprite blockULR;
     public Sprite blockRLD;
     public Sprite blockULD;
     public Sprite blockURD;
     public Sprite blockUDRL;
     [Space]
     public Sprite blockDLC;
     public Sprite blockDRC;
     public Sprite blockURC;
     public Sprite blockULC;
     [Space]
     public Sprite blockUDRC;
     public Sprite blockUDLC;
     public Sprite blockURLC;
     public Sprite blockDRLC;
     [Space]
     public Sprite blockUDRLC;
 
 
 
 
 
 
 
 
     public override void RefreshTile(Vector3Int position, ITilemap tilemap)
     {
         for (int yd = -1; yd <= 1; yd++)
         {
             Vector3Int location = new Vector3Int(position.x, position.y + yd, position.z);
             if (IsNeighbour(location, tilemap))
                 tilemap.RefreshTile(location);
         }
         for (int xd = -1; xd <= 1; xd++)
         {
             Vector3Int location = new Vector3Int(position.x + xd, position.y, position.z);
             if (IsNeighbour(location, tilemap))
                 tilemap.RefreshTile(location);
         }
     }
 
     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
     {
         tileData.sprite = block;
 
         Vector3Int up = new Vector3Int(position.x, position.y + 1, position.z);
         Vector3Int down = new Vector3Int(position.x, position.y - 1, position.z);
         Vector3Int left = new Vector3Int(position.x - 1, position.y, position.z);
         Vector3Int right = new Vector3Int(position.x + 1, position.y, position.z);
         Vector3Int upRight = new Vector3Int(position.x + 1, position.y + 1, position.z);
         Vector3Int downLeft = new Vector3Int(position.x - 1, position.y - 1, position.z);
         Vector3Int upLeft = new Vector3Int(position.x - 1, position.y + 1, position.z);
         Vector3Int downRight = new Vector3Int(position.x + 1, position.y - 1, position.z);
 
 
 
 
         if (IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockUD;
         }
         else if (IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockU;
         }
         else if (IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockD;
         }
         else if (IsNeighbour(left, tilemap) && IsNeighbour(right, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap))
         {
             tileData.sprite = blockRL;
         }
         else if (IsNeighbour(left, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
         {
             tileData.sprite = blockL;
         }
         else if (IsNeighbour(right, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockR;
         }
         else if (IsNeighbour(up, tilemap) && IsNeighbour(right, tilemap) && !IsNeighbour(upRight, tilemap) && !IsNeighbour(left, tilemap) && !IsNeighbour(down, tilemap))
         {
             tileData.sprite = blockUR;
         }
         else if (IsNeighbour(up, tilemap) && IsNeighbour(left, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(down, tilemap))
         {
             tileData.sprite = blockUL;
         }
         else if (IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(downLeft, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
         {
             tileData.sprite = blockLD;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(downRight, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockRD;
         }
         else if (IsNeighbour(up, tilemap) && IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockUDRC;
         }
         else if (IsNeighbour(up, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap))
         {
             tileData.sprite = blockUDLC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap))
         {
             tileData.sprite = blockURLC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap))
         {
             tileData.sprite = blockDRLC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(upRight, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockURC;
         }
         else if (IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(upLeft, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap))
         {
             tileData.sprite = blockULC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(downRight, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
         {
             tileData.sprite = blockDRC;
         }
         else if (IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(downLeft, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
         {
             tileData.sprite = blockDLC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(upLeft, tilemap) && IsNeighbour(upLeft, tilemap) && IsNeighbour(downRight, tilemap) && IsNeighbour(downLeft, tilemap))
         {
             tileData.sprite = blockUDRLC;
         }
         else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(downRight, tilemap) && !IsNeighbour(downLeft, tilemap))
         {
             tileData.sprite = blockUDRL;
         }
         else
         {
             tileData.sprite = block;
         }
 
 
     }
 
     private bool IsNeighbour(Vector3Int position, ITilemap tilemap)
     {
         TileBase tile = tilemap.GetTile(position);
         return (tile != null && tile == this);
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check to see if Switch Case is true. 1 Answer

If statements with AudioListener C# ? 0 Answers

I would like to return the Z block, if z block is chosen then, the user should have 3 chances to change the block 0 Answers

Not going through my switch case on scene loaded. 0 Answers

OnGui() Causing lag, am I doing it wrong? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges