Destroy RenderSettings.customReflection

Hello

I read this link to change skybox and customreflection at runtime.

I’m working with WebPlayer.

My problems is that memory increases each time that i change the skybox and customreflection.

I’ve tried using destroy, but nothing:

// Change Reflection
        #if UNITY_EDITOR
            Destroy(RenderSettings.customReflection);
        #else
            DestroyImmediate(RenderSettings.customReflection, true);
        #endif
        RenderSettings.customReflection = Environments[this.m_iCurrentEnvironment].Reflection;

        // Change Skybox
        #if UNITY_EDITOR
            Destroy(RenderSettings.skybox);
        #else
            DestroyImmediate(RenderSettings.skybox, true);
        #endif
        RenderSettings.skybox = Environments[this.m_iCurrentEnvironment].Skybox;

How can i do a good clean of customReflection and Skybox?

Thanks

I’m not sure, but I’m guessing that memory use increases each time you change the skybox/reflection because you are loading new assets into memory each time you do so. Have you tried using Resources.UnloadAsset() to remove them from memory after you change to the new ones? I’m not sure just how many different skyboxes and cubemaps you’re using, but if the memory usage isn’t a huge problem, I’d recommend keeping them in memory until you’re sure they won’t be used in the scene anytime soon.

Thanks Hyblademin,

Finally I found where is the memory leak. Is not in “customReflection”, is not in “skybox”.

My complete code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Rendering;

[System.Serializable]
public struct Environment
{
    public GameObject Probe;
    public Material Skybox;
    public Cubemap Reflection;
}

public class EnvironmentLoader : MonoBehaviour
{
    #region Fields

    // Logger
    public Logger Logger;

    // Environments list
    public List<Environment> Environments;

    // Current
    private int m_iCurrentEnvironment = -1;

    #endregion

    #region Public

    public void Initialize(int iEnvironment)
    {
        // Disable current environment
        if (this.m_iCurrentEnvironment != -1) Environments[this.m_iCurrentEnvironment].Probe.SetActive(false);
      
        // Active selected environment
        this.m_iCurrentEnvironment = (iEnvironment < Environments.Count) ? iEnvironment : 0;
        Environments[this.m_iCurrentEnvironment].Probe.SetActive(true);
      
        // Change Reflection
        #if UNITY_EDITOR
            Destroy(RenderSettings.customReflection);
        #else
            DestroyImmediate(RenderSettings.customReflection, true);
        #endif
        RenderSettings.customReflection = Environments[this.m_iCurrentEnvironment].Reflection;
      
        // Change Skybox
        #if UNITY_EDITOR
            Destroy(RenderSettings.skybox);
        #else
            DestroyImmediate(RenderSettings.skybox, true);
        #endif
        RenderSettings.skybox = Environments[this.m_iCurrentEnvironment].Skybox;
      
        // Update
        Resources.UnloadUnusedAssets();
    }

    public void Clear()
    {
        if (this.m_iCurrentEnvironment == -1) return;
        Environments[this.m_iCurrentEnvironment].Probe.SetActive(false);
        this.m_iCurrentEnvironment = -1;
    }

    #endregion

    #region Private

    private void Start()
    {
        if ((Environments == null) || (Environments.Count == 0))
        {
            Logger.Display(Logger.ELog.Error, "EnvironmentLoader Start: there is not environments");
            return;
        }
      
        foreach (Environment oEnv in Environments) oEnv.Probe.SetActive(false);
    }

    #endregion
}

The problem is that for each “environment” i have a GameObject with a “Reflection Probe” (type: Realtime)
Every time that I change the “environment”, I activate or deactivate this object.

I created a small test scene with 2 objects, each object has a “Reflection Probe”:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    public GameObject Reflection1 = null;
    public GameObject Reflection2 = null;

    // Use this for initialization
    void Start ()
    {
        Reflection1.SetActive(false);
        Reflection2.SetActive(false);
    }
  
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Reflection1.SetActive(true);
            Reflection2.SetActive(false);
            return;
        }

        if (Input.GetKeyDown(KeyCode.Z))
        {
            Reflection1.SetActive(false);
            Reflection2.SetActive(true);
            return;
        }
    }
}

I created a “WebPlayer” build, and i tested in web browser. The memory increases each time that I active the “Reflection Probe”. I have also observed that if I test it in a PC that the GPU drivers are not updated (2010) the leaks are bigger.

Is my code wrong?
Is the right way to work with “Reflection Probe”-s?
Is a bug? Unity: 5.3.2f1

Thanks.