|
Hi! 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!
(comments are locked)
|
|
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:
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! Also, if you want to key by something different than the GameObject name, you could add two public arrays to your MonoBehaviour "keys" and "prefabs" and then zip them up in Awake(): for (int i=0; i<keys.Length; i}}) { mDict[keys[i]] = prefabs[i]; }
Jan 14 '10 at 05:37 PM
Max Kaufmann
Thanks for the answers! For the moment, I think that this will be the best solution but, in the future, I will have a lot of objects and filling the array can be horrible. Moreover, I would like to add more objects without "re-fill" (I don't know if this word exists) the array every time I add one more. Are there any other way to create a Dictionary or something like this to do it?
Jan 18 '10 at 03:53 PM
SeRoX
Just a note than when you create an array and want to add prefabs in the Inspector; you must first set the size which will give you that many elements which you can drag prefabs onto.
Jul 17 '10 at 08:18 AM
Andre Odendaal
(comments are locked)
|
|
Use Prefab in your Game Here is the tutorial for that . http://lnkd.in/J6ufVr . Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution ..
(comments are locked)
|
