I have a editor window where I let the user give some input which is related to a certain gameObject.
How do I save this input, so that on closing and reopening the window (and when the connection with the gameObject is somehow made again) I can show that exact same input, so that the user can reëdit it? (I am talking about positions of dragged GUI.Boxes)
Do I save it in a file, if so how? And how can the data in that file be related to the corresponding gameObject?
It sounds like you want to do something like this
[Serializable]
public class SerializableGUIMetaData {
public int x;
public int y;
// etc
}
public class MyScriptableObject : ScriptableObject { // MonoBehaviour should also work if the instance is in the scene
public SerializableGUIMetaData[] boxData = new SerializableGUIMetaData[1];
}
public class MyEditorClass : Editor {
MyScriptableObject myGUIBoxData;
public void UpdateData() {
myGUIBoxData.boxData[0].x = 10; // Example
Editor.SetDirty(myGUIBoxData); // This will make it "save" for you
}
}
Assuming the instance of MyEditorClass is a prefab or instance of a scriptable object in the scene or assets, the data should just be saved.
To create an instance of a scriptable object (if you didn’t already), you’ll want to do something like this:
T asset = ScriptableObject.CreateInstance<T> ();
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/New " + typeof(T).ToString() + ".asset");
AssetDatabase.CreateAsset (asset, assetPathAndName);
AssetDatabase.SaveAssets ();
Edit:
Another way that might be more suitable depending on your goals, is importing and exporting the data as a json. Shouldn’t be too difficult to find a class to turn a class into a json string for you via reflection.