- Home /
How to serialize a scriptable object ?
How to Serialize this so i can save it in binary formatter
[CreateAssetMenu(menuName = "Item", fileName = "New Item")]
public class Item : ScriptableObject
{
public string itemName;
public int itemPrice;
public Sprite itemSprite;
}
Answer by logicandchaos · Feb 06, 2021 at 02:05 PM
Scriptable objects are serialized. Actually everything that shows up in the unity inspector is serialized, it has to be for unity to view it.
Not really, they're serializable. That's why scriptableobject data does not persist between edit and play mode (unity serializing and deserializing everything) if you don't specifically a) set them dirty, or b) save them explicitly.
but it does persist between edit and play mode, there are videos all about using scriptable object architecture based on that attribute. I work a lot with scriptable objects and all the data persists for me between edit and play mode.
You don't seem to have read my comment.
Answer by AbandonedCrypt · Feb 06, 2021 at 03:14 PM
ScriptableObject do not have to be serialized with any serialization tool. The good thing about ScriptableObjects is that they can natively be saved as .asset files (Note: when using CreateAssetMenu tag, these are automatically saved in your Assets and you don't need to go through the following steps)
You can use AssetDatabase.CreateAsset(Object ob, String path); to save a SO in your assets. You can use AssetDatabase.LoadAssetAtPath() to load your ScriptableObject asset at Editor time.
At runtime you can use whatever your preferred resource retrieval method is.
If you want your SO data to persist, you can use EditorUtility.SetDirty() or AssetDatabase.SaveAssets()
Follow this Question
Related Questions
How to Serialize Scriptable Object at Runtime? 1 Answer
How can I find all instances of a Scriptable Object in the Project (Editor) 3 Answers
Are ScriptableObjects good for non-visualised models? 2 Answers
[Custom Editor] MonoBehaviour vs Scriptable Object 0 Answers
How to share a script between multiple gameobjects but with different values 3 Answers