- Home /
Drawing interconnected sprites among the line in 2D.
Hello.
Imagine if you want to draw a top-down 2D road. You click or touch to your game and then drag your cursor do draw a line. Instead of line, sprites of road will appear. When your imaginary line will intersect, there will be crossroads. When your line is not straight, roads must be curved.
I thought instantiating new gameobjects with game sprites and then correct them via other script. However, having a lot of gameobjects is very harmful for perfromance.
Is there a method to add multiple sprites and correct them in one GameObject?
Is there any solutions for Unity about this in 2D?
Answer by Drakonno · Aug 21, 2016 at 12:29 PM
Hello,
Problem is not how to do it in Unity, but how can You do it overall. I have few tips, I hope. :)
Few questions about performance: Aren't You instantinating tousands of objects (every pass of loop while holding a button)? Are You using some kind of "spawner" (object pooling)?
And I assume You wan't behaviour like in Cities:Skylines. There is first transparent, single object, and is just scaled along the way, and when You release button, road is placed. You can achieve that using three sprites? Single road, crossroad and one, created manually with for example ax^2+bx+c equation to make curves.
I thought just first draw line, then instantiate sprite GameObjects (proportionally) among the line, then remove the line. Is there any better methods? Is it possible to draw multiple sprites inside one GameObject?
You can create new gameobject with "sprite" component and attach such object to another empty one. For example with such hierarchy:
Object_House:
-Sprite_Roof
-Sprite_Doors
-Sprite_Body
--SubObject_Windows:
---Sprite_LeftWindow
---Sprite_RightWindow
And to be exact, You can't add another "Sprite" component to a gameobject, which have one already.
I will mention it again. If You have lags while instatinating objects, You are doing something wrong, or Your hardware, or drivers. I can normally instantinate multiple objects without lag, use them and play around.
Your answer
Follow this Question
Related Questions
Question on Sprite and Movement 0 Answers
Why do they hide behind the tiles? 1 Answer
Texturing custom 2d sprite 0 Answers
Handling Multiple Resolutions in 2D? 0 Answers
Top Down Sprite Facing Mouse 2 Answers