Search Unity

Excluding unreferenced scripts from builds

Discussion in 'Scripting' started by Dougxing, Sep 15, 2008.

  1. Dougxing

    Dougxing

    Joined:
    Mar 13, 2008
    Posts:
    90
    I'm working on games for web deployment where every kilobyte of extra size counts.
    I have a script which loads some very big external DLL's, but I don't need it in every player I build from my project. Unfortunately, each build includes every script in the project, not just ones that are used in scenes or explicitly referenced, so I have to manually move the big script out, build, then move it back in.
    Can this be avoided?

    Along the same lines, I've been building streaming players with a fast-loading first scene and a larger scene that comes in underneath the first. From the build logs, it looks like a giant DLL from the second scene is being built into the first scene. Even though the code is only built-in once for both scenes, it's clearly bloating my first scene even though it is not needed at all. So I'd also like to keep script inclusion boxed-in within a multi-part streaming web player.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unlike texture, sounds, models, etc., Unity can't determine which scripts are used and which aren't, so no, I don't think that can be avoided. There was a discussion about this quite a while ago...I can't remember the exact reason, but it made sense at the time anyway. Generally scripts are such a tiny percentage of a project's size that it's not even worth worrying about, but if you're looking at every last K I can see that it might be annoying.

    --Eric
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    What Eric5h5 said. Basically, it's very hard to know that a script is really not referenced. Some of your code might do:
    Code (csharp):
    1. AddComponent("Foo" + "Bar")
    And that should already referenece a FooBar script, even if it's name is not mentioned anywhere in the other scripts. Some other code might access it via reflection, and so on.
     
  4. S.T.

    S.T.

    Joined:
    Sep 10, 2008
    Posts:
    40
    I have similar environment. I created several packages and exported them and copied the .unityPackage -file to Unity application folder along the default packages.

    Based on what type of assetBundle I am creating I import "Custom Editor", "Custom Loader" or other packages.

    So in your Loader scene you just need to have the minimum scripts and then load the other scenes, that you have created using other packages, dynamicly.

    However, I am still new to Unity3D and am not sure what kind of pitfalls there might be ahead. So far seems to work perfectly, though.

    Nice example:
    http://unity3d.com/support/resources/example-projects/assetbundles
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    Mono should know all compile-time dependencies and Unity could probably easily track those. Some people here in the forum use the default mono build environment for Unity scripts and there could be solutions to check which symbols are not referenced in a build.

    There's no good way to check for runtime dependencies, though (like in Aras' example). But as long as you make sure you never use strings to reference symbols you shouldn't run into problems.
     
  6. Dougxing

    Dougxing

    Joined:
    Mar 13, 2008
    Posts:
    90
    Certainly nothing I've written is that big, but the mono libraries... I'm seeing 3.3MB of included DLL's, bloating the build from 828K to 1.7MB. I really wish the US's broadband infrastructure was up to standards so this wouldn't matter :p

    I hadn't considered using packages, but it would be the same hassle of manually deleting and importing packages into the project for different builds, rather than moving and restoring individual files. I can't find anything in the docs about scripted package management. I'm pretty sure Asset bundles can't contain code, so there's no help to be had there.

    I figured that the issue must be some way of introducing runtime dependencies and I understand that it's untenable or even impossible to foresee them at compilation. But... If we promise to use it responsibly and accept another potential source of runtime errors, could Unity maybe add another checkbox in the build dialog to exclude unreferenced scripts? Adrian thinks it's probably easy to keep track of :) You could even make it unchecked by default.
     
  7. S.T.

    S.T.

    Joined:
    Sep 10, 2008
    Posts:
    40
    I am afraid you are right. I assumed the opposite :oops:

    Here is a thread about the issue:

    http://forum.unity3d.com/viewtopic.php?t=13123

    This seems very interesting, though...

    If anyone has experience trying this, would be very interesting to hear about it.
     
  8. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    I'm running into this very issue. I'm getting "'CreateText' is not a member of 'System.IO.File'." when trying to build a web build, but I'm not including that scene (level editor) in the build settings so it shouldn't need that script... is there really not a way to tell Unity to not include this script in the build?

    edit: NCarter pointed me here http://unity3d.com/support/documentation/Manual/Platform Dependent Compilation.html
     
    Last edited: Jan 5, 2011
  9. GADefence

    GADefence

    Joined:
    Aug 15, 2011
    Posts:
    1
    This is remarkably easy to do.

    Go into your file, cut and pasta the code you do not want from your project's asset folder, create your build, and boom, you're golden. This is obviously tedious if you're creating lots of builds and removing lots of scripts, but at that point you're probably doing it wrong.

    This also will not work if you have 1 script file in your entire build, at which point you need to comment it out.
     
  10. rodger_hu

    rodger_hu

    Joined:
    Apr 10, 2012
    Posts:
    5
    Move all you don't want compiled resource to WebPlayerTemplates folder. may be you need create WebPlayerTemplates folder first.