Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Serialize fields only in Editor

Discussion in 'Scripting' started by zee_ola05, Sep 27, 2016.

  1. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Is there a way to serialize fields but only in Editor? In the example below. I have a prefab that is in Resources folder. I have a tool to parent it on a transform. I don't want PlacementTool to hold reference to the prefab when the game is built, I only want it in the Editor.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using FullInspector;
    4.  
    5. public class PlacementTool : BaseBehavior
    6. {
    7.     [SerializeField]
    8.     private GameObject m_prefab;
    9.  
    10.     [InspectorName ("Place")]
    11.     [InspectorButton]
    12.     private void PlacePrefab()
    13.     {
    14.         // Place prefab under this transform
    15.     }
    16.  
    17.     [InspectorName ("Delete")]
    18.     [InspectorButton]
    19.     private void PlaceInstance()
    20.     {
    21.         // Destroy prefab instance
    22.     }
    23. }
    24.  
     
  2. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Answering my own question. Looks like putting the fields inside #if UNITY_EDITOR does it.
     
    Alverik likes this.
  3. Xaon

    Xaon

    Joined:
    Feb 28, 2013
    Posts:
    62
    I'm digging this up for the sake on future googlers. DO NOT PUT any serialized fields under #if UNITY_EDITOR! It will screw you're builds. And it's so nasty that the serialization bug can pop randomly months after the code is written.

    (any serialized means: public, public with HideInInspector attribute and private/protected with SerializeFields attribute)
     
    Bunny83 likes this.
  4. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    147
    Could you elaborate on this? Do you know of an alternative or workaround for serializing variables only for the editor?
     
  5. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    147
    Ok, this was fun. Searching for this problem again after some time I found my own answer asking about it. Nice.

    I found that not only #if UNITY_EDITOR doesn't work, if that encloses an array, the game can crash on startup and report the asset with the serialized data as corrupt
     
  6. Xaon

    Xaon

    Joined:
    Feb 28, 2013
    Posts:
    62
    Sorry I haven't replied back then. Dunno how I forgot.
    If I guess correctly data for each component is serialized to flat byte stream. Unity reads all public/serialized fields as a description of what those bytes should mean. But when you put code under conditional compilation the description of fields becames invisible for Unity editor. Though probably there's more to the story.

    In order to have editor only data it has to be put in separate file or at least component. Then you can remove component from scenes/prefabs at runtime. You can also try removing those components during build but you'll have to revert changes on your repo each time after build or you'll need second repo just for that.
     
  7. HarryCodder

    HarryCodder

    Joined:
    Feb 20, 2015
    Posts:
    82
    Xaon and Bunny83 like this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    2,446
    Good spot. Yeah they may have fixed the build pipeline to specifically look out for editor only fields and re-serialize all assets when building the game. Though I haven't tested it yet myself.

    I'm actually confused what they actually mean by this line:
    This doesn't seem to make much sense to me. "UNITY_STANDALONE" is a platform define symbol which is also set in the editor if the current target is standalone. Of course you would run into issues when you switch platforms as this would again screw up the serialized data. Maybe they meant that case?

    Anyways, this was a good necro post. Correcting outdated information is a good thing. Though certain outdated topics should be simply ignored (like UnityScript related questions for example).
     
  9. HarryCodder

    HarryCodder

    Joined:
    Feb 20, 2015
    Posts:
    82
    I was wondering the same thing, it would be nice if a Unity dev can give some insights about that.

    I'm note sure if something like this would work (to have some kind of forward compatibility) :
    Code (CSharp):
    1. #if UNITY_EDITOR || !UNITY_2021_2_OR_NEWER
    2. public int m_inEditorOnlyIfSupported;
    3. #endif