Search Unity

Releasing ComputeBuffer in editor sceneview script.

Discussion in 'General Graphics' started by Avol, Nov 1, 2019.

  1. Avol

    Avol

    Joined:
    May 27, 2016
    Posts:
    46
    I'm using such script to run OnRenderImage in scene view, but other mono events like OnDisable, Awake etc don't get called, hence unable to release compute buffers (OnApplicationQuit seems to work, but its not the right place to release the buffers). Getting flooded with warnings, any ideas how to solve this?

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class SceneViewCameraProxy : MonoBehaviour
    3. {
    4.     #if UNITY_EDITOR
    5.     private SceneView SceneView;
    6.     private Camera Camera;
    7.  
    8.     public void OnEnable()
    9.     {
    10.         Camera = GetCamera();
    11.  
    12.         if (Camera != null)
    13.             UpdateComponents();
    14.  
    15.         EditorApplication.update += TryUpdate;
    16.     }
    17.  
    18.     void TryUpdate()
    19.     {
    20.         SceneView    = SceneView.lastActiveSceneView;
    21.         Camera        = GetCamera();
    22.  
    23.         if (SceneView != null)
    24.             EditorApplication.update -= TryUpdate;
    25.     }
    26.  
    27.     private Camera GetCamera()
    28.     {
    29.         SceneView = SceneView.lastActiveSceneView;
    30.  
    31.         if (SceneView == null)
    32.             return null;
    33.  
    34.         return SceneView.camera;
    35.     }
    36.     private Component[] GetComponents()
    37.     {
    38.         var result = GetComponents<Component>();
    39.         return result;
    40.     }
    41.     private void UpdateComponents()
    42.     {
    43.         var components = GetComponents();
    44.         if (components != null && components.Length > 1)
    45.         {
    46.             var cameraGo = Camera.gameObject;
    47.  
    48.             for (var i = 0; i < components.Length; i++)
    49.             {
    50.                 var c = components[i];
    51.                 Type cType = c.GetType();
    52.                 var existing = cameraGo.GetComponent(cType) ?? cameraGo.AddComponent(cType);
    53.  
    54.                 //Debug.Log(cType.ToString());
    55.  
    56.                 if (cType.ToString() == "PRenderer" || cType.ToString() == "Camera")
    57.                     EditorUtility.CopySerialized(c, existing);
    58.             }
    59.         }
    60.     }
    61.     #endif
    62. }
    63.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,916
    Why not release them at the end of the OnRenderImage function?
     
  3. Avol

    Avol

    Joined:
    May 27, 2016
    Posts:
    46
    I guess that could work, though would need to rebuild buffers on each frame.
     
unityunity