This is a follow up question of Importing different type of files into Lists. Where the original lists are acquired.
In this script I am processing the obtained lists, and comparing them with each other to determine whether or not a used material has duplicate functions. This using their most commonly known traits, which are: Shader name, Color property and a texture property
I have the feeling my script is far from optimal, and currently takes roughly 120 seconds to compare 1200 materials. Making the rough iteration time per material 0.10 Where 200 of them are unique.
In the future this script will be expanded to process more different details besides the color/name/texture as well, so advice on how to to keep the process more 'open for change' is appreciated as well.
public void InitShaderSorting()
{
//wipe data so it won't duplicate if button is pressed again
unique = new List<Material>();
broken = new List<Material>();
duplicate = new List<Material>();
dupof = new List<Material>();
for (int i = 1; i < TotalMaterial; i++)
{
//Filter broken shaders out first so they can never be a unique one
if (allMat[i].shader.name == "Hidden/InternalErrorShader")
broken.Add(allMat[i]);
//If no unique material has been assigned to compare with, assign one first
else if (unique.Count == 0)
unique.Add(allMat[i]);
//If it aint broken, and there is a unique one. Go and compare the shader properties
else
CompareShader(allMat[i], allMat[i].shader.name, allMat[i].HasProperty("_Color"), allMat[i].HasProperty("_MainTex"));
}
}
/// <param name="sn">Shader Name</param>
/// <param name="hc">Has Color Property</param>
/// <param name="ht">Has Texture Property</param>
void CompareShader(Material mat, string sn, bool hc, bool ht)
{
bool finished = false;
for (int i = 0; i < unique.Count; i++)
{
//Check if shader names are identical, if not continue as it can't be a duplicate
if (sn != unique[i].shader.name)
{
if (i == unique.Count - 1)
{
finished = true;
break;
}
else continue;
}
//Check if this material even uses a color
if (hc)
{
//If the color is a match and there is no texture to compare, mark as duplicate
if (unique[i].color == mat.color && !ht)
{
duplicate.Add(mat);
dupof.Add(unique[i]);
break;
}
else
{
if (i == unique.Count - 1)
{
finished = true;
break;
}
else continue;
}
}
if (ht)
{
//If the texture is a match, mark as duplicate
if (unique[i].mainTexture == mat.mainTexture)
{
duplicate.Add(mat);
dupof.Add(unique[i]);
break;
}
}
if (i == unique.Count - 1) finished = true;
}
//If we reached the end without a match, then this material is unique.
if (finished) unique.Add(mat);
}