- Home /
How prefab the instance works?
Hi guys, let's say I have built a entire city made of different blocks.
Each block is a prefab.
Each block has some buildings that
are also prefabs or just models.
When in game I don't want the entire city loaded, only the blocks around the player will be alive. Here is my doubt, Unity does not have until now a "Prefabs of Prefabs" system, when I instantiate a new different block that has some models that are already used in other prefabs does Unity knows that these are the same ones?
I must know because I'm aiming for mobile, and there can be no waste of RAM/VRAM.
Thanks in advance.
Yes, Unity should know that the objects are all clones of one prefab (thus if you change the prefab, the clones will change, too). But I'm not sure that you can affect all prefabs at the same time in-game.
Thanks for the answer @sparkzbarca, can you post ir as a real answer?
I was forgetting the basics from my C++ days, unity for sure knows that X mesh and Z texture are already in the memory and don't reload them :)
@sparkzbarca please post your comment as an answer so I can mark it as the correct answer.
Answer by sparkzbarca · Jan 24, 2013 at 09:40 PM
yes if you have a prefab that is
Prefab A + prefab B
and another that is
Prefab A + prefab C
of course it recognizes the connection between A in each. As long as you make sure your actually using the prefab in both.
if in the hierarchy you have it as A and B for one. A and C for the other. It will only have 1 instance of A in memory.
That is because a prefab is meaningless to the computer, its instituted for us not for the computer. To the computer prefabs dont exist, it just says draw A here and B here and A here and C here and has no need to group A and B together.
Basically we have difficulty with doing the same thing over and over again and perfectly every time. We make mistakes and forget.
But a computer sees no need for a prefab. A prefab is really a relationship formed between multiple objects as it relates to there position/scale because remembering to set each position and each scale everytime would be tedious and difficult. Writing spawn prefab A is alot easier to do and get right everytime than spawn object A then spawn object B so far away. Then scale A to B by so much, and doing that every single time.
But that is in fact what a computer does. It replaces your prefab with lots of tiny little instructions like that. Cause it has no problem repeating those instructions over and over again perfectly even without a frame of reference to compare it against.
Your answer
Follow this Question
Related Questions
How can I instantiate two prefabs at different times and positions ? 1 Answer
Instantiate Pre-fab clone altered on Instantiate 2 Answers
Different prefab getting instantiated? 1 Answer
Replace instantiated objects in an array 1 Answer
Instantiate prefabs working in editor but not in builds 0 Answers