Find texture by name? Or other types?

I'm looking for a simple way to find and assign a texture by name. Analogous to Shader.Find. In fact, it would be nice to find any asset by name. I don't mean AssetPath.

So if I have a texture named "MyTexture" and it was created from MyTexture.png originally (but I may not know the extension), what's the best way to get it for something like

myComponent.tex = <FindTexture>("MyTexture");

I'd like to avoid using the AssetDatabase or Resources (assuming you really must have assets under Resources folder for that to work) if at all possible.

It is important to note that Unity doesn't include textures that an object doesn't reference unless it is in the Resources folder. This is so that you don't have to remove unused assets when you build your player, but it means that you cannot assume an asset will be there unless it is referenced by an object in a scene so you might be better off using Resources.

But, if you are still interested in gettng image data from a texture, you could get the bytes from the texture then load it using Texture2D.LoadImage()

public static class ExtensionMethods {
     public static bool LoadTexFromPath (this Texture2D tex, string fileName) {
          if(File.Exists(fileName)) {
              byte[] imageData = File.ReadAllBytes(fileName);
              return tex.LoadImage(imageData);
          }
          else {
              Debug.LogWarning("File doesn't exist");
              return false;
          }
     }
}

Then call it as such:

someTex2D.LoadTexFromPath("/AppPath/" + "/CurImage.png");

I wrote a method to find any texture by name. It is a slow process, but very reliable if you want to organize the textures into sub-folders.

public static class ResourceExt
{
    static Dictionary<string, Texture> loadedTextures = new Dictionary<string, Texture>();

    /// <summary>
    /// Find texture by name, if the path is unknown. Warning: It is a slow process and uses a lot of memory.
    /// </summary>
    public static Texture FindTexture(string name)
    {
	    Texture result;
        if (loadedTextures.TryGetValue(name, out result))
        {
            return result; //Already loaded the texture
        }
        else
        {
            //Search in all the textures that been loaded by unity
            Texture[] allTextures = Resources.FindObjectsOfTypeAll<Texture>();
            foreach (Texture tex in allTextures)
            {
                if (tex.name == name)
                {
                    loadedTextures.Add(name, tex); //Store the found texture
                    return tex;
                }
            }

            //Final trying to search the files and load everything
            allTextures = Resources.LoadAll<Texture>("");
			//Resources.LoadAll<Texture>("*" + name + ".*") or ("*" + name) or (name) doesn't work;

            foreach (Texture tex in allTextures)
            {
                if (tex.name == name)
                {
                    loadedTextures.Add(name, tex); //Store the found texture
                    return tex;
                }
            }

            loadedTextures.Add(name, null);
            Debug.LogError("Could not find texture: " + name);
            return null;
        }
    }
}