Skip to content

Instantly share code, notes, and snippets.

@frostyandy2k
Created June 28, 2020 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frostyandy2k/4e56798aa843dfb334db0ad9019d936e to your computer and use it in GitHub Desktop.
Save frostyandy2k/4e56798aa843dfb334db0ad9019d936e to your computer and use it in GitHub Desktop.
public class ScriptableObjectLoader : EditorWindow
{
private static readonly int TYPE = 0;
private static readonly int NAME = 1;
private static readonly int IMAGE_PATH = 2;
[MenuItem("Tools/Load Scriptable Object Data")]
public static LoadScriptableObjectsFromFile()
{
string path = "Assets/Cores/SomeCoolValuesForOurSO.csv";
using (var reader = new StreamReader(path))
{
// first line is the table header, we can skip or do something with it
reader.ReadLine();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
//Create new instance of the specific type read from file
// Note: you can also use the type system CreateInstance<MySOType>()
var newScriptableObject = (MySOType)ScriptableObject.CreateInstance(values[TYPE]);
newScriptableObject.name = values[NAME];
// Create the asset on the file system
AssetDatabase.CreateAsset(newScriptableObject, "Assets/MyDynamicallyCreatedSOFolder/" + values[NAME] + ".asset");
newScriptableObject.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(values[IMAGE_PATH]);
// optional
// AssetDatabase.SaveAssets();
// AssetDatabase.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment