Search Unity

Access to LODGroup data in script

Discussion in 'Wish List' started by Zergling103, Feb 16, 2012.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Hi,

    I've been needing to access the data in LOD groups which I can set and assign in the editor.

    The reason for this is, I need to be able to combine objects together using a script to reduce draw calls, separating submeshes according to material and, now, LOD. This way the combined mesh also has the ability to render at different LODs.

    Many thanks,

    Steve
     
  2. Kaldorei

    Kaldorei

    Joined:
    Aug 12, 2010
    Posts:
    112
    You actually can access it through SerializedObject :

    SerializedObject obj = new SerializedObject(ToLOD.GetComponent<LODGroup>());
    Debug.LogError(obj.FindProperty("m_LODs.Array.data[0].renderers").arraySize);

    But it's a little bit complicated :p
     
  3. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Thank you.

    May you elaborate a bit more on this? What properties are there to access through FindProperty(), or in other words, what strings can I put in there that will work? The documentation doesn't elaborate on what members it has.

    What kind of access do I have? Can I read and write, or just read? I need to be able to create LODGroups entirely through a script.

    Edit: Crap. It looks like SerializedObject is an Editor class, and I cannot use it in game or else it won't compile... :(
     
    Last edited: Feb 17, 2012
  4. Kaldorei

    Kaldorei

    Joined:
    Aug 12, 2010
    Posts:
    112
    Sample with my auto lod calculator

     
  5. joe_ict

    joe_ict

    Joined:
    Sep 15, 2010
    Posts:
    12
    1. How did you find the LODGroup component properties?

    2. I was able to get all the way to the following line..
    but this assignment does not actually happen to the prefab. I attached my script.

    Code (csharp):
    1. List<Transform> allLods = new List<Transform>();
    2. ..Adds to the list of allLods..
    3.  
    4. //Applying to Lod meshes to LODGroup components
    5. LODGroup prefabLodCmp = prefabObj.GetComponent<LODGroup>();
    6. if (prefabLodCmp != null){
    7.     SerializedObject obj = new SerializedObject(prefabLodCmp);
    8.     for (int i = 0; i < prefabLodCmp.lodCount; i++){
    9.         SerializedProperty prop = obj.FindProperty("m_LODs.Array.data[" + i.ToString() + "].renderers");
    10.         prop.arraySize = 1;
    11.         SerializedProperty propRenderer = prop.GetArrayElementAtIndex(0).FindPropertyRelative("renderer");
    12.  
    13.         propRenderer.objectReferenceValue = allLods[i].gameObject;
    14.     }
    15. }
     
    Last edited: May 4, 2012
  6. joe_ict

    joe_ict

    Joined:
    Sep 15, 2010
    Posts:
    12
    Found my answer. While the above code is correct, Unity always returns a COPY of the array reference, rather than the reference itself. This means the modified properties have to be applied back to the prefab in order for the modifications to take effect.

    Add the following line to the end:

    Code (csharp):
    1.  
    2.  obj.ApplyModifiedProperties();
    3.  
     
  7. Nuty

    Nuty

    Joined:
    Jun 12, 2012
    Posts:
    2
    can i have access the others properties in the lodgroup by this way ?
    i'll really like to delete lod's level but lodcount is read only and i don't know how to find the good name with FindProperty. also i'll like to change the percentage of the display distance ...
    everyting is possible through the inspector but maybe someone here knows how to do with the code.
     
    Last edited: Jun 12, 2012
  8. Nuty

    Nuty

    Joined:
    Jun 12, 2012
    Posts:
    2
    it works with that ...
    SerializedProperty prop2 = obj.FindProperty("m_LODs");
    prop2.DeleteArrayElementAtIndex(1);
    and
    SerializedProperty lods = obj.FindProperty("m_LODs.Array.data[0].screenRelativeHeight");
    for the display distance
     
    Last edited: Jun 14, 2012
  9. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    thanks!