New assets can not be imported by script

Hey folks,

we build our scenes as AssetBundles. The export script loads the scene, does some customization to the scene, gets the root GameObject and creates an AssetBundle from it.

Because we create some new GameObjects during the customization of the scene we have to create them as assets and save them to file, because Unity can’t build objects that aren’t located on disk.

So in short the script does

  • load the level
  • customizes the level
    – creates new GameObjects
    – saves every GameObject as asset using AssetDatabase.CreateAsset()
  • gets the root GameObject and builds an AssetBundle from it using BuildPipeline.BuildAssetBundle()

Now this works. But, we get lots of these errors: “Inconsistent asset when sorting preload assets: ‘’ fileID: 0”

We get this error because we’ve created some assets (customization of the scene) but Unity hasn’t imported them. Also when we force refreshing / importing assets it doesn’t work since somehow it doesn’t import the new assets. It only works when we refresh ALL assets using ImportAssetOptions.ForceUpdate, but you can image that this takes loooong.

So when we load this AssetBundle it sometimes crashes Unity complete.

Does somebody has any idea how we can solve this problem?

Can you post the code you are using to create the assets and asset bundles?

Yes, of course:

public static void CreateSelectedTrackAssetBundle( )
{
	Object[] listSelectedObject = Selection.GetFiltered( typeof( Object ), SelectionMode.Assets );
	if( listSelectedObject == null || listSelectedObject.Length == 0 )
		return;

	// save current scene
	string prevScene = EditorApplication.currentScene;

	EditorApplication.SaveCurrentSceneIfUserWantsTo( );

	for( int i = 0; i < listSelectedObject.Length; ++i )
	{
		string source = AssetDatabase.GetAssetPath( listSelectedObject[i] );
		string sceneName = System.IO.Path.GetFileNameWithoutExtension( source );

		CreateSceneAssetBundle( source, m_dirBuildPipeline + Game.FOLDER_ASSETS_TRACKS, sceneName );
	}

	// restore prev scene
	EditorApplication.OpenScene( prevScene );
}


EditorApplication.OpenScene(sourceFile);
    string sceneName = System.IO.Path.GetFileNameWithoutExtension(sourceFile);
    GameObject sceneRoot = GameObject.Find(rootname);

    if (sceneRoot != null)
    {
        // delete existing assetbundle
        AssetDatabase.DeleteAsset(destDir + "/" + sceneName + ".unity3d");
        System.IO.File.Delete(destDir + "/" + sceneName + ".unity3d");

	System.IO.Directory.CreateDirectory( m_dirTemp );

        // create a dummy prefab
        UnityEngine.Object prefab = EditorUtility.CreateEmptyPrefab("Assets/Prefabs/" + sceneName + ".prefab");
        GameObject gamePrefab = EditorUtility.ReplacePrefab(sceneRoot, prefab);
            
        //collect lightmaps
        Texture2D[] lightmaps = LightMapSaver.getInstance().collectLightMaps();
           
        // build assetbundle
        bool res = BuildPipeline.BuildAssetBundle(gamePrefab, lightmaps!=null?lightmaps:null, destDir + "/" + sceneName + ".unity3d",
            BuildAssetBundleOptions.CollectDependencies);

        // remove dummy prefab
        string prefabPath = AssetDatabase.GetAssetPath(prefab);
        UnityEngine.Object.DestroyImmediate(gamePrefab, true);
        //UnityEngine.Object.DestroyImmediate(prefab, true);
        AssetDatabase.DeleteAsset(prefabPath);

	System.IO.Directory.Delete( m_dirTemp, true );

        if (!res)
            Debug.LogError("Error packing AssetBundle");
        else
            Debug.Log("Build AssetBundle succeeded.");
    }
    else
    {
        Debug.LogError("The Scene contains no object: '" + sceneName + "'");
    }
}

The lightmap code is new, so it doesn’t has something to do with the crash since the crash is older.