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. Dismiss Notice

Question Scripts only for editor not for builds

Discussion in 'Editor & General Support' started by iLollipop, Sep 5, 2023.

  1. iLollipop

    iLollipop

    Joined:
    Jan 19, 2023
    Posts:
    3
    hello

    Is there a way to mark a code to be only for development? I want to write some test lines but not build them .
    I know that i can go back and delete them when the game is ready for build but it is time consuming to check every script.

    sorry for my English
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Either surround the code in
    #if UNITY_EDITOR
    pre-processors, put the code in a folder called 'Editor', or have the code compiled into an editor-only assembly definition.

    Do note that you can't have editor only monobehaviours.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    if you want to write tests
    https://unity.com/how-to/automated-tests-unity-test-framework
     
    CodeSmile likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I'd prefer this:

    #if DEBUG
    ..
    #endif


    Because one may want to have a debug/development build with those lines active. ;)

    There are however issues with both approaches if the condition includes/excludes serialized fields. Because the editor may serialize some fields which are not compiled in a release build, thus causing at least an error in the release build because the serialized data doesn't match the serializable fields of the script.