I’m trying to create a prefab from a model in OnPostprocessModel. The prefab gets created and the model hierarchy is added but the Meshes in the Meshfilters are all set to None. Can anyone explain this behaviour?
What I do is basically this:
public void OnPostprocessModel(GameObject go) {
PrefabUtility.CreatePrefab("Assets/test.prefab", go);
}
,
I had this same problem just today - surprisingly close to you!
I figured you have the save the prefab’s meshes again.
This fixed it for me:
MeshFilter[] mfs = toPrefab.GetComponentsInChildren<MeshFilter>();
foreach(MeshFilter mf in mfs) {
Debug.Log("Saving mesh: "+mf.name +"_M" );
char[] invalids = System.IO.Path.GetInvalidFileNameChars();
String newName = String.Join("_", mf.name.Split(invalids, StringSplitOptions.RemoveEmptyEntries) ).TrimEnd('.');
AssetDatabase.CreateAsset(mf.mesh, "Assets/TempPrefabs/SavedMesh/" + newName +"_M" + ".asset");
}
PrefabUtility.CreatePrefab("Assets/TempPrefabs/Resources/"+toPrefab.gameObject.name+".prefab",toPrefab.gameObject,ReplacePrefabOptions.ReplaceNameBased);
public Transform prefab;
void Start() {
Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
}
reference this
Any references to game objects or meshes will become invalid after the import has been completed. Thus it is not possible to create a new prefab in a different file from OnPostprocessModel that references meshes in the imported fbx file.
Please try the OnPostprocessAllAssets method.
static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string assetPath in importedAssets)
{
if (assetPath.Contains("Your target asset path") && System.IO.Path.GetExtension(assetPath) == "Your target asset extension")
{
var go = (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));
PrefabUtility.CreatePrefab("Assets/test.prefab", go);
}
}
}