- Home /
How can I get complete path of a sprite?
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());
}
Answer by valyard · Jun 02, 2015 at 11:31 AM
What you are doing is getting path to your sprite in Scene Hierarchy.
You probably need a script like this which prints a path in your project to sprite used with selected GameObject in your scene:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class Test : MonoBehaviour {
[MenuItem("Custom/Find Sprite")]
public static void FindSprite()
{
var selected = Selection.activeGameObject;
if (selected == null) return;
var renderer = selected.GetComponent<SpriteRenderer>();
if (renderer == null) return;
Debug.Log(AssetDatabase.GetAssetPath(renderer.sprite));
}
}
Add it to your project and in main menu you will see "Custom" item. Select a GameObject with a SpriteRenderer in your scene and click Custom > FInd Sprite in menu.
Just chi$$anonymous$$g in: This is an editor script. Is there a way to do this at runtime?
Your answer
![](https://koobas.hobune.stream/wayback/20220613200539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Filling list with editor scripts is empty on Play 1 Answer
invalid asset Id if player is connected 0 Answers
How to not allow ball to be thrown after first Toss 2 Answers