Hello Unity Answers!
My current script is raising this exception whenever I try to set LOD levels: “SetLODs: Attempting to set LOD where the screen relative size is greater then or equal to a higher detail LOD level.”
Before I post the actual script, what is supposed to happen, and what is actually happening?
The script is supposed to create a new LODGroup out of a selection of GameObjects. The GameObjects have to be named MyGameObjectName_LOD0, MyGameObjectName_LOD1, etc. This works 100% fine as long as you only have one GameObject selected. When selecting a 2nd or more GameObjects the exception above is raised.
private void CreateLodGroup()
{
GameObject[] sel = Selection.gameObjects;
if (sel == null || sel.Length == 0)
{
Debug.LogError("FAILED -> You must have at least 1 GameObject selected!");
return;
}
var lods = new SortedList<int, GameObject>();
string mainName = null;
foreach (GameObject o in sel)
{
string oName = o.name;
Match regexMatch = Regex.Match(oName, @"(.*)_LOD([0-9])");
if (regexMatch.Success)
{
string oMainName = regexMatch.Groups[1].ToString();
int oIndex = Convert.ToInt32(regexMatch.Groups[2].ToString());
if (mainName == null || oMainName == mainName)
{
mainName = oMainName;
}
else
{
Debug.LogError("FAILED -> The GameObject main names aren't matching! Expected main name \"" +
mainName + "\", got \"" + oMainName + "\"!");
return;
}
if (!lods.ContainsKey(oIndex))
{
lods.Add(oIndex, o);
}
else
{
Debug.LogError("FAILED -> The LOD object with the ID " + oIndex +
" exists twice! Make sure to avoid duplicates!");
return;
}
}
else
{
Debug.LogError("FAILED -> GameObject \"" + oName +
"\" was in the wrong name format to be added as a LOD object!");
return;
}
}
int lfIndex = lods.Count - 1;
while (lfIndex >= 0)
{
if (lods.ContainsKey(lfIndex))
{
lfIndex--;
}
else
{
Debug.LogError("FAILED -> LOD level " + lfIndex + " could not be found!");
return;
}
}
var lodObject = new GameObject(mainName);
var lodGroup = lodObject.AddComponent<LODGroup>();
var lodLevels = new LOD[lods.Count];
foreach (var lod in lods)
{
float lodSize = (float)Math.Round(1d / lods.Count, 1);
Debug.Log("LOG -> Size=" + lodSize);
Renderer[] renderers = lod.Value.GetComponentsInChildren<Renderer>();
if (renderers == null || renderers.Length == 0)
{
Debug.LogWarning("WARNING -> No Renderers for LOD level " + lod.Key + " found!");
}
lod.Value.transform.SetParent(lodObject.transform);
LOD lodLevel = new LOD(lodSize, renderers);
lodLevels[lod.Key] = lodLevel;
}
lodGroup.SetLODs(lodLevels); // <!!> The exception is raised from calling this method! <!!>
}
I suspect that the exception is raised by a “bad” screenRelativeTransitionHeight (Documentation: http://docs.unity3d.com/). However I have tried out multiple values, all without success. I am kinda stuck here, so Id really appreciate any help possible c:
Thanks,
7BiT.psycho
Btw: The script is being executed in the editor.