- Home /
Can I generate prefab instances based on locators inside a 3d model file?
I have quite a complicated spatial setup going on in one of the scenes i'm working on. At the moment I've placed them by hand, but as the number of elements increases and art changes continue occurring its growing frustrating having to do this manually.
I wonder if there is any accepted way of harvesting out certain elements from an imported 3d model and using their position and rotations to create the instances of a given prefab. I guess i'm talking about an editor script which could take the pattern of the names of locators to search the model for, and the prefab to create.
I don't know if actual modelling locators themselves are even imported as they usually don't contain any geometry. At the same time I guess I don't want to waste any time rendering anything that is only present to be a locator for this purpose.
Finally it'd be great if this is at all possible that subsequent imports could have the script update already created instances rather than creating new ones, otherwise the values set for script members on the instances would be lost and i'd have to start again with their setup.
Answer by Jessy · Jan 06, 2010 at 12:32 AM
I'm not sure if this matches what you need, but I use Blender as a level editor instead of Unity; what I do would work with any 3D app, however. This has gotten kind of complex, and I'm still working on it. (I don't even know what all I've added since I made the forum posts.) I will be putting a cleaned-up version on the Wiki, with complete documentation, once my next game is out, but feel free to check it out.
Working with Unity, and syncing assets, is kind of a freaking pain if you're not using prefabs, if you need your objects to have scripts applied. (Currently, trying to make a copy of a script and attach it to something else seems to be completely undoable, due to bugs, according to me and a few others who have tried.) But if you are using a lot of prefabs, and/or scriptless objects, I believe this solution will work out well for you.
You mention needing to have unique instance variables. Marking something as a "Clone" in this script can accomplish that. However, that takes away from the power of syncing in ways that I haven't even had time to come up with yet. It might be what you're looking for. Maybe run the complete sync once, then "Clone" all the instances, and set up their scripts as desired. Future syncs will keep these values, but update the transforms as dictated by your 3D app. Also, if you make any mesh edits, that will come through as well.
Personally, I have been just syncing my entire level, then running a Menu Item setup script afterwards that sets up all my unique instance variables. Whether that is appropriate for your use, I don't know.
I'm uploading my latest version right now. http://forum.unity3d.com/viewtopic.php?t=32551
thanks, i'll take a look when i'm next at the keys. Now i'm back at work its harder to find the time (and energy) for home dev!
Glad that i'm not the only one to come across these sort of dilemmas!
Answer by Stelimar · Jan 05, 2010 at 11:40 PM
This should be possible with an editor script, but I'm not familiar enough with how exactly Unity imports external models to give you a script. However, you should be able to somehow get a list of locators by giving them a specific name, and searching for objects with that name, or something similar.Then just iterate through the list with a for loop, Instantiate() a new object, and copy the position, rotation and scale of the locator.
I guess the question is whether locators even make it into unity to be searchable. I was thinking about this some more and wondered whether empty groups could work which i then place the instantiated prefabs into correctly. Then if the groups are moved in the model the prefabs SHOULD be. Thanks though.
I'm not sure, but you can always try it and see if it works.
Your answer
Follow this Question
Related Questions
Generate colliders on a prefab 0 Answers
How do I programmatically assign a GameObject to a prefab? 6 Answers
Changing Settings for Mesh Collider on Model does not update Prefab as expected? 0 Answers
Creating a complex prefab during asset postprocessing 1 Answer
Getting Blender scale to correspond with Unity scale 1 Answer