Create new asset from code (save UObject to asset)

How can I save some existing UObject to .uasset file from code? I want to do: Instantiate an object of UMyClass, fill it with data and then save it as .uasset.

Is something like this or similar possible?

Thank You for any help.

PS. The reason I want to do this is that i want to be able to create files in editor (from code) and make sure those files are then included in packaged game. Currently, the only files being packaged are .uassets (without modifying settings, but that’s something I don’t want to do since it’s a plugin)

Without digging deep into code I can suggest you to look for UDataAsset class. You can inherit your class from it and then you will be able to create instances of it with Editor.

That’s the problem: I can’t create the asset from editor (there is a lot of data). It must happen from code.

Well, in such case you may look into SContentBrowser::NewAssetRequested method to know how to create new asset (it is called when you create new asset by right clicking content browser).
And then you may look into FAssetEditorToolkit::SaveAsset_Execute method to know how to save the asset after modifying it (it is called when you press Save button inside Asset Editor window).