x


Instantiate dynamic Prefabs

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!

more ▼

asked Jan 14 '10 at 04:27 PM

SeRoX gravatar image

SeRoX
57 6 6 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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>();

void Start()
{
    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!

more ▼

answered Jan 14 '10 at 04:51 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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)
10|3000 characters needed characters left

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 ..

more ▼

answered Jul 13 '11 at 10:30 AM

vivek2012 gravatar image

vivek2012
-39

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x1253

asked: Jan 14 '10 at 04:27 PM

Seen: 3910 times

Last Updated: Jul 13 '11 at 10:30 AM