- Home /
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.
Okay forgive me if im misinterpreting this, but perhaps wouldn't a quick fix be to create a member variable and assigning $$anonymous$$yTexture.png to that?
This is exactly the thing I'm trying to avoid. I have dozens of instances of a script, where I've added textures for a GUI, and now faced with assigning all those textures to all my instances. I'm writing an editor script to help with such occasions. For the moment I used Load$$anonymous$$ainAssetAtPath but I'd like something more general.
Right these are quiet obvious solutions so I hope i'm not co$$anonymous$$g across at patronizing, but is it possible to create a static var for myTexture in a management script, then within the dozen other scripts reference that one texture, just thinking of simple solutions rather than delving into editor scripts.
Thanks. I've done that for run-time assignments, but this would be a 'one-time' assignment. The script I ended up with thus far uses the physical path, pretty handy, just not as flexible as I'd like.
Answer by Peter G · Apr 23, 2011 at 12:33 AM
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");
Thanks, but this is for an Editor script, so I this process is mimic the user dragging a texture onto a variable in the Inspector (hundreds of times). At the moment I have: gc.propName=AssetDatabase.Load$$anonymous$$ainAssetAtPath(replacementAsset); but that also requires I know the full path to the asset. I'd like to just use the 'Asset Name' There should be a GetAssetByName() function one would think.
Yes but this has nothing to do with my problem at hand, thanks anyway
oh I get it. Sorry, but my answer is still the same. Write a method that takes the asset name as a parameter, appends the editor path on to the beginning, and then loads the resource.
Yeah that's what I did. I'm voting for a 'GetAssetByName' over at the feedback forum
Answer by vikingfabian-com · Jun 21, 2014 at 10:44 AM
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;
}
}
}