Search Unity

Resolved Unity Does Not Remove Unused Assets even they're if outside Resources folder

Discussion in 'Editor & General Support' started by cyangamer, Feb 21, 2021.

  1. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    234
    I've been following this article and I noticed there are some glaring inaccuracies. Name this paragraph:

    I've attached a gif showing an issue with this:

    All of the scenes in the game are opened additively. And yet, the file I reference (along with many other files) are not being used by any scene.

    In my defense I looked for official unity documentation on how Find References in Scene works, so if there's an article somewhere that explains that better I'd appreciate it.

    It is possible that that file is part of a prefab, but it won't be a prefab that's instantiated or part of any component in this game.

    How does Unity actually determine what files are being used? And is there a solution to my problem besides "go through the build report and manually delete all the unused files"? The article I posted up top specifically said I wouldn't need to do that.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Is the prefab in a Resources folder? Think of it as all one big tree that Unity goes out and gathers. It will include anything referenced by any included scene PLUS anything referenced by anything in a Resources folder.

    aka, "Dependencies."
     
  3. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    234
    The only thing in my Assets/Resources folder is my MasterAudioSettings file. No prefabs there.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Not sure how handy you are with command line stuff, but if you are... what I like to do is search for a suspiciously-included file's GUID in the entire project.

    I have a utility called
    grp
    to do this fairly easily:

    https://pastebin.com/kaVEgYqT

    It's in Python (v2). Someday I'll make it v3 but just haven't bothered yet.

    For instance, here is finding the
    truss1_x10.blend
    object's references in my Jetpack Kurt game:

    First I get the GUID out of that Blender file's meta file:

    Assets kurt$ head ./SpaceFlight/Models/truss1/truss1_x10.blend.meta
    fileFormatVersion: 2
    guid: 4883ab464766f414cba6eaafeb905a27
    ...


    Now I use that at the root of the project (-l option says only the filename):

    Assets kurt$ grp -l 4883ab464766f414cba6eaafeb905a27 \*.unity,\*.prefab,\*.asset


    and I find it is referenced three places:

    Assets kurt$ grp -l 4883ab464766f414cba6eaafeb905a27 \*.unity,\*.prefab,\*.asset
    option_fileonly enabled
    File: ./SpaceFlight/Prefabs/TrussTower10Assembly_prefab.prefab:
    File: ./SpaceFlight/SpaceFlightContent1/SpaceFlightContent1.unity:
    File: ./SpaceFlight/SpaceFlightContent0/SpaceFlightContent0.unity:


    Pick a file you are POSITIVE isn't referenced and do the above. You may have to "Chain it on" if it is referenced by several items leading up to something being included in the build.
     
    cyangamer likes this.
  5. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    234
    Thanks! That script looks really useful. I put python2 back on my system (i usually use python3) and first ran it on files I know I'm not using, and then on files I know I'm using. I keep getting this back:


    PS C:\...\unity.sunsear.mobile> C:\Python27\python.exe grp.py -l 7e5eedba094424c46a4cc7a87f763037 \*.unity,\*.prefab,\*.asset
    option_fileonly enabled
    Don't forget to quote glob chars on Posix shells.


    I guess it's not finding anything. But in any case, after I mentioned Assets/Resource directory I began to wonder something. I then discovered something horrifying about Unity by reading this article on Resources. Apparently if you name a folder Resources, those files also end up in the build:
    What a terrible, awful, no-good, very bad system. No wonder even the folks at Unity say not to use it. I'm going to try clearing the 3rd-party prefabs from a Resources directory I found and see what happens.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It's definitely dirty the way it folds file namespaces over on one another. You could have two identically-named files accessible from the same Resources.Load<T>() path.

    But all in all, I use it when appropriate, don't otherwise.

    The biggest takeaway caution for Resources.Load<T>() is to ALWAYS use the generic version, otherwise if you have two identical asset names (even different types), the un-generic version does not guarantee you get the type you want, so your cast-as might fail.

    The other thing is: Use lots of subdirectories!
     
  7. cyangamer

    cyangamer

    Joined:
    Feb 17, 2010
    Posts:
    234
    Absolutely. I can see its usefulness. I guess my annoyance comes from 3rd party stuff using it, since now that decision is taken away from me. I'm glad I'm aware of that issue though. I removed the prefabs from that Resources directory like I said I would I was able to make those files no longer show up from the GIF I posted.

    It looks like that's all it was.
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    As your last remark there swerves closer to the techniques available for controlling what goes into your build...

    Let me suggest also partial scenes additively loaded as being awesome tools. You can load partial scenes that contain anything, and all that goes in the build as long as that scene is part of the build, no muss, no fuss.

    And with that I shall leave you with links to some more of my mad ravings about partial scene additive loading.

    Additive scenes are one possible solution:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718
     
    cyangamer likes this.