- Home /
Editor extension data saving
Hello!
I try make tool in editor which i can use create new parts to car. In tool i cive name part and how much it create HP ja NM and then there is button "Save" when i press that save button i don't know what i can do.
I want that save it somewhere and then i can use it from game? And i can't do that. I think that maybe i need some class there? But how editor can save that so it remember it. Because if i save all that data some class it vanish when i close editor and start it again.
Thank you. I hope you undestand what i mean. =) and almost forgot. I use C# and i have that tool which ask part name, HP and NM my only problem is that saving system.
You have to read up on Binary Serialization, although it would also be possible to save the data as a long string and save it to a text file directly. It can be a very complex topic for beginners so be prepared fora lot of reading and trial & error :)
Answer by Bunny83 · Dec 05, 2016 at 01:30 PM
What you described sounds like you want to use a ScriptableObject derived class for that. A class derived from ScriptableObject is a serialized data-class that doesn't need to be attached to a gameobject. It just holds data. Those classes can be used at runtime to access that information. Such a class can be saved as asset into your project where you can even edit the information in the inspector.
As far as i understood all information you want to store is:
a string for the name
a float for the HP
a float for the NM
Such a class would look like this:
[CreateAssetMenu(fileName = "CarPart", menuName = "Create new CarPart")]
public class CarPart : ScriptableObject
{
public string name;
public float HP;
public float NM;
}
You can create a new asset instance either via the new menu item in the "create" menu (that's what the createassetmenu attribute is good for) or via code inside an editor script.
// create a new instance via code. This is not yet saved anywhere. It just exist in memory
CarPart part = ScriptableObject.CreateInstance<CarPart>();
// save the instance as asset into your project. It will be saved at the asset path you provide.
AssetDatabase.CreateAsset(part, "SomeProjectFolder/PartName.asset");
Note that the assetpath is relative to your Assets folder and the asset name need to have the extension ".asset".
Inside another runtime class (like a MonoBehaviour) you can create a List / array of "CarParts" and assign your created assets to that list.
public CarPart[] parts; // assign in inspector
Important: The CarPart class is a runtime class so it must not be placed in an editor folder. Generally things in an editor folder only exist while you're inside the editor. At runtime those things don't exist.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102015im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Wait until scripts compile 1 Answer
Distribute terrain in zones 3 Answers
How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer