Drag and drop Streaming Asset to Inspector to get file path

Hi!

TLDR;
Is there a way to fill in a string parameter in the Inspector by dragging a Streaming Asset file?

Explanation:
I use Streaming Assets to load a lot of files… It would be really helpful to be able to drag a file from the Streaming Assets folder to a string parameter on a script in the Inspector and get the full file path in the string parameter.

It seems like I would need a Custom Editor script to do this and then use the DragAndDrop API to get the file paths, but it is empty. It looks like dragging a Streaming Asset is not allowed and is ignored by the DragAndDrop API.

Any thoughts on how to do this?

Thanks,
Chris

2 Likes

Since you can use Application.streamingAssetsPath as a shortcut, the only other thing needed is file name and subfolders, which you could just fill in yourself. Why do you need to drag and drop?

Imagine typing the names in for 100 files… I don’t want to type the file names and subfolders for all those files. Drag and drop would be much easier and mirrors how actual assets are assigned.

1 Like

Forget it, I figured it out. Just make an Object parameter on a script and you can drag the Streaming Asset file right in.

3 Likes

In case anyone else is interested, this is how I got the drag and drop to work correctly…

This is the script where I will load the Streaming Asset using its file path, the Object allows me to drag and drop a Streaming Asset file:

public class StreamingImage : MonoBehaviour {
#if UNITY_EDITOR
    [SerializeField] private Object streamingAsset;
#endif
    [SerializeField] private string filePath;
}

This is the Editor script that actually fills in the file path string based on the Streaming Asset that I drag to the Object parameter:

[CustomEditor(typeof(StreamingImage))]
public class StreamingImageEditor : Editor {

    SerializedProperty filePath;
    SerializedProperty streamingAsset;

    const string kAssetPrefix = "Assets/StreamingAssets";

    void OnEnable()
    {
        filePath = serializedObject.FindProperty("filePath");
        streamingAsset = serializedObject.FindProperty("streamingAsset");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(streamingAsset);
        EditorGUILayout.PropertyField(filePath);

        if (streamingAsset.objectReferenceValue == null) {
            return;
        }

        string assetPath = AssetDatabase.GetAssetPath(streamingAsset.objectReferenceValue.GetInstanceID());
        if (assetPath.StartsWith(kAssetPrefix)) {
            assetPath = assetPath.Substring(kAssetPrefix.Length);
        }
        filePath.stringValue = assetPath;
        serializedObject.ApplyModifiedProperties();
    }
}
10 Likes

Pretty cool! Thanks for that.

Does this not just load the streaming asset as if you had just saved a reference to the gameObject?

Love this, thank you!

Hi, If you don’t need/want a custom inspector, and you request only one extension type (.txt in my case), you can do this (I use it in Unity 2021.3) :

public UnityEngine.Object myStreamingAssetFile**;** //filled by dragging in inspector from the streamingAssets folder

void MyScript() {
string zePath = Application.streamingAssetsPath + “/” + myStreamingAssetFile.name+“.txt”;
Debug.Log("The path is : "+zePath);
}

I have used the above technique of using only UnityEngine.Object. But this fails for me in WebGL builds in Unity 2022.2.15 because myStreamingAssetFile is simply null. And that’s why myStreamingAssetFile.name throws an exception. I have no idea what is going on there.

It’s been years since I have looked at this code (and missed your reply!), but I think you are right. Thanks for pointing that out!
The [SerializeField] private Object streamingAsset; should be surrounded with a #if UNITY_EDITOR guard so it is excluded from compilation. Only the string path to the asset is intended to be serialized onto the GameObject and compiled into the build.