- Home /
Importing a model without it becoming a prefab
This may be a stupid question (I don't even know if anyone else has it), but is there a way to import models into the game without them directly turning into prefabs?
Very often I need to add scripts to a model, but I cant, because it's a prefab, and for some reason I can't add scripts to a "model prefab" without breaking the link (and I can't use apply).
This wouldn't seem like a big problem ( i can make a new prefab and drag the broken into that one), but it gets really messy.
As always, any help is appreciated!
The question is O$$anonymous$$.. Just few notes: there is no such thing as a stupid question.. as long as it has to do with Unity.. and there is no need for asking for help.. we are all here to help and get help..!! more in http://answers.unity3d.com/faq Regards
@Lipis - at least he asked for help in the text - not one giant Title saying HELP!
Answer by Eric5h5 · Mar 31, 2010 at 04:05 PM
The prefabs you get when you drag in a model are not "real" prefabs. You can see that they have a different icon. The correct procedure is to make an actual prefab the usual way (Create -> Prefab) and drag the model onto that.
Hmm...I'm not sure why you think so, since the answer to your question about "is there a way to import models without them turning into prefabs" is basically "they never were actually being turned into prefabs", which seems to be what you wanted in the first place. You can drag the bare mesh into the editor if you don't want the "fake prefab" effect, but then you lose out on things like automatic texture assignment and orientation correction.
Answer by duck · Apr 01, 2010 at 09:08 AM
There's one other option here, which is to use the AssetPostprocessor scripting.
With this, you can programmatically add components to your models during the import process (including built-in components such as Colliders, as well as your own scripts).
In this way, you can actually end up with a model with a script attached to it, which you can use right off the bat. :-)
Here's an example C# script which adds an Enemy behaviour to any object imported which contains the word "enemy" in its name:
using UnityEngine; using UnityEditor; using System.Collections;
public class ModelPostProcessor : AssetPostprocessor {
void OnPostprocessModel (GameObject g) {
Apply(g.transform);
}
void Apply (Transform transform)
{
if (transform.name.ToLower().Contains("enemy"))
{
transform.gameObject.AddComponent<Enemy>();
}
// Recurse
foreach (Transform child in transform)
{
Apply(child);
}
}
}
Answer by duck · Mar 31, 2010 at 03:37 PM
I think your problem here is that you're thinking that making a new prefab from the "broken" one is "messy", wheras it's actually a very powerful tool which doesn't need to be messy at all - it's one of the fundamental ways that developing in unity works, and in fact what you're doing is creating a new game-ready functional prefab based on that imported model.
If you're finding it messy, you probably just need to work out a way of organising your original imported 3d assets in your Asset folder so that they are separated from your functional prefabs that you use in your game. You can do this by creating folders in your project pane.
For example, you might have "enemy.fbx" which contains your enemy model. You'd then want to create a functional prefab based on this model. So first, you'd drag the enemy model into your hierarchy (which adds it to the current scene). You'd then add any relevant scripts - such as a script which controls its behaviour, and maybe other separate scripts which might perform global actions like allowing it to show up on a radar, or allowing it to be a valid targetable item, etc.
You'd probably also want to add a collider and perhaps a rigidbody component if you're using the physics engine to move it around. You might also want to add things like a particle system for its engines, or wheelcolliders if it's a wheeled character. It might need an audio source for any sounds that it should emit, etc.
In this way, you should be able to see that you start off with the basic model, and build up a fully functional Game Object by adding components to it.
Now - you create a new prefab, and drag your completed Game Object into this new prefab. This is now the "Enemy" prefab that you should instantiate in your game - either by manually placing them around your levels, or by dynamically instantiating them at runtime via some kind of enemy manager script.
I hope this helps!
I wouldn't have to organize half as much if I could make a prefab right of the bat with assets that I only have one specific use for. cheers for the one page answer though
I've added another answer. This answer still holds (this is the way you're supposed to use prefabs in unity), however there's a editor-scripting method you can use to add scripts directly to models as they are being imported.