- Home /
Load Sprite and Prefab without Resources.Load
Hey guys!
I'm struggling a lot with my inventory system. I tried to optimize it when I found this article: https://unity3d.com/de/learn/tutorials/topics/best-practices/resources-folder
What I want to do is simply load a sprite and a prefab for my items into the scene. Here is an overview how I designed my item database:
Each item has a specific folder. Inside there is a .json file which contains the properties of this item. As well as a Sprite (for the inventory) and a prefab for the instantiation.
If you have a better idea for a database instead of folders, please present it here ;). I'd highly appreciate it!
But I don't want to have a plain array database*, since I want to implement quite a lot of items. Furthermore different item types have different properties, so only one database wouldn't be enough...
*By plain array database I mean something like: public Items[] itemDatabase;
Thanks in advance :D
Well, i personally like "plain array" usually when is too much stuff i just make some subclasses and set everything on tree data structure, it makes it fast and easier to search for specific objects latter.
Insted of looking into the items as a single thing see if you can separate them into categories and make this categories into a class or sub class that inherit of item, then make arrays for each category of item.
Then you can have a tree of items with the main nodes being the categories, and if possible or needed make some more sub nodes with some other attribute.
So if you have like 200 items but only 10 of them are heavy you won't need to look through the entire array of items.
Answer by FortisVenaliter · Jan 18, 2017 at 07:09 PM
Well, if you don't want to set up references before runtime, you'll need to use the resources folder.
What I've done in the past is:
Set up a database script that can be used as a prefab. Store the data however it makes the most sense for you.
Write an editor script that allows the items to be dropped into a field that adds it properly to the database.
Use Unity's custom serialization to bake that complex dataset down to a simple array for saving to the disk. When you deserialize it on load, take the array you get and filter it out to your complex dataset.
An alternate way would be to use resources, and have a simple text file asset that lists all the prefabs to load. Then you can Resources.Load each one at load-time and put them into the appropriate place in your database.
Thank you guys :D I highly appreciate your answers. I think I'll go with the new 'plain array' kind of thing. If I split it into smaller pieces it is indeed a good solution. If I find another alternative I will update this topic.
The other solution with the serialization may be too difficult for me for now, but I will give it a try when I am more confident ;)
Answer by Hirnfroster · Jan 21, 2017 at 06:50 AM
Thank you guys :D I highly appreciate your answers. I think I'll go with the new 'plain array' kind of thing. If I split it into smaller pieces it is indeed a good solution. If I find another alternative I will update this topic.
Your answer
Follow this Question
Related Questions
Resources not loading in APK 0 Answers
Display Item Sprite on Inventory UI 1 Answer
Changing the sprite of a prefab with in a script? 1 Answer
Network.Instantiate prefab with added parts 1 Answer
Editor Works, Build Not 1 Answer