Search Unity

Adding volume overrides via script not saving?

Discussion in 'High Definition Render Pipeline' started by JohnKP-Mindshow, Feb 20, 2020.

  1. JohnKP-Mindshow

    JohnKP-Mindshow

    Joined:
    Oct 25, 2017
    Posts:
    56
    Using 2019.3.0f6 HDRP 7.1.8


    I'm trying to automate some of the post process setup for the scenes in my project on my port of a project to HDRP.

    In an editor script I do the following in this order for each scene in the project:
    1) Open scene.
    2) Add gameobject to scene.
    3) Add VolumeComponent.
    4) Create a VolumeProfile using VolumeProfileFactory.CreateVolumeProfileAtPath.
    5) Add needed components to the volume profile.
    6) Mark the volume profile, game object, and volume component as dirty.
    7) AssetDatabase.SaveAssets();
    8) AssetDatabase.Reload();
    9) Save scene.
    10) Close scene.

    If I stop my script before closing the first scene and inspect the VolumeProfile asset in the project, it has the components/volume overrides that I added via script.

    If the scene is closed that was open when the volume profile was created, the asset in the project no longer has any of the components/overrides that were added.

    Is there something I am overlooking?
     
  2. JohnKP-Mindshow

    JohnKP-Mindshow

    Joined:
    Oct 25, 2017
    Posts:
    56
    Code (CSharp):
    1. Scene scene = EditorSceneManager.GetActiveScene();
    2. int stringIndex = scene.path.LastIndexOf('/');
    3. string path = scene.path.Remove(stringIndex);
    4. string filePath = path + "/" + scene.name + "_Volume.asset";
    5.  
    6. GameObject obj = new GameObject("Skybox and post process volume");
    7. Volume volume = obj.AddComponent<Volume>();
    8. volume.isGlobal = true;
    9.  
    10. VolumeProfile volumeProfile = VolumeProfileFactory.CreateVolumeProfileAtPath(filePath);
    11. volumeProfile.Add<VisualEnvironment>();
    12. volumeProfile.Add<Fog>();
    13. volumeProfile.Add<HDRISky>();
    14. volumeProfile.SetDirty();
    15. volume.sharedProfile = volumeProfile;
    16. AssetDatabase.SaveAssets();
    17. AssetDatabase.Refresh();
    18. EditorUtility.FocusProjectWindow();
    19. Selection.activeObject = volumeProfile;
     
  3. JohnKP-Mindshow

    JohnKP-Mindshow

    Joined:
    Oct 25, 2017
    Posts:
    56
    Nevermind I figured it out!!

    Instead of
    Code (CSharp):
    1. volumeProfile.Add<VisualEnvironment>();
    try
    Code (CSharp):
    1. VolumeProfileFactory.CreateVolumeComponent<VisualEnvironment>(volumeProfile);
    To Unity devs:

    I would recommend making those more obvious, as I only found this out digging through your github. Perhaps if the VolumeProfile.Add<T> shouldn't be used directly, it should be set to private or have some sort of note point users to VolumeProfileFactory
     
  4. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    589
    Interesting, I also used the Add method and did not know about the other method. Thank you!
     
  5. K_kizaki

    K_kizaki

    Joined:
    Apr 4, 2016
    Posts:
    1
    I had same problem that add using volumeProfile.Add<T> method but not saved.
    However I resolved it by your helpful writing.
    Thank you so much!
     
  6. unity_6E755A5DE86160969147

    unity_6E755A5DE86160969147

    Joined:
    Feb 24, 2023
    Posts:
    1
    Thanks a lot! I was banging my head on the wall for this :))