How to properly handle field variables only used in Custom Editors

I have a class with a lot of variables and a custom editor handling them. Right now, I have the problem, that a lot of these variables referenced by the custom Editor via SerializedProperty’s are just used there and never in the actual class. I only need those to save certain things during editing them. Of course, MonoDevelop complains about unused variables at lot.

How do I store a state, only needed for an editor script on a per object basis, without having them as just “serialized and available” in the object making MonoDevelop mark all of them as unused. This approach does not even sound clean to me, but right now I don’t really know how to deal with it better.

The issue of your IDE giving you a warning about unused or unassigned variables is something you also have to deal with, when using the UnityEngine.SerializeField attribute to serialize private variables. For unassigned warning you could simply initialize you variables. For the other one you could specifically disable this type of compiler warning.

#pragma warning disable 0169
// field declaration which is never used
#pragma warning restore 0169

You usually want to restore it immediately after the block of variables, so that it still warns you of unintentional issues later in the file.

#pragma warning disable 0649
// field declaration which is never assigned to
#pragma warning restore 0649

I agree on your feeling, that is doesn’t sound clean to you storing editor-only data in your MonoBehaviour. Alternatively, you can use EditorPrefs: Unity - Scripting API: EditorPrefs

Be aware that these, like PlayerPrefs, designed to store small values, such as user interface settings. I would store a bool or an index to store the last shown view or selected item in my custom editor for example. You probably don’t want to store large amounts of data this way (although you can of course… just find a way to serialize anything to a big string and then parse it back like any text-based serializer, such as xml or json…), because it will be stored in the registry (on Windows) and it will stay there until you delete it. As a user I wouldn’t want programs to litter my drive this way, without an easy way to delete that data.

For more control over config data, create your own file. You can create an xml or json directly in you’re project’s “Resources” folder and load the file via: Unity - Scripting API: Resources.Load

Files in the resources folder will also (and always) be included in the final build. Better use the “Editor Default Resources” folder for editor only data and load via: Unity - Scripting API: EditorGUIUtility.Load
A XML reader/serializer is built into the .NET framework. For json you could use the builtin JavaScriptSerializer or a library like JSON.Net. I recommend json, because it is smaller, more succinct, easier to read, maybe even the more “modern” choice; but that’s another diskussion.

If you’re data shouln’t be edited by anyone inside the project, or is somewhat unrelated to the project’s assets, you can also just create it outside of the assets folder at any custom location and load it from there. That way, it doesn’t get imported as an asset (maybe you don’t want to it to slow down Unity if it’s very big, or you want to exclude it from version control etc.).

Another option would be to create a custom ScriptableObject asset inside your project and have it be editable via the inspector and also load it via the mentioned methods. Don’t forget the “CreateAssetMenu” to make things super easy in that case. :wink:

Feel free to ask, if you want me to elaborate. :slight_smile:

I think thats the most elaborate answer I’ve ever received, thank you for that.
I went with option one since it’s 5 variables that we’re talking about at the moment, but now I know how to deal with bigger projects.