How can I check to see if a resource with a particular name exists? I have some objects that have e.g. a state "1000", "0100", "0010", or "0001". If the resource with that name doesn't exist, it changes the object's rotation and tries the next one, e.g. "0100" uses the same texture as "1000" but rotated 90 degrees.
you can load all texture resources that they are related to your set of GameObjects using LoadAll and then check if what you want is in the returned array or not. if yes, you can use it and if no, do whatever you want. when the exist function does not exist in any situation you can create it by getting all elements and using some sort of search algorithm. you can use texture names to identify your textures. note that the LoadAll method returns an array of objects and you need to cast them to the type that you want.
You can check the names in file and compare them.
sample usage
private string[] GetLevelNamesFromResources(string name)
{
List<string> PlayersInFolder = new List<string>();
string myPath = "Assets/Resources/" + name;
DirectoryInfo dir = new DirectoryInfo(myPath);
FileInfo[] info = dir.GetFiles("*.*");
foreach (FileInfo f in info)
{
if (f.Extension == ".prefab") //f.Extension == ".FBX" ||
{
string tempName = f.Name;
string extension = f.Extension;
string strippedName = tempName.Replace(extension, "");
PlayersInFolder.Add(strippedName);
}
}
Debug.Log(PlayersInFolder.Count + " adet level temp array içine konuldu.");
return PlayersInFolder.ToArray();
}
private void CheckLevelNamesInArray()
{
List<string> tempArray = GetLevelNamesFromResources("Levels").ToList();
foreach (var name in _levelNames)
{
if (!tempArray.Contains(name))
{
Debug.LogError("Error name level : " + name);
}
}
}
Just for reference, since you already have this solved by now, if you load resources using Resources.Load then your question is already answer in first lines of that doc, and unlike your conclusion from the brief comment-discussion with Jessy:
Returns the asset at path if it can be found otherwise returns null.
https://docs.unity3d.com/ScriptReference/Object-operator_Object.html
It's not specific to name-related stuff, but it will certainly work for you.