Hi,
I want to save imported models as prefabs, so I use SaveAsPrefabAsset inside OnPostprocessAllAssets,
basically I want to load the model through AssetDatabase.LoadAssetAtPath, and then InstantiatePrefab(I can’t directly save the loaded game object, because it says you can’t save persistent game object. But why?)with the loaded model(game object), and then save it through PrefabUtility.SaveAsPrefabAsset,
code as follow:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class importtest : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string assetPath in importedAssets)
{
GameObject go1 = (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));
string localPath = "Assets/Prefabs/" + go1.name + ".prefab";
GameObject instantiatedgo = PrefabUtility.InstantiatePrefab(go1) as GameObject;
PrefabUtility.UnpackPrefabInstance(instantiatedgo, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Object prefab = PrefabUtility.SaveAsPrefabAsset(instantiatedgo, localPath);
}
}
}
So I tried to import some simple models(like a cube.fbx), but it took too long to save them as prefabs, there is a message box staying at “hold on” state with the prefab it’s creating:
So did I miss something? Does anybody know why this is happening? Thanks!