Ways to evaluate a programming technique to solve a problem? Ex: GameObject visibility
Hello there,
I am a beginner in programming with C# and Unity, so sorry in advance if it is weird silly talk :).
I am working on my first project, and i am trying to figure out how to evaluate which technique will be best for a given serie of problems. Here is an example:
I have "Boxes" which are all consisting of a group of UI Buttons, text, icons. My goal is to give the user the possibility to Hide/Show these different elements as he/she wants.
From these guidelines, i can think of 3 different ways to deal with this:
Hide/Show these elements by Activating/Deactivating the gameobject
Destroy the gameobject/ Instantiate it when i need it again
Set the object to transparent or opaque
The twist is that these boxes will be in a 3D space in which you can navigate, meaning they will be sometimes on screen and sometimes not. That has i guess its importance in order to determine which technique is best?
The question i have are:
Can I evaluate in advance which solution is going to be the most performance effective ? Is there some kind of conscensus which says that for example activate/Deactivate a game object is always slower than destroying instantiating it ?
Are there some lists about these conscensus somewhere ?
Are there other techniques in order to perform this task ?
I am guessing it is all about experience on working on different projects/tasks, though i find it difficult as a beginner to know where to find the informations. If it was not for the Unity community/ Stack Overflow and so on, i would be in real trouble, so thanks again all for being here and be so helpful =)
Note: i stumbled on the profiler thing in unity and will look into what it can do.
I have a question about the situation.
Are these 'Boxes' empty gameobjects, which means that their only purpose is to contain UI elements and to provide an option to hide all those elements at once?
If so, then i would choose the first solution.
Activating and deactivating a main gameobject is relatively easy, fast, and clean. The best part of it is that it will deactivate all of the child elements - which means that the UI elements will also be hidden. The only drawback is that these objects are not easy to be found, when they are deactivated. You should get some references of them before deactivating.
However, instantiating is different - it needs more calculation, which has an impact on the performance. There is nothing wrong with instantiating a few objects at runtime, like firing bullets and stuff, but you can not use it at excessive amounts.
For example, you can optimize a game by using an object pool, rather than instantiating more objects. Object pools store created objects, and inactivate them, when they are not needed. When you need another object (like another bullet), you can reactivate it using the object pool. Of course, it means that the objects the object pool is using are instantiated at the start of the game. (Note: you do not need that in this case, this is just a way of optimization)
Now, the third method is unclear for me. Do you mean, that those 'Boxes' are Cube gameobjects, and it has a $$anonymous$$aterial on it, which can be transparent or opaque? If so, i think this method is not the best option in this case, because it will not affect the UI elements, only the cube (as i know).
As you can see, these methods are for different kind of problems. If you want more information, i would like to know exactly, what your purpose is. Are you trying to create a menu with these, or is it only a test?
Your answer
Follow this Question
Related Questions
All unity games are black screen? 7 Answers
Low framerate on Android devices 0 Answers
What is the best practice for update functions in different scripts attached to same game object? 1 Answer
PostLateUpdate.FinishFrameRendering 2 Answers
any physical/practical limit to number of lights in view? 0 Answers