- Home /
Save/Attach content of editorWindow to a gameObject
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?
I assume you mean linking scripts in the editor. Your use of "Draw" is rather confusing, I almost went off on a tangent about textures!
You probably want to use public/[SerializeField] and data objects that unity knows how to serialize (primitive types and arrays, not List/ArrayLists), and EditorUtility.SetDirty after the assignment. Note that you can make your own classes serializable with the [Serializable] attribute.
Your "drawing" confuses me as well. What's the purpose of the "drawing"? Without that information we can't suggest any solutions.
Your basic idea is correct.
It seems my tangent was probably what you were actually looking for then!
Give Texture2D.EncodeToPNG a look
I updated my question to be less detailed and more clear.
Tarlius, after rereading your comment that might be exactly what I need. It gives me the ability to save a class to disk? (Not sure how that works.. it gets a file? ) Example?
Answer by Tarlius · Mar 17, 2013 at 03:19 AM
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.