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. Dismiss Notice

AssetDatabase.GetAssetPath() doesn't work with Meshes.

Discussion in 'Asset Database' started by landon912, Jul 12, 2014.

  1. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Currently working on writing a saving system with XML. Where I write the file location of certain fields. Blah, Blah. My issue is that I can't retrieve the mesh asset path. For example:

    Code (CSharp):
    1. AssetDatabase.GetAssetPath(someMesh);
    Will return an empty string. I did a bit of investigating and if you right-click on a mesh in the Project window, and select "Show in Explorer". It opens to show a .blend(or any other format it is orginally in; I assume).

    How should I work around this. Currently I use GetAssetPath() at save time, and then GetAssetAtPath() at load time for such things as Texture2D and this works perfectly. How should I go about this?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    not sure if it helps, but i've used this in some editorwindow script:
    Code (CSharp):
    1. Debug.Log("meshasset:"+AssetDatabase.GetAssetPath(Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh));
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    AssetDatabase is of course an editor-only thing.

    The following code prints the (In my test case) 3ds Max file where the mesh is found.
    Code (CSharp):
    1.     Mesh mesh;
    2.     void OnGUI () {
    3.         mesh = (Mesh) EditorGUILayout.ObjectField( mesh, typeof( Mesh ), false );
    4.         if ( GUILayout.Button( "Mesh is where?" ) ) {
    5.             Debug.Log( AssetDatabase.GetAssetPath( mesh ) );
    6.         }
    7.     }
    8.  
    Naturally, if the mesh chosen is one of the primitives (Cube, Sphere) then the path is empty, because there's no asset that contains them.

    You should note that a .max file (or similar) can contain many mesh assets, which is why LoadAllAssetsAtPath exists.
    http://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAllAssetsAtPath.html
     
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Hmm, thanks. I'll look into it. By editor-only you mean, this won't work in a standalone build correct? What would an alternative be for built projected?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://docs.unity3d.com/ScriptReference/Resources.html

    Resources is sort of like the run-time equivalent, you have to put assets in a resources folder - anything you put in gets included in the build, even things you never actually use.

    It doesn't have any GetPath methods though. I'm not sure why you're really doing this, if you are creating meshes at runtime, then they are not assets, and therefore have no path; and if you are using meshes assigned by variable in the inspector, then you don't need to deal with paths at all.

    And yeah, i don't think the AssetDatabase class will compile (nor does anything in the UnityEditor nameapce)
     
    yakumopai likes this.
  6. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    The idea is to load the mesh asset path from an XML file, and assign that mesh to a variable. Essentially saving/loading the mesh location into a file.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    So you're generating asset paths during edit, then loading from path during run time - if so, then you can use AssetDatabase in the editor, and Resources in game.
     
  8. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    No, that's my issue. I need to generate the asset path and be able to load an asset from its path at runtime.
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Then I don't personally know of a way to do it.

    If it was me, I'd just stick all my meshes into a big Mesh[] allMeshes array, then you can just save and load their indices.
     
  10. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    403
    Any updates on this?
     
  11. jdrewsen

    jdrewsen

    Unity Technologies

    Joined:
    Mar 28, 2011
    Posts:
    201
    I suggest you ask in the addressables and/or the asset bundle sub forums as they are all about runtime access to assets.