- Home /
How do you save changes made with custom editor?
Hi, I'm writing custom editor something similar to what Sebastian Lague does in this series: Curve Editor . Video's TL; DR; You're adding some points in editor mode and draw bezier curve based on those points.
Now, what I want to do is to create similar editor, but than save result and show in game play mode as well. How would you go about saving this data? To sum it up I think it's enough to save points values from editor mode, and retrieve this values in game play mode, however I'm not sure how to do that.
Answer by Xarbrough · Mar 13, 2018 at 01:06 PM
I tried to skim through the video to see what the issue might be. I assume the author will show how to save the data later on, but he starts out placing his "path" class in the custom editor.
Simply move the "path" class to a new MonoBehaviour and add it as component to a GameObject in the scene. Then change the editor to a custom editor for that MonoBehaviour type to edit the data. If the path class and all of it's values are serializable, the data is saved.
Not sure, if part of the question, but showing the points as the video author does in the editor, is not as easy as showing them in the editor. The runtime does not have the UnityEditor.Handles classes and Gizmos are also only shown in the editor. There are other ways to show lines, e.g. via the LineRender, in the game, but that might be another question.
Thanks for a detailed answer, I'm not sure how would your solution work, I'll test it when I get home. I found out that my problem could be solved by using scriptableobjects in that case we create asset and assign values to it. In your solution however it seems like every instance of path class will read serialized value of path variable? And does it mean that if I modify it in play mode, modifications will be saved as well?
Answer by Longman1981 · Mar 14, 2018 at 09:33 AM
I think I got it now, and I'll explain what was problem for anyone else with same problem with understanding this issue.
Let's say you have custom TestScript, which doesn't inherit from anything at all. You set it serializable and have simple public int value. Now let's say in edit mode you create instance of this TestScript with "new" keyword, and set value to 10. Now let's say you enter play mode and you want to retrieve this value, so how would you find it? I thought I would create new instance of TestScript and if it's serializable than "value" would hold 10. However that's completely incorrect, Serializable means that object will be serialized, but you can't retrieve this serialized values only by knowing class, you need to find serialized object, you have no way to retrieve this "value" 10 in this case... Yes object of TestScript was serialized but you have no way of finding that serialized object. You have few other options: 1)Create ScriptableObject asset, than find this asset in Resources and retreive "value" 10 from it. 2)Attach your script to gameobject/prefab, than find with GetComponent/Instantiate prefab. In this case you will be able target certain object of TestScript type rather than TestScript itself. So to sum up you can't do this with custom script which doesn't inherit from unity's MonoBehavior/ScriptableObject