Get Resources Path Easy

Hello there,
for a while I was thinking of a easy way to get a Path from a Prefab that is stored in the Resources Folder, unfortunately I had a bigger Folder structure and I dont wanna rewrite the Path to my Prefabs every Time i change a Folder name or somthing. So i made this


public class ResourcePath
{
#if UNITY_EDITOR
    static public string GetPath(UnityEngine.GameObject gbj)
    {
        //Returns the path name relative to the project folder where the asset is stored.
        string path = UnityEditor.AssetDatabase.GetAssetPath(gbj);
        //after this, is what i want
        string at = "Assets/Resources/";
        //get this
        string newPath = path.Substring(path.IndexOf(at) + at.Length);
        //split string at . result is [0] corrent path [1] .prefab
        string[] fixedPath = newPath.Split('.');

        return fixedPath[0];
    }
#endif

}
its a Helper Class that gives u the Path to ur Prefab in the Resources Folder.

Im using it like this
#if UNITY_EDITOR
[SerializeField]GameObject prefab;
[SerializeField]string resourcesPath;
    void OnValidate()
    {
        resourcesPath = ResourcePath.GetPath(prefab);
    }
#endif
OnValidate() is updated every Time u change something in the Inspector
so u drag and drop the Prefab in the Slot and the helper class gives u the Path
Sign In or Register to comment.