Search Unity

Question How do I copy a scene through script, including its LightingDataAsset?

Discussion in 'Scripting' started by gwst1199, Mar 31, 2023.

  1. gwst1199

    gwst1199

    Joined:
    May 20, 2022
    Posts:
    4
    I want to copy and modify a scene via script, including its LightingDataAsset.
    However, the copied LightingDataAsset references the source scene.
    Opening the LightingDataAsset through AssetDatabase, I found that there was no Scene property on it.
    https://docs.unity3d.com/ScriptReference/LightingDataAsset.html
    But Inspect has the Scene property above it.
    I can manually modify the Scene reference.

    Is there any way I can modify it via script?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    gwst1199 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    The only way I've modified scenes via script is to load them, change them, then write them out... pretty sure this is the only way that works. This class is likely your friend:

    https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager.html

    But now that I see Spiney's post I actually don't see a way to reconnect a cloned LightingDataAsset to the scene.

    Perhaps if you just Instantiate<T>()-clone the LightingDataAsset and write it to the asset database as a fresh asset, then ransack the Scene file (via reading the lines of text from it on disk) for the GUID of the old LightingDataAsset and replace it with the new GUID of the clone?

    After manipulating the Scene asset like this you'd need an refresh call to the asset database.

    I've not played much with the LightingDataAsset obviously. :)
     
  4. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    I believe it's the other way around. The scene has an Object called LightmapSettings that inside has a serialized field called m_LightingDataAsset. You can see this by opening the scene in a text editor. I've not looked into how to use this information; I imagine it would involve some reflection. I hope this helps, though :).
     
    Kurt-Dekker likes this.
  5. gwst1199

    gwst1199

    Joined:
    May 20, 2022
    Posts:
    4
    https://forum.unity.com/threads/lighting-data-asset-was-a-mistake.547591/#post-8793973

    Thank you very much, this is a great reference!
    Now I can do it~
     
  6. gwst1199

    gwst1199

    Joined:
    May 20, 2022
    Posts:
    4
    Thank you very much for your help~
     
  7. gwst1199

    gwst1199

    Joined:
    May 20, 2022
    Posts:
    4
    Thank you very much for your help~