- Home /
Programmatically Exporting an Asset Package
I would like to save the currently open scene file and all it's dependencies to a Unity Asset Package (.unitypackage) file from an Editor script.
It seems that this topic has been previously broached at http://answers.unity3d.com/questions/3237/export-files-to-a-unitypackage-file-from-c , but I am posting anew to include some more ideas on the subject.
Here's a screenshot from MonoDevelop which shows some interesting functions in Unity's dll file: http://img72.imageshack.us/img72/3380/screenshot20100501at556.png
And here's some code I wrote that serves to illustrate my purpose:
var guids : String[] = Array(AssetDatabase.AssetPathToGUID(EditorApplication.currentScene)).ToBuiltin(String);
UnityEditor.AssetServer.ExportPackage(guids, "/");
The code currently returns an "'AssetServer' is not a member of 'UnityEditor'" error when compiling.
Mike_Mac said that this was because the function I need to call is a private one, internal to Unity - but that I might be able to access it through reflection.
Anyone have some ideas on how this could be accomplished?
@Aubrey, remember to upvote good answers (and checkmark the correct one).
Hi, doing so would be very interesting! Have you ever managed to implement it?
Allright ladies and gentlemen ~ Unity 3 has come, and it has disabled reflection and added cool new API tools. I a more interested than ever in creating asset packages programatically, so please have at the bounty!
Bounty ends tomorrow, everyone's suggestions are welcome
this is still only possible with the PRO editions of Unity3D, right?
Answer by Lucas Meijer 1 · May 02, 2010 at 09:56 PM
Internal things in UnityEditor/UnityEngine are internal for one of a few reasons: - it doesn't work - it only works if you use it in a very obscure way - we want to be able to change it later without breaking your game - it has some sort of security risk - we didn't realize you might actually want it
That said, you can use reflection to try to call it, but you'd be walking on very thin ice.
Thanks for your reply! I wouldn't at all $$anonymous$$d Unity breaking my code some time in the future - this will be a nothing more than a component of an easy-to-distribute Editor plugin, and obscurity doesn't scare me :) I have no idea how to go about using reflection, so I may have to give up on the idea for now - but if anyone would like to point my researches in the right direction, I would be most grateful.
Answer by RickyAh · Feb 12, 2014 at 12:31 PM
Unity now provides a public API that allows creating a package from a collection of assets.
UnityEditor.AssetDatabase.ExportPackage
It does not automatically resolves dependencies though.
PD: I think this question should be marked as a duplicate of this one And the original question should also be edited to reflect the new changes.
Answer by Art 1 · Dec 31, 2010 at 05:00 AM
This might be cheesy, but what if you used Windows API functions (SendMessage and such)? Get the window handle and send the menu code that simulates you choosing the menu item.
It brings me great pain that something as powerful as the Unity editor would have to be controlled through windows API scripting - but you're right, this may indeed be our only option at this time.
Answer by ButtersGoog · May 31, 2016 at 07:08 PM
I also need to do an export, but I can't even use the internal one if I wanted to because it's broken anyway. For my native os x plugins, the file extension is .bundle (as expected by unity), and the exporter for whatever reason considers these .bundle files to be empty directories, and thus cannot be selected as part of the export. So I end up having to rename the file and hack the package that's exported. Such a pain!
I'm about to roll my own, it's not that complicated. The assets in the zip are not encrypted, the guids can be read from the .meta files, and the rest is pretty straight forward.
I'm not really dealing with scenes so my guids aren't really critical anyway, but if I had to generate a guid I don't think it would be the end of the world.
Your answer
Follow this Question
Related Questions
Export/Save procedural GameObjects with meshes 4 Answers
How to assign variables like Texture2D, TextAsset, Transform, etc. using code? 1 Answer
Get path of editor script? 2 Answers
Resources.FindObjectsOfTypeAll() doesn't return all Textures 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers