- Home /
How to create a scriptableobject file with specific path through code?
I have a CSV file that holds all of my weapon's data (like ammo, damage, etc) and I have an editor script that read the CSV file and creates some Scriptableobject files to a folder. I can read CSV file for now but I don't know how to create a new file (like creating with CreatAssetAttribute()) through code. How to do that? (all code is in OnPostprocessAllAssets() method)
Answer by Long2904 · Aug 30, 2020 at 04:00 AM
OK, so first you need to create a scriptable object instance with ScriptableObject.CreateInstance(). Then you can use AssetDatabase to create new assets. For example:
// MyClass is inheritant from ScriptableObject base class
MyClass example = ScriptableObject.CreateInstance<MyClass>();
// path has to start at "Assets"
string path = "Assets/Your path.../filename.asset";
AssetDatabase.CreateAsset(example, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.FocusProjectWindow();
Selection.activeObject = example;
That's a lot of methods to create a single asset :) If you call AssetDatabase.CreateAsset(), you don't have to call SaveAssets() and Refresh(). Reference
@SolidAllow Thanks for answering! I've noticed it before but never actually tested it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010759im_/https://answers.unity.com/themes/thub/images/avi.jpg)