- Home /
Question by
DanRad1287 · Mar 26, 2014 at 08:17 PM ·
issue
How do you add a mesh to a LOD Level in Code?
This is probably a noob question but I thought I should ask it anyway since I can't figure it out. Does anyone know how to add a mesh into a LOD:0 level in Code? I create a empty game Object and put a LODGroup Component on with just a LOD:0 level and a Culled Level. I want to put a certain mesh in the LOD:0 level but I really don't know how to do that in code.
void OnGUI () {
if (!selectionDetected)
{
OnSelectionChange();
selectionDetected = true;
}
if (meshes.Count > 0)
{
var selectedMeshesText = string.Format("{0} selected mesh{1}: {2}{3}",
meshes.Count,
meshes.Count > 1 ? "es" : "",
string.Join (", ", meshes.Take(25).Select(x => x.gameObject.name).ToArray()),
meshes.Count > 25 ? "..." : "");
GUILayout.Label(selectedMeshesText, GetExpandingTextStyle(), GUILayout.ExpandWidth(true));
distanceFromTerrain = EditorGUILayout.Slider ("Distance From Terrain", distanceFromTerrain, distanceMin, distanceMax);
GUILayout.Label("New Building LOD Model:", new GUILayoutOption[]{});
prefab = (GameObject)EditorGUILayout.ObjectField(prefab, typeof(GameObject), false, new GUILayoutOption[]{});
if (GUILayout.Button(string.Format("Create Building LOD{0}", meshes.Count > 1 ? "s" : "")))
{
processedMeshes = 0;
try
{
foreach (var mesh in meshes)
{
//Bring Old Models down to Terrain
ConformMeshToTerrain(mesh);
++processedMeshes;
//store gameObject reference
GameObject objToSpawn;
//Gather Selected Building's Name
var objName = mesh.name;
//Gather Selected Building's Position XYZ Values
var objPOS = mesh.transform.position;
//Gather Selected Building's Rotation XYZ Values
var objROT = mesh.transform.localEulerAngles;
//Gather Selected Building's Scale XYZ Values
var objSCAL = mesh.transform.localScale;
//spawn object
objToSpawn = new GameObject(objName);
//Add Components
group = objToSpawn.AddComponent<LODGroup>();
//Grabbing the LOD Component on New Empty Game Object
LODGroup objLodCmp = objToSpawn.GetComponent<LODGroup>();
//Testing to see if the New Game Object has a LODGroup Component
if (objLodCmp != null)
{
SerializedObject obj = new SerializedObject(objLodCmp);
//Creating a Variable that will allow me to access the data properties of the LODGroup Component
SerializedProperty prop2 = obj.FindProperty("m_LODs");
//Deleting the LOD:1 & LOD:2 from the Array
prop2.DeleteArrayElementAtIndex(1);
prop2.DeleteArrayElementAtIndex(0);
//This is Crticial for the above changes to take effect.
obj.ApplyModifiedProperties();
}
//Apply Position, Rotation and Scale to new GameObject
objToSpawn.transform.position = new Vector3(objPOS.x,objPOS.y,objPOS.z);
objToSpawn.transform.localEulerAngles = new Vector3(objROT.x,objROT.y,objROT.z);
objToSpawn.transform.localScale = new Vector3(objSCAL.x,objSCAL.y,objSCAL.z);
if(prefab != null)
{
Debug.Log("This is working!");
}
//DestroyImmediate(mesh.gameObject);
}
}
finally
{
EditorUtility.ClearProgressBar();
}
}
}
else
{
GUILayout.Label("No segments selected.", GetExpandingTextStyle(), GUILayout.ExpandWidth(true));
}
Repaint();
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613141241im_/https://answers.unity.com/themes/thub/images/avi.jpg)