I'm making a house editor where users will be able to insert some objects into the house. So, I need to instantiate prefabs, but I don't know which prefab will be chosen by the user.
I have in mind to create a Dictionary where all prefabs will be listed. The user should be able to choose any of them and place them into the scene.
Do you think that is the best way to do this or would you suggest a different one?
Thanks a lot!
This sounds like a fine way of doing it. If there aren't too many objects, you could drag-assign all the prefabs into a basic public GameObject array in the inspector, and at runtime start, quickly transfer these to a dictionary of the form where the string is taken from the gameobject's name. Eg:
public GameObject housePrefabs;
private Dictionary<string,GameObject>houseObjects = new Dictionary<string,GameObject>();
foreach (GameObject obj in housePrefabs)
houseObjects[GameObject.name] = obj;
An alternate method might be to define a custom class for your house objects. You might need this if you find that you need more data to accompany the object besides the name itself (such as 'price', 'colourOptions', etc).
In this case, you'd probably still find a Dictionary structure useful, but you might want to have some kind of simple identifier as the key, and your custom object class as the value. The custom object class would then have properties such as "name", "price", "colourOptions", and of course "prefab" which would hold the link to the prefab itself.
If you go this far, you'll probably want to start looking at reading these object-related data from another file such as a text file or xml file.
Hope this helps get you started!
Use Prefab in your Game Here is the tutorial for that . Unity3D Notes: Instantiate Prefab in Your Game Tutorial . Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution …