Search Unity

SaveAsPrefabAsset took too long when used in OnPostprocessAllAssets

Discussion in 'Prefabs' started by Tomwwy, Oct 6, 2018.

  1. Tomwwy

    Tomwwy

    Joined:
    Oct 6, 2018
    Posts:
    2
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6.  
    7. public class importtest : AssetPostprocessor
    8. {
    9.  
    10.     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    11.     {
    12.         foreach (string assetPath in importedAssets)
    13.         {
    14.             GameObject go1 = (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));
    15.             string localPath = "Assets/Prefabs/" + go1.name + ".prefab";
    16.             GameObject instantiatedgo = PrefabUtility.InstantiatePrefab(go1) as GameObject;
    17.             PrefabUtility.UnpackPrefabInstance(instantiatedgo, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
    18.             Object prefab = PrefabUtility.SaveAsPrefabAsset(instantiatedgo, localPath);
    19.         }
    20.     }
    21. }
    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:
    upload_2018-10-6_14-0-46.png

    So did I miss something? Does anybody know why this is happening? Thanks!
     
    Last edited: Oct 6, 2018
  2. Tomwwy

    Tomwwy

    Joined:
    Oct 6, 2018
    Posts:
    2
    Okay I think I find the reason, OnPostprocessAllAssets is also called when you save a prefab, so it's called recursively:p