Best way to change the shape of an object at runtime
Helo guys.
I am new to unity. I want create a simple 2d game. One of the entities has to grow and shrink. This is done by merging simple shapes. A rough example in the picture below just to show what I mean.
It grows by adding components and shrinks by removing them.
There will be a lot of entities on the screen so the performance is very important. Is it possible to change dynamically the shape of one gameobject? If not, which of the following solutions is better:
Constantly add new gameobjects (shapes) to the previous one and then remove them?
Create an animation. In this case is it possible to change the animation speed at runtime so for example first it grows faster and then grows slower or shrinks? My issue is whether the change of speed will apply to the whole loop of the animation or is it possible to apply it in the middle (so that the speed of shrinking and growing is different)? I would prefer the latter to happen.
If you have any other suggestions I'd be glad to hear them.
Answer by Taxen0 · Oct 30, 2015 at 07:56 PM
Not exactly sure how you want this to happen, but if the objects should appear at the same place all the time you can make child objects and simply enable/disable them.
For example: the default state, the big circle is the parent object. And the smaller circles and rectangle would be their own child objects that you can enable by script when you want the object to grow, and of course the other way around.
If the player should be able to add different objects wherever they want a more advanced system would be required.
Hope this helped a bit!
$$anonymous$$y apologies, the description was not detailed enough. Firstly, thank you for your answer, it's very helpful.
The objects will not be placed in the same place all the time, they will move (but each child will still be attached to the same "parent" object).
The growth of the whole entity is dependent on a variable (i.e speed) that changes at runtime. Is there an easy way to provide the object with this variable as a property so that it would grow and shrink with a speed matching it?
The target platform is Android and I am not sure of the performance if there will be let say 10 gameobjects in the scene, each having a couple hundred children?
The target platform is Android and I am not sure of the performance if there will be let say 10 gameobjects in the scene, each having a couple hundred children?
$$anonymous$$ake a dummy test with 10x200 rendered game objects and measure performance with the profiler or the internal profiler. If the test is too slow, try to figure out how to make it faster. Look at where time is spent and tackle the biggest issues first.
Edit: There are lots of different android devices, with varying performance and capabilities. Testing performance on one specific device will give you an idea if the game will run on that device or better devices. However, if you are going to be serious and release a game on the market you should make the performance tests on the lowest perfor$$anonymous$$g android device that you want to support, to ensure that the lowest hanging fruit can play the game.
Thank you for your suggestion.
The problem is not only with the amount of objects, but with the fact that they will also all have to move constantly. As I said in the comment below, their growth/shrinking behavior will be triggered by some events. Having GameObjects with children would require enabling/disabling them very frequently; how will this affect the performance?
Their behavior will be rather repetitive: each entity will grow and shrink the same way throughout the game.
I will test it but if you have any more suggestions I'd be glad to hear them too.
I guess there are a few ways to do it depending on how dynamic you need the changes to be.
If the objects should change shape in a set pattern I would probably do an animation and loop that. Or jump to the different frames when you need to change state.
Should you need more control and change the object to all kinds of crazy shapes depending on user input or something, so it would be too much work to make a seperate image for each state imaginable, a setup where you enable / disable the child objects as needed would probebly be better.
Try to explain what kind of game it is and how it works if you need more help. :)
The changes won't have to be very dynamic.
It doesn't depend on the player in any way -- each entity always grows and shrinks the same way repeatedly (kinda like in a loop). This behavior (growth and shrinking) is triggered by in-game events but normally the entities are idle.
Then you could simply make a animation, like a sprite and jump to the frame that have the look you want when you want it to change.
I recently started with mobile development myself so i have no clue regarding many objects and performance. But the sprite should be nice and quick.
Your answer
Follow this Question
Related Questions
2D Sprite animations flicker on first run in Unity 5.3 13 Answers
Can't seem to get my animations to work proper 1 Answer
Select Object And Spawn it 1 Answer
Unity 5 Animator hides Sprites on Android [Strange] 0 Answers
Help with 2D Enemy Animations 0 Answers