I recently came across a strange behavior while loading text resource assets. In particular the Resources.LoadAll method does not accept Path.DirectorySeparatorChar characters in a path. In fact it only accepts Path.AltDirectorySeparatorChar characters. This behavior is different then standard .net file/folder methods that accept either Path.AltDirectorySeparatorChar or Path.DirectorySeparatorChar without distinction. What this means is that you can’t directly use Path.Combine to build a path and pass it to the Resources.LoadAll method you first have to replace any Path.DirectorySeparatorChar characters with Path.AltDirectorySeparatorChar characters.
The documentation for Resources.Load also does not mention this behavior.
I have submitted a bug report here –> https://fogbugz.unity3d.com/default.asp?533268_jgvrk2lbu1qm398e
using System.IO;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Handles settings registration.
/// </summary>
[InitializeOnLoad]
public class EditorInitialization
{
/// <summary>
/// Holds a value indicating whether the RunCallbacks method has been called at least once before.
/// </summary>
private static bool ranOnce;
/// <summary>
/// Initializes static members of the <see cref="EditorInitialization"/> class.
/// </summary>
static EditorInitialization()
{
EditorApplication.update += RunCallbacks;
}
private static void RunCallbacks()
{
if (!ranOnce)
{
// try to load resource
var path = Path.Combine("Test/SubFolder", "testfile"); // result is Test/SubFolder\testfile
// var data = Resources.LoadAll("Test/SubFolder/testfile", typeof(TextAsset)); // this line is successful
var data = Resources.LoadAll(path, typeof(TextAsset)); // this line fails
if (data != null && data.Length != 0)
{
Debug.Log("found it");
}
else
{
Debug.Log("not found! " + path);
}
ranOnce = true;
return;
}
// do stuff
}
}