- Home /
Do bulk prefabs cause performance issues?
I'm making a 2D game and I want to be able to spawn in a large variety of different weapons (like 1,000+). Are prefabs the best way to go about doing that or should I put all the information for each weapon into an XML document and load that at runtime to dynamically create each weapon? I'm fairly new to Unity so I don't know if that's the norm but having that many prefabs seems heinous, although it would be much easier.
Thanks!
Bentley
Do you have different textures/sprites for each weapon? If so, how large are they? Is it a PC or mobile game?
This would be vital information for a good answer.
Each weapon has a different sprite sheet which contains multiple sprites for attachments, like a scope and a suppressor. The largest I have so far is 20.0 kb according to the Unity inspector. It is going to be a PC game, but I would like to keep it as light as possible as I will probably move it to mobile as well after it is finished. Also each prefab will have approximately 10 components attached (various managers and network scripts).
Answer by DaDonik · Jan 19, 2015 at 07:43 AM
If one of your weapons is as small as 20kb, that still means you have around 20mb in memory, if you want to show all 1000+ at once. Since you maybe want to have that game on mobile devices, 20mb's is quite large. I guess the weapons are not everything in your game, so it will get even bigger than that.
Assuming that you will have some kind of a weapons shop, you could make weapon categories and only show one category at a time. That way not all weapons will be in memory at the same time, which can make a huge difference on mobile devices. It might lag a bit when loading the next category, but with that amount of weapons you don't have much choices.
For PC only i would load all the weapons at once when loading the scene you use to display them, or at game start. This will of course, like abi.kr01 says, add to the loading time, but at least it won't lag while playing.
Regarding your question on prefabs vs. XML: XML might be more flexible when you want to change something later on, but the extra time spend parsing 1000+ XML's files will make it slower. It won't be much slower, at least not on PC, but somewhat slower. That said, i would probably use a SQLite database for this purpose.
I can recommend this plugin for database stuff on PC and mobile: SimpleSQL
If I were to do X$$anonymous$$L I would not have 1,000 files. I would have one file with a thousand tags and children tags like and . I would then hold just the file in memory and read from it as needed. I guess the main question I was trying to ask is whether or not prefabs require memory even if they are not instantiated, or if they only require memory after instantiation. I would not be displaying all of the weapons at once, only 50 or so max, which even then would be highly unlikely and infrequent. If they require memory prior to instantiation I would go with X$$anonymous$$L, as a single X$$anonymous$$L file will be much smaller than 1,000 prefabs, but if memory is only allocated after instantiation then I don't see any downside to the prefab option, as I'm not particularly worried about application size. Let me know if any of that logic isn't sound for some reason or another.
Thanks! Bentley
The prefabs would only increase your build size, until you instantiate them.
Your original question led me to the assumption you want to display all at once, dunno why...
To put it in a nutshell, do what you feel most comfortable with =)
Answer by abi-kr01 · Jan 19, 2015 at 05:44 AM
There are two ways you can do this either create an xml and create prefab at run time when you need them or you may use object pulling, all the items will we available on scene, no need to create new game-object.level loading will be slow in if you use object pulling.
Your answer
Follow this Question
Related Questions
Prefab with UI.Text is not unloaded 1 Answer
Hundreds of weapons, best practice 0 Answers
About Model Combine Optimization? 1 Answer
Huge prefab file sizes 0 Answers