- Home /
How to save an asset
Hello,
I have written a script to save data set into unity universal asset format (.asset format) at runtime. This method works fine and all data within that .asset file were kept as long as the unity editor is running. But if I quit unity editor application and re-open this project, all data were lost. why?
Somebody please let me know how to successfully save that asset.
Thanks in advance
Answer by AYoung_PikPok · Apr 20, 2015 at 03:09 AM
You have to first mark the asset as dirty using EditorUtility.SetDirty(assetObject) Then it will be saved to disk when AssetDatabase.SaveAssets is called, either manually or when the project is saved.
SetDirty works well! Additionally If the object is part of a Scene, the Scene is marked dirty. If the object may be part of a Prefab instance, you additionally need to call PrefabUtility.RecordPrefabInstancePropertyModifications to ensure a Prefab override is created. from: https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
Answer by DaveA · Aug 16, 2012 at 12:49 AM
You probably want something in the AssetDatabase collection: http://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.SaveAssets.html
Answer by DESTRUKTORR · Aug 15, 2012 at 03:32 PM
There is no simple answer to this question, and many have asked similar questions, but there aren't really any efficient methods to save all assets' data at runtime, in Unity, like a game-save.
However, there are several methods to save large amounts of data that are commonly used:
I/O (Input/Output)(Javascript) (C#) <--- C# is much easier than JS, for this... trust me :P
Check out this answer.
Personally, I prefer C# I/O. It allows for a lot easier customization of your system, once you've got the base methods down (eg. where the files go, what they're called, all that fun stuff).
What Im looking for is to save a specific .asset file. Because I am building this on top of an existing project with the .asset format to keep the data, I cannot switch to I/O nor the X$$anonymous$$L format.
And it's actually not necessary to save that file at runtime. In my case my data is populated at runtime, and the .asset file is filled with the correct data as long as the unity editor application is running. But I quit unity, the data will be lost and the .asset file turns will revert to empty. So I'm looking for a method to save that before unity quits, which may not necessarily to be save at runtime.
Do you know how can I save that .asset file?
Answer by AWollert · Nov 22, 2019 at 12:11 PM
Since I have the same issue now in 2019 and didn't find any satisfying answer I wake this post up again. I am building my application (why would anyone not build it?) and therefore, I cannot use the UnityEditor classes AssetDatabase. How do you save your assets at runtime?
Best regards
The answer is, that anything you want to save during runtime you have to convert to a byte array and save it with File.WriteAllBytes(string path, byte[] bytes); -> https://docs.unity3d.com/ScriptReference/Windows.File.WriteAllBytes.html
Your answer
