Search Unity

Assign Lightmap Parameters to individual objects via C#

Discussion in 'Scripting' started by ArteyS, Feb 6, 2016.

  1. ArteyS

    ArteyS

    Joined:
    Dec 15, 2014
    Posts:
    16
    Hi All,

    I have been trying to assign custom lightmap parameters to my 100s of imported objects automatically when bringing them into unity.

    I can modify settings like the Important GI per object, but I have no clue how to set the LightmapParameters (Advanced Parameters) to another value in the array. Do I need to load reference from the asset database first? Before Applying It? I have tried that also, but getting all kinds of errors. Here is the snippet of code I'm using.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.                     //objectToAssignTo.gameObject.renderer.
    3.                     UnityEditor.SerializedObject Sob = new UnityEditor.SerializedObject(objectToAssignTo.gameObject.GetComponent<Renderer>());
    4.                     UnityEditor.SerializedProperty Sprop = Sob.FindProperty("m_LightmapParameters");
    5.                     var temp = Sprop.objectReferenceInstanceIDValue as System.Object as UnityEditor.LightmapParameters;
    6.                     Debug.Log(temp.name);
    7.                     //Debug.Log(Sob.FindProperty("m_ImportantGI").boolValue.ToString());
    8.                     //Debug.Log(Sob.FindProperty("m_LightmapParameters").objectReferenceInstanceIDValue.ToString());
    9.                     //Debug.Log(Sob.FindProperty("m_LightmapParameters").enumValueIndex.ToString());
    10.                     //Debug.Log(Sob.FindProperty("m_LightmapParameters").type);
    11.                     //UnityEditor.LightmapParameters lp = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Model_Library_General/Settings/LMSettings_Tag4.giparams", typeof(UnityEditor.LightmapParameters)) as UnityEditor.LightmapParameters;
    12.                     //Sob.FindProperty("m_LightmapParameters").objectReferenceInstanceIDValue = 5;
    13.                     //Sob.ApplyModifiedProperties();
    14. # endif
    I've tried all kinds of combinations, I'd really appreciate some clarification if anyone had any ideas.

    Thanks
     
  2. ArteyS

    ArteyS

    Joined:
    Dec 15, 2014
    Posts:
    16
    Ok, I found a clunky awkward way of doing this. I don't want to hurt the feelings of the Unity Devs, so be prepared for a messy solution until this is resolved.
    You can create dummy objects in the scene which have the lightmapparameters applied already. Then on instantiate, you reference the lightmapparameters of the instanced dummy objects, and apply it to the object that you are planning on assigning it to.

    Code (CSharp):
    1.  //objectToAssignTo.gameObject.renderer.
    2.                     UnityEditor.SerializedObject Sob = new UnityEditor.SerializedObject(GameObject.Find("RefCube").GetComponent<Renderer>());
    3.                     UnityEditor.SerializedProperty Sprop = Sob.FindProperty("m_LightmapParameters");
    4. UnityEditor.SerializedObject Sob2 = new UnityEditor.SerializedObject(objectToAssignTo.gameObject.GetComponent<Renderer>());
    5.                     UnityEditor.SerializedProperty Sprop2 = Sob2.FindProperty("m_LightmapParameters");
    6.                     Sprop2.objectReferenceInstanceIDValue = Sprop.objectReferenceInstanceIDValue;
    7. Sob2.ApplyModifiedProperties();
     
  3. lighter_cd

    lighter_cd

    Joined:
    Feb 26, 2014
    Posts:
    4
    this code can be work.
    Code (CSharp):
    1. UnityEditor.SerializedObject Sob = new UnityEditor.SerializedObject(r);
    2. UnityEditor.SerializedProperty Sprop = Sob.FindProperty("m_LightmapParameters");
    3. UnityEditor.LightmapParameters temp = Sprop.objectReferenceValue as UnityEditor.LightmapParameters;
    4. if(temp != null){
    5. Debug.Log(temp.name);
    6. }