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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Editor Extension -> Get asset path from selected scene object?

Discussion in 'Scripting' started by KyleStaves, Feb 1, 2012.

  1. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Hello,

    First - thanks in advance for any tips/suggestions here.

    1) Get the AssetPath of Selection.activeGameObject's Prefab if it is, in fact, a prefab.

    Is it possible to get the GUID of a scene object? I can't seem to find anything in the scripting reference that would allow for it. Thanks in advance!
     
    Last edited: Feb 1, 2012
  2. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Just in case anyone else encounters this in the future, here is what I used to accomplish this...

    Code (csharp):
    1.  
    2. GameObject prefab = PrefabUtility.FindPrefabRoot(Selection.activeGameObject);
    3.         UnityEngine.Object prefabObject = null;
    4.         if (prefab != null){ prefabObject = PrefabUtility.GetPrefabParent(prefab); }
    5.         if (AssetDatabase.Contains(prefabObject) == false){
    6.             Debug.LogError("Selected terrain is not a prefab, or prefab could not be located. Please create a prefab before continuing...");
    7.         }
    8.  
     
    thomskyCan and guywald like this.
  3. Jashengmato

    Jashengmato

    Joined:
    Mar 23, 2013
    Posts:
    11
    Thanks dude, i'm writing an editor extension that need to do this too.
    Your codes saved my time!
     
  4. Jashengmato

    Jashengmato

    Joined:
    Mar 23, 2013
    Posts:
    11
    This doesn't work in "play" mode...
     
  5. Mahmoud-AlSati

    Mahmoud-AlSati

    Joined:
    Aug 14, 2013
    Posts:
    4
    Use AssetDatabase.GetAssetPath method.