Search Unity

Difference between AssetDatabase.Contains and EditorUtility.IsPersistent

Discussion in 'Asset Database' started by yasirkula, Feb 15, 2020.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    aurelioprovedo likes this.
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @yasirkula

    I think AssetDabase.Contains is something you use when you are sure you are only handling asset files (textures, prefabs, whatever). But like it says in EditorUtility.IsPersistent, it does this check:

    "Returns false if the object lives in the Scene."

    So it could be used to check if some Object you are handing isn't actually an asset file, but only part of a scene.

    Note - I've only actually used AssetDatabase.Contains...
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Thank you for the reply. I believe AssetDatabase's documentation also states the same thing: "Returns false if object is not an asset (for example object in the Scene, or an object created at runtime)"
     
    eses likes this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @yasirkula

    Hah - so my idea about it wasn't true then... maybe someone else knows better.
     
  5. aurelioprovedo

    aurelioprovedo

    Joined:
    Dec 26, 2012
    Posts:
    10
    My 2 cents, 3 years later:

    Looking at Unity's own C# code, I have seen that
    EditorUtility.IsPersistent
    is used 6x more often than
    AssetDatabase.Contains

    Both methods end up calling native code, which we don't have access to. So, until someone from Unity chimes in, I will stick to the seemingly preferred
    EditorUtility.IsPersistent
     
    yasirkula likes this.