- Home /
How to make a serialized editor only field
Just like the title says, I need a class member that is editable in the inspector, is saved and loaded in the .prefab file when working in the editor, but for a player build that field does not exist in the runtime.
This other Unity answer is the closest thing I could find: http://answers.unity3d.com/questions/155662/howto-editor-only-properties.html
The #ifdef sounds like what I want if it weren't for the fact that Unity is throwing an error for him that for the player build that is requiring that the serialized field be present ... and the "answer" to that question isn't what I want because by making it non-serialized it will never be saved in the prefab even when working in the editor.
My use case for this is for my localized text objects. I would like to have a string field for localization houses called "Context" which can describe the use case of the string to remove ambiguity when translated.
I know I could store localization data somewhere else, and there's a lot of ways to do it ... but it makes so much sense to keep all of the localization data with the text object itself for a million reasons, we just need persistent, editor only data to do it.
I'd love something that could like like the following if the ideal #ifdef solution is not possible, the field might still be there but at least wouldn't have any data for a player build:
public class Translation : MonoBehaviour { [SerializeEditorOnly] public string Context; // explanation of string context (editor only) public string English; // english text public string German; // german text }
Could you use
#if UNITY_EDITOR
[SerializeField]
#endif
private float editorOnlyVar;
It seems better to just remove the variable but that might do what you want.
Answer by watsonsong · Jun 29, 2017 at 12:04 AM
I am seeking a way for SerializeEditorOnly. use UNITY_EDITOR define on the field will cause a lot of error report.
Answer by YoungDeveloper · Dec 07, 2017 at 10:25 PM
Only way to do i found
#if UNITY_EDITOR
[SerializeField]
#endif
private string _myField = string.Empty;
Personally i usually group it with #region tag in visual studio to make it more readable
#region EDITOR-ONLY Serialized
#if UNITY_EDITOR
[SerializeField]
#endif
#endregion
private string _myField = string.Empty;
Here's the result
Your answer
Follow this Question
Related Questions
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
'Unsupported type' error in custom editor script 4 Answers
Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers
Displaying System.objects not serialized by Unity in a Custom Editor 0 Answers
Serializing list of base and derived classes with custom inspector 1 Answer