Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by JakeManfreDev · Jul 09, 2018 at 02:01 AM · ui2d gamecanvastilemap

Is making a 2d tile based game all in UI a bad solution or is it viable?

I'm making a 2D mobile game in which the entire game view is contained within the screen. There is no scrolling. The game is also grid based in that, depending on the difficulty of the level, the playable area will be made up of a certain number of square tiles that all fit flush within the screen. If I make everything based under a canvas then I can easily position things so that they are aligned next to each other as rect transforms are based off of pixels and I can calculate how wide the tiles need to be based on the screen resolution so that it gets filled to the edges of the screen.

Is this a bad approach? I looked into tile maps but I don't know how to align them with screen space so that the edges are flush with the sides and the top, or flush against a UI element like a top menu bar. Is there a way to do that and would it be better than doing everything under a canvas? Is there an alternative solution that I didn't mention or is making everything in UI a fine solution?

Comment
Add comment · Show 2
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
avatar image ShadyProductions · Jul 09, 2018 at 08:35 PM 0
Share

You'll end up with a lot of rects / images whatever you want to use for the tiles. This might cause a lot of performance issues due to the amount of rendering. Which you wouldn't get if you went the $$anonymous$$esh route.

avatar image JakeManfreDev · Jul 10, 2018 at 12:03 AM 0
Share

@shadyproductions how would you go about the mesh route? I need to align the game view with ui elements and the game view has to be divided up into square tiles that fit flush against the sides of the screen and a menu bar at the top which is a UI element. Are you saying to convert between pixels and units in unity? I've tried that in the past but its kind of a pain. Not sure of an easy way to do that.

there won't be too many ui elements cause the bulk of the tiles will just be one sprite with a tiled texture and I'll just use the tiling component on that texture. There will be a few other objects in the scene but it shouldn't be more than a few dozen I'd say. I've tried doing meshes in the past but I'm just not sure of a good way to convert between pixels and unity units so that I can easily size them through code at the generation of each level.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DrZarqawi · Jul 09, 2018 at 08:07 PM

Depending on what you need this can lead to some issues. But from the way you describe it i see no issues with that approach.

Alternative solution would be to get Screen.width and Screen.height on startup and then use those numbers to calculate size and position of your objects. Assuming of coarse the screen resolution don't change during run-time.

Comment
Add comment · Show 2 · Share
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
avatar image JakeManfreDev · Jul 10, 2018 at 12:06 AM 0
Share

@DrZarqawi what kind of issues can it lead to and what does it depend on exactly?

You say get the width and height at start and use that but that just gives you the dimensions in pixel but if I were to use a game object that uses transform and not rect transform, then how do you convert between the two? There seems to be no easy way to do that unless I just haven't looked in the right places? Is that a good solution to convert between the two and if so how exactly do you do that?

avatar image DrZarqawi JakeManfreDev · Jul 10, 2018 at 02:27 AM 0
Share

Collisions would be one issue, that the Canvas wouldn't be able to handle too well.

To accomplish the alternative solution you would need to be very structured with your assets.

  1. Decide on a base pixel to units ratio (default 100 px.per unit).

  2. $$anonymous$$ake sure all sprites sizes are the same size

  3. Then decide how many tiles fill the screen in each direction.

  4. Divide Screen.size.x with X amount of tiles and divide Screen.size.y with Y amount of tiles. Lets call that CellSize.

  5. Then lets scale the CellSize to fit the screen, (1/PixelToUnitRatio) * CellSize.x;

    (1/PixelToUnitRatio) * CellSize.y;

  6. Set your tile objects localScale to the scaled CellSize

  7. When positioning your objects remember to offset by half a scaled CellSize to each axis relative to the grid position, since its pivot is in center of the object.

Example:

Positioning at coordinate (3,4):

 //Gridsize is defined elsewhere as a Vector2Int
 
 Vector2 topLeft = new Vector2(scaledCellSize.x * (gridSize.x * .5f), scaledCellSize.y * (gridSize.y * .5f))
 Vector2Int gridPosition = new Vector2Int(3,4);
     Vector2 position = new Vector2(((scaledCellSize.x * .5f) + (gridPosition,x * scaledCellSize.x)) - topLeft.x, ((scaledCellSize.y * .5f) + (gridPosition.y * scaledCellSize.y)) - topLeft.y);
 



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

162 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

Related Questions

Unity 5.2 Canvas component makes the position not to update 2 Answers

Custom Cursor hidden behind my UI 2 Answers

Unity 2D Combat Text Issue With Multiple Enemies 2 Answers

Align Canvas with Hex tilemap 0 Answers

post processing stack with particle system. 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