Search Unity

AssetDatabase and temporary object deletion

Discussion in 'Scripting' started by theHost, Aug 2, 2017.

  1. theHost

    theHost

    Joined:
    Nov 30, 2009
    Posts:
    37
    Quick version: Is there a way to check if an asset in the AssetDatabase is in use or being referenced in any scene?

    Say I have create a new asset in the Scene, which internally creates a new material in the Asset Database (eg. AssetDatabase.CreateAsset(material, <path>)). Then I save the scene. Now the new asset lives in the scene, and has reference to the material in the Asset Database.

    Then if I delete the object in the scene, and I have a hook to also delete the material in the Asset Database (because I don't want to leave unused assets around). If I now don't save the scene, then when I reload it, the asset is still there, but the material is missing. Whats the best way to go about this?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,033
    It would be really useful to have, unfortunately there is no "where is this asset used" functionality. However, Unity supports the other way around, where you can get the dependencies of an asset.

    What you could do, is to get the scene dependencies and compare each dependency with your material.
    If a scene has a dependency to that material, it means the material is referenced/used in that scene.


    I found the following script very useful. It uses another application/process to scan for an asset references, thus by-passing Unity and it runs lot faster than doing the dependency search inside Unity:
    https://github.com/dantreble/FindRe...ssets/Scripts/Editor/FindProjectReferences.cs
     
unityunity