3
\$\begingroup\$

I want to get a path of sprite using the spriteRenderer component in Unity.I want my path should be like - "asset/PATH OF MY FILE/FILENAME.EXTENSION". Currently I am using following code but it returns only parent name/path.

string GetSpritePath(Transform goPath){
    List<string> path = new List<string> ();

    Transform current = goPath.transform;
    path.Add(current.name);

    while (current.parent != null) {
        path.Insert(0, current.parent.name);
        current = current.parent;
    }

    return string.Join("/", path.ToArray());
}

Whats wrong with my code ?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Don't try to reinvent the wheel; the first thing you should do is to try find existing functionality and read the documentation. In your case you should use:

var path = AssetDatabase.GetAssetPath(this.renderer.sprite);

And your resources must be in the Assets directory. That is all.

Please see related documentation:

http://docs.unity3d.com/ScriptReference/AssetDatabase.GetAssetPath.html

\$\endgroup\$
5
  • 1
    \$\begingroup\$ This isn't really a complete answer. Perhaps quote or paraphrase the pertinent information and link the documentation as a source? \$\endgroup\$
    – Pip
    Commented Aug 1, 2015 at 11:39
  • \$\begingroup\$ You must be able to read documentation or you are not developer \$\endgroup\$ Commented Aug 1, 2015 at 11:43
  • \$\begingroup\$ Although that is quite opinionated, I agree, However, you have still answered the question improperly. \$\endgroup\$
    – Pip
    Commented Aug 1, 2015 at 11:49
  • \$\begingroup\$ I extended my answer \$\endgroup\$ Commented Aug 1, 2015 at 12:34
  • 1
    \$\begingroup\$ "You must be able to read documentation or you are not developer." Note that that is irrelevant to an answer being on this site; we expect answers to be able to stand the test of time as much as possible, and answers that just link to documentation are not considered appropriate (and will be removed). That's why @Pip was suggesting you expand your answer (as you have, which is good). \$\endgroup\$
    – user1430
    Commented Aug 31, 2015 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .