Hi, let me present you the situation:
I need to import in .fbx format some geometry I have generated. As we know, we can’t use object with more than 65536 vertices. So, I split my original object into smaller ones, and I want to make a prefab with all of them.
And has it can be quite important: I’m a total newbie with Unity and with it’s scripting system, so I might be making enormous mistakes.
Substantially, my script works like this:
- search for newly imported objects which match my conditions
- make a treatment on them (position, rotation, parenting)
- create a prefab with all of this.
I’m actually getting random crashes on this script, despite the fact that I have written it again and again, trying to make it as efficient as possible.
Here is a overview of my js script :
class MyClass extends AssetPostprocessor
{
private static var myHasTable: Hashtable = new Hashtable();
private function OnPostprocessModel(imported_game_object : GameObject)
{
if(<the asset fits some conditions I check on it's assetpath>)
{
// Prevent the script from stopping if there is an error with an object
try
{
var prefab_part : GameObject = GameObject.Instantiate(AssetDatabase.LoadMainAssetAtPath(assetPath)) as GameObject;
myHasTable.Add(Path.GetFileNameWithoutExtension(assetPath), prefab_part);
}
catch(error)
{
}
}
}
private static function OnPostprocessAllAssets(importedAssets : String[], deletedAssets : String[], movedAssets : String[], movedFromAssetPaths : String[])
{
if(myHasTable.Count > 0)
{
var tempPrefab : GameObject = new GameObject();
var multiFilePrefab : Object = new Object();
for(var key in myHasTable.Keys)
{
var myTextReader : XmlTextReader = new XmlTextReader(Path.Combine("Assets", "multi_export_global_file.xml"));
myTextReader.Read();
while(1)
{
if(myTextReader.GetAttribute("name") == key)
{
myHasTable[key].transform.parent = tempPrefab.transform;
myHasTable[key].transform.position = Vector3(parseFloat(megf_reader.GetAttribute("xposition")), 0.0f, parseFloat(megf_reader.GetAttribute("zposition")));
myHasTable[key].transform.Rotate(0.0f, 0.0f, 180.0f);
break;
}
if(!myTextReader.Read())
{
break;
}
}
myTextReader.Close();
}
multiFilePrefab = EditorUtility.CreateEmptyPrefab(Path.Combine("Assets", "multi_file_prefab.prefab"));
EditorUtility.ReplacePrefab(tempPrefab, multiFilePrefab, ReplacePrefabOptions.ReplaceNameBased);
GameObject.DestroyImmediate(tempPrefab);
}
myHasTable.Clear();
}
}
Sorry if the code is not very clear, quite hard to make it clean on a forum interface.
To launch the script, I select the concerned assets in the project view and then right click → Reimport.
Sometimes if works perfectly, and I get my prefab created with all object at the right place.
Sometimes it’s quite a mess, the prefab is missing but I have all my object on a gameobject (so the one called “tempPrefab” in the script)
Sometimes object’s are missing on the prefab.
And quite often Unity simply crashes, with no error warning.
I can even have (just happened right now) the two opposite behavior with the same assets: worked perfectly the first time I launched the script, crashed the second one.
If I’m note clear enough, let me know it, I’ll try to make it easier to understand, or post a attached version of the script if needed.
Last thing I’ve just thought about: does the 65536 vertices limitation concerns the prefab too? I haven’t found nothing about it, hop it’s only about imported object themselves.