Hi there,
I’ve jumped out of the window a couple times trying to make an assetbundle with an editor tool I’ve created. It works like this:
User inputs a path for a texture.
Tool imports texture and creates the asset.
Tool creates gameobject with a mesh, material and stuff and adds the texture to it.
Tool creates a prefab from that object using PrefabUtility.
Up to here it works like a charm, everything is imported correctly, assets are created using AssetDatabase.CreateAsset and I get to instantiate the prefab to use it in editor. Now I want to create an assetbundle with all those objects (I have all the references to them stored in an array) but I don’t know how to do that with the prefab. I Know I have to use an AssetBundleBuild as stated in the docs here, and that’s what I do:
private void GenerateAssetBundle(UnityEngine.Object[] selection)
{
string[] assetNames = new string[selection.Length];
int i = 0;
foreach (UnityEngine.Object o in selection)
{
assetNames[i++] = AssetDatabase.GetAssetPath(o);
}
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
buildMap[0] = new AssetBundleBuild();
buildMap[0].assetBundleName = mBundleId.ToString();
buildMap[0].assetNames = assetNames;
BuildPipeline.BuildAssetBundles("Assets", buildMap, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows64);
}
So selection is the array that contains all the references to the objects I want to include in the assetbundle, but When I create the assetbundle I get the error:
As the prefab is the one that creates the problem I’m trying to figure out how to add it to the assetbundle. I create it the old way:
Holy cow, just tried loading the damn assetbundle and it creates the instance perfectly. What the hell is going on here? Why do we get the error when creating the assetbundle?
I switched to the old assetbundle generation system (using the functions from 4.x) and tried to store the parent folder in the assetbundle (to avoid getting the error) and instead of the error I get the warning in this thread. Both ways (adding the prefab or the folder) generate an assetbundle with the prefab inside it. Why the error? Why the warning? No idea yet.
If you’re using PrefabUtility.CreateEmptyPrefab(path) followed by PrefabUtility.ReplacePrefab Unity seems to have broken that combination in the new build system, leaving you with an empty prefab (which I think is what causes the “Editor only objects cannot be included in AssetBundles” message).
I was trying to make an asset containing a new prefab based on on old prefab (not something everyone needs to do, but we had need for it, I’m relaying what we’re doing here but it may be more steps that most people need to do). I found I needed to instantiate the original prefab as a game object and then create a prefab from the instantiated object in order to successfully generate an asset bundle.
AssetDatabase.ImportAsset( origPrefabPath , ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ImportRecursive );
var originalPrefab = AssetDatabase.LoadMainAssetAtPath( origPrefabPath );
var go = Instantiate( originalPrefab ) as GameObject;
go.name = desiredGameObjectName;
UnityEngine.Object prefab = PrefabUtility.CreatePrefab( newPrefabPath , go );
var newAsset = AssetDatabase.LoadMainAssetAtPath( path ); // Note: not sure if this line is needed for successful exporting, our system loads the main asset here for other reasons and I'm including it here because it could well be required for Unity to properly initialize the prefab
string outputFolder = System.IO.Path.GetDirectoryName( outputPath );
string outputFilename = System.IO.Path.GetFileName( outputPath );
AssetBundleBuild [] descriptor = new AssetBundleBuild []
{
new AssetBundleBuild()
{
assetBundleName = outputFilename,
assetNames = new string [] { newPrefabPath }
}
};
BuildPipeline.BuildAssetBundles( outputFolder , descriptor , BuildAssetBundleOptions.None, mBuildTarget );
Is it possible to setup an Editor script generated prefab with the assetbundle name etc like you would do if doing it manually via the asset inspector but using an Editor script (Eg ScriptableWizard)? Bugger if I can see how.