I am trying to write a simple 2D game in unity. The basic concept is dragging circular "tokens" into other circular "holders". The holders are situated in rows on the upper ~65% of the screen, and at the lower part of the screen is a sort of "HUD" where the tokens are instantiated for the first time (so the tokens are dragged from the bottom HUD to the holders at the top)
I should also note that the holders are arranged in rows, and I want there to be X number of holders per row and Y number of rows.
The easiest and most straightforward way to do this seemed to be using Unity's UI and canvas. This was because I also thought it would be nice to have the entire game, including sprites, rescale if the window were to be rescaled since canvas can scale with screen size.
I created a scrollview
to hold the rows of holders
. Each row was a panel
with a horizontal layout group, so adding sprites to it was easy because they would automatically get centered (the sprites had to be nested inside of a blank "image" object in order to be placed on the panel correctly).
This caused a lot of problems - while the panel
s interacted nicely with the mask of the viewport
in the scrollview
, the sprites did not. Adding separate sprite masks also didn't help, and for some strange reason messed up with the drawing order, causing the sprites to be rendered behind the UI. I made sure to select visible inside mask
in the mask interaction
section of the sprite renderer.
My main question: Is my approach reasonable, is it OK to mix sprites with UI and render them on the canvas?
If not, what would be another way of doing this? Would I have to add scrolling and positioning of sprites myself, through scripting?
Sprites can be used in the ui system, through the Image component.
I would avoid using sprite renderers in conjunction with the ui in the way you described.
It can be done, but will undoubtedly cause headaches.
To do everything in the UI system, just use the Image component on a GameObject.
Set the Images sprite property to your sprite, done and done.
It can be confusing, since you can accomplish the same/similar things with both (albiet more work to accomplish certain things in one tech vs the other).
Take some time to go through unitys manual for both topics. Unity 2D and uGUI