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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Exclude scripts from build

Discussion in 'Scripting' started by Desprez, May 25, 2022.

  1. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    I have some scripts that are for dev use only. They are attached to objects in a scene to do things with controls in the inspector.

    I want to automatically exclude these scripts from a build.
    It seems there are a few ways to do this, but none seem to be ideal.

    1) Place scripts in the editor folder.
    This doesn't work because scripts in the editor folder can't be attached to game objects.

    2) Use assembly definition to include only editor.
    Doesn't work for the same reason as 1. Scripts can't be attached to game objects.

    3) use #if ( UNITY_EDITOR )
    Works, but now the whole script is turned grey in VS and this doesn't lend itself to editing very well.

    Is there another way to do this? Alternately, is there a way to keep code readable using #if ( UNITY_EDITOR )?
     
    QuebecDev likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    If you're using the #if UNITY_EDITOR and #endif, the code between it will not be grey in VS if you're in the editor.
     
  3. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    I'm using both UnityEngine and UnityEditor.
    I just tried it again, and now it's not gray, but it was before. I dunno why.
     
  4. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
    Bump, Is there a way to exclude a script from build without having to use one of the 3 point above?

    Something like exclude Unity Object from build at build time... or a special Tag?

    I didn't see your post last week... so I have created a duplicate :/ It is scary my post was almost create the 25 may one year later!

    There is something with the month of May and Excluding object from build.... XD

    https://forum.unity.com/threads/exclude-script-from-build.1442044/#post-9054898
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    People have been asking about this for decades. It's probably not going to happen so plan accordingly. Here's how:

    Just use source control and delete the files you don't want. You can instantly bring them back anytime you like.

    Alternately, use source control and move the files to some other folder outside of the Unity Assets/ area.

    Work WITH your tooling, not against it.

    PROPERLY CONFIGURING AND USING ENTERPRISE SOURCE CONTROL

    I'm sorry you've had this issue. Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    I usually make a separate repository for each game, but I have some repositories with a bunch of smaller test games.

    Here is how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally the ONLY folders you should ever source control are:

    Assets/
    ProjectSettings/
    Packages/

    NEVER source control Library/ or Temp/ or Logs/
    NEVER source control anything from Visual Studio (.vs, .csproj, none of that noise)

    Setting git up with Unity (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It's simply ridiculous not to back up.

    If you plan on joining the software industry, you will be required and expected to know how to use source control.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,116