Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    59
    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)
     
  4. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    99
    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:
    99
    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:
    59
    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.
     
unityunity